From b3c9faf056e7d642785a8cfd53d1184b37a74a69 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Mon, 16 Oct 2023 15:39:23 +0100 Subject: [PATCH] gh-110912: Correctly display tracebacks for MemoryError exceptions using the traceback module (#110921) --- Lib/test/test_traceback.py | 11 +++++++++++ .../2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst | 2 ++ Python/pythonrun.c | 5 +---- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 9bb1786c547..a0f7ad81bd5 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -927,6 +927,17 @@ class TracebackErrorLocationCaretTestBase: ] self.assertEqual(actual, expected) + def test_memory_error(self): + def f(): + raise MemoryError() + + actual = self.get_exception(f) + expected = ['Traceback (most recent call last):', + f' File "{__file__}", line {self.callable_line}, in get_exception', + ' callable()', + f' File "{__file__}", line {f.__code__.co_firstlineno + 1}, in f', + ' raise MemoryError()'] + self.assertEqual(actual, expected) @requires_debug_ranges() diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst b/Misc/NEWS.d/next/Core and Builtins/2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst new file mode 100644 index 00000000000..d70d45ebb93 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst @@ -0,0 +1,2 @@ +Correctly display the traceback for :exc:`MemoryError` exceptions using the +:mod:`traceback` module. Patch by Pablo Galindo diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 74994295a9b..b915c063d0b 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1031,6 +1031,7 @@ error: void _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) { + assert(value != NULL); assert(file != NULL && file != Py_None); if (PyExceptionInstance_Check(value) && tb != NULL && PyTraceBack_Check(tb)) { @@ -1047,10 +1048,6 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt; - if (!value || PyErr_GivenExceptionMatches(value, PyExc_MemoryError)) { - goto fallback; - } - // Try first with the stdlib traceback module PyObject *traceback_module = PyImport_ImportModule("traceback");