Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-126209: Fix inconsistency of skip_file_prefixes in warnings.warn's C and Python implementations #126329

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,18 @@ def test_skip_file_prefixes(self):
warning_tests.package("prefix02", stacklevel=3)
self.assertIn("unittest", w[-1].filename)

def test_skip_file_prefixes_file_path(self):
# see: gh-126209
with warnings_state(self.module):
skipped = warning_tests.__file__
with original_warnings.catch_warnings(
record=True, module=self.module,
) as w:
warning_tests.outer("msg", skip_file_prefixes=(skipped,))

self.assertEqual(len(w), 1)
self.assertNotEqual(w[-1].filename, skipped)

def test_skip_file_prefixes_type_errors(self):
with warnings_state(self.module):
warn = warning_tests.warnings.warn
Expand Down
10 changes: 6 additions & 4 deletions Lib/test/test_warnings/data/stacklevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import warnings
from test.test_warnings.data import package_helper

def outer(message, stacklevel=1):
inner(message, stacklevel)

def inner(message, stacklevel=1):
warnings.warn(message, stacklevel=stacklevel)
def outer(message, stacklevel=1, skip_file_prefixes=()):
inner(message, stacklevel, skip_file_prefixes)

def inner(message, stacklevel=1, skip_file_prefixes=()):
warnings.warn(message, stacklevel=stacklevel,
skip_file_prefixes=skip_file_prefixes)

def package(message, *, stacklevel):
package_helper.inner_api(message, stacklevel=stacklevel,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix an issue with ``skip_file_prefixes`` parameter which resulted in an inconsistent
behaviour between the C and Python implementations of :func:`warnings.warn`.
Patch by Daehee Kim.
3 changes: 2 additions & 1 deletion Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,8 @@ is_filename_to_skip(PyObject *filename, PyTupleObject *skip_file_prefixes)
for (Py_ssize_t idx = 0; idx < prefixes; ++idx)
{
PyObject *prefix = PyTuple_GET_ITEM(skip_file_prefixes, idx);
Py_ssize_t found = PyUnicode_Tailmatch(filename, prefix, 0, -1, -1);
Py_ssize_t found = PyUnicode_Tailmatch(filename, prefix,
0, PY_SSIZE_T_MAX, -1);
if (found == 1) {
return true;
}
Expand Down
Loading