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
20 changes: 19 additions & 1 deletion Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import warnings as original_warnings
from warnings import deprecated


py_warnings = import_helper.import_fresh_module('_py_warnings')
py_warnings._set_module(py_warnings)

Expand Down Expand Up @@ -1237,6 +1236,25 @@ def test_issue31566(self):
support.swap_item(globals(), '__file__', None):
self.assertRaises(UserWarning, self.module.warn, 'bar')

@support.cpython_only
# Python built with Py_TRACE_REFS fail with a fatal error in
# _PyRefchain_Trace() on memory allocation error.
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
def test_issue151673(self):
# Skip this test if the _testcapi module isn't available.
_testcapi = import_helper.import_module('_testcapi')
# warn() shouldn't crash when the "<sys>" fallback filename
# can't be allocated under memory pressure.
code = """if 1:
import _testcapi, warnings
warnings.simplefilter("always")
_testcapi.set_nomemory(0)
warnings.warn("boom")
"""
rc, out, err = assert_python_failure("-c", code)
self.assertIn(rc, (1, 120))
self.assertIn(b'MemoryError', err)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not make this a try/except?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skip is now handled inside the test trough import_helper.import_module



class WarningsDisplayTests(BaseTest):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in :func:`warnings.warn` when the ``"<sys>"`` fallback filename
could not be allocated under memory pressure: :c:func:`!setup_context` left
the filename ``NULL`` and it was later passed to :c:func:`Py_DECREF`.
13 changes: 10 additions & 3 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,18 @@ setup_context(Py_ssize_t stack_level,
}
}

/* Initialize the output references so handle_error can Py_XDECREF them
even when we bail out before they are assigned. */
*filename = NULL;
*registry = NULL;
*module = NULL;

if (f == NULL) {
globals = interp->sysdict;
*filename = PyUnicode_FromString("<sys>");
if (*filename == NULL) {
goto handle_error;
}
*lineno = 0;
}
else {
Expand All @@ -1043,8 +1052,6 @@ setup_context(Py_ssize_t stack_level,
Py_DECREF(f);
}

*module = NULL;

/* Setup registry. */
assert(globals != NULL);
assert(PyAnyDict_Check(globals));
Expand Down Expand Up @@ -1084,7 +1091,7 @@ setup_context(Py_ssize_t stack_level,
handle_error:
Py_XDECREF(*registry);
Py_XDECREF(*module);
Py_DECREF(*filename);
Py_XDECREF(*filename);
return 0;
}

Expand Down
Loading