Skip to content

Commit b3c9faf

Browse files
authored
gh-110912: Correctly display tracebacks for MemoryError exceptions using the traceback module (#110921)
1 parent bad7a35 commit b3c9faf

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Lib/test/test_traceback.py

+11
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,17 @@ def f():
927927
]
928928
self.assertEqual(actual, expected)
929929

930+
def test_memory_error(self):
931+
def f():
932+
raise MemoryError()
933+
934+
actual = self.get_exception(f)
935+
expected = ['Traceback (most recent call last):',
936+
f' File "{__file__}", line {self.callable_line}, in get_exception',
937+
' callable()',
938+
f' File "{__file__}", line {f.__code__.co_firstlineno + 1}, in f',
939+
' raise MemoryError()']
940+
self.assertEqual(actual, expected)
930941

931942

932943
@requires_debug_ranges()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correctly display the traceback for :exc:`MemoryError` exceptions using the
2+
:mod:`traceback` module. Patch by Pablo Galindo

Python/pythonrun.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ print_exception_recursive(struct exception_print_context *ctx, PyObject *value)
10311031
void
10321032
_PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
10331033
{
1034+
assert(value != NULL);
10341035
assert(file != NULL && file != Py_None);
10351036
if (PyExceptionInstance_Check(value)
10361037
&& tb != NULL && PyTraceBack_Check(tb)) {
@@ -1047,10 +1048,6 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
10471048

10481049
int unhandled_keyboard_interrupt = _PyRuntime.signals.unhandled_keyboard_interrupt;
10491050

1050-
if (!value || PyErr_GivenExceptionMatches(value, PyExc_MemoryError)) {
1051-
goto fallback;
1052-
}
1053-
10541051
// Try first with the stdlib traceback module
10551052
PyObject *traceback_module = PyImport_ImportModule("traceback");
10561053

0 commit comments

Comments
 (0)