warnings.warn's skip_file_prefixes doesn't work when provided __file__
.
#126209
Labels
interpreter-core
(Objects, Python, Grammar, and Parser dirs)
type-bug
An unexpected behavior, bug, or error
Bug report
Bug description:
I was attempting to make warning.warn skip all frames in the calling file by setting
skip_file_prefixes=(__file__,)
. Frustratingly it wasn't working as expected and was blaming an intermediate frame in the file I was trying to skip. Looking atLib/warnings.py
I was confused as to why it wasn't working. After some digging I discovered there is a parallel C implementation inPython/_warnings.c
.The following script demonstrates the difference between the two implementations:
Broken C implementation:
Correct Python implementation:
There is a slight implementation difference between
Python/_warnings.c:is_filename_to_skip
andLib/warnings.py:_is_filename_to_skip
. The Python implementation is usesfilename.startswith(prefix)
1 while the C implementation usesPyUnicode_Tailmatch(filename, prefix, 0, -1, -1)
2. It looks like the C implementation should be something likePyUnicode_Tailmatch(filename, prefix, 0, PY_SSIZE_T_MAX, -1)
.CPython versions tested on:
3.12
Operating systems tested on:
Linux
Footnotes
https://github.com/python/cpython/blob/679dfaeb4c78a138441a2073530be105c6752766/Lib/warnings.py#L280 ↩
https://github.com/python/cpython/blob/3ef8a3a9004ff7ed2bf6f2f67f13ff5b37c42204/Python/_warnings.c#L809 ↩
The text was updated successfully, but these errors were encountered: