Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,19 @@ def test_skip_file_prefixes_type_errors(self):
with self.assertRaises(TypeError):
warn("msg", skip_file_prefixes="a sequence of strs")

def test_skip_file_prefixes_empty_tuple_subclass(self):
with warnings_state(self.module):
warn = warning_tests.warnings.warn
class EmptyTuple(tuple):
pass
# Passing an empty tuple subclass used to lead to a crash.
for _ in range(3):
with self.module.catch_warnings(record=True) as w:
warn("msg", skip_file_prefixes=EmptyTuple())
self.assertEqual(len(w), 1)
self.assertEqual(w[-1].filename, __file__)
self.assertEqual(w[-1].module, __name__)

def test_exec_filename_module(self):
filename = "<warnings-test>"
codeobj = compile(("import warnings\n"
Expand Down
Comment thread
jelle-openai marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :func:`warnings.warn` when ``skip_file_prefixes`` is an empty tuple subclass.
Propagate allocation errors when creating the fallback filename.
4 changes: 3 additions & 1 deletion Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,9 @@ setup_context(Py_ssize_t stack_level,
if (f == NULL) {
globals = interp->sysdict;
*filename = PyUnicode_FromString("<sys>");
if (*filename == NULL) {
return 0;
}
*lineno = 0;
}
else {
Expand Down Expand Up @@ -1196,7 +1199,6 @@ warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
stacklevel = 2;
}
} else {
Py_DECREF((PyObject *)skip_file_prefixes);
skip_file_prefixes = NULL;
}
}
Expand Down
Loading