From 42796e64728ce3ef0f9b0a68d0948269a127d732 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Mon, 16 Oct 2023 12:13:11 +0100 Subject: [PATCH 1/6] gh-110912: Correctly display tracebacks for MemoryError exceptions using the traceback module --- Lib/test/test_traceback.py | 16 +++++++++++++--- ...023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst | 2 ++ Python/pythonrun.c | 2 +- 3 files changed, 16 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 9bb1786c5472f5..1496a3410aaaa4 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -926,8 +926,18 @@ def f(): " ^^^^", ] 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() class PurePythonTracebackErrorCaretTests( @@ -2803,7 +2813,7 @@ def f(): ' x = 12', 'ZeroDivisionError: division by zero', '']) - + class TestTracebackException_ExceptionGroups(unittest.TestCase): def setUp(self): 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 00000000000000..42348c314aaee6 --- /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:`traceeback` module. Patch by Pablo Galindo diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 74994295a9b23a..8d49c011a3f882 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1047,7 +1047,7 @@ _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)) { + if (!value) { goto fallback; } From 9dc30dfe4a10324ded198d14bf4bbd7912c35d49 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Mon, 16 Oct 2023 12:15:51 +0100 Subject: [PATCH 2/6] Update Lib/test/test_traceback.py --- Lib/test/test_traceback.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 1496a3410aaaa4..af8b0ed3b113be 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -2813,7 +2813,6 @@ def f(): ' x = 12', 'ZeroDivisionError: division by zero', '']) - class TestTracebackException_ExceptionGroups(unittest.TestCase): def setUp(self): From f16ce2959bced3a892bb6e4ca73684515552323f Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Mon, 16 Oct 2023 12:18:15 +0100 Subject: [PATCH 3/6] fixup! Update Lib/test/test_traceback.py --- Lib/test/test_traceback.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index af8b0ed3b113be..2e25ed58573e9d 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -926,11 +926,11 @@ def f(): " ^^^^", ] 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', From ad992316d2f1348ea550bc453b10faed3f80d333 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Mon, 16 Oct 2023 13:52:32 +0200 Subject: [PATCH 4/6] Update 2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst --- .../2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 42348c314aaee6..d70d45ebb931ea 100644 --- 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 @@ -1,2 +1,2 @@ Correctly display the traceback for :exc:`MemoryError` exceptions using the -:mod:`traceeback` module. Patch by Pablo Galindo +:mod:`traceback` module. Patch by Pablo Galindo From fe9f9ee59979eb5f46f688c9c7ac4aeaf99377ce Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Mon, 16 Oct 2023 13:56:50 +0100 Subject: [PATCH 5/6] fixup! Update 2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst --- Lib/test/test_traceback.py | 5 +++-- Python/pythonrun.c | 5 +---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 2e25ed58573e9d..2d7e6eeb2f65c3 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -933,12 +933,13 @@ def f(): actual = self.get_exception(f) expected = ['Traceback (most recent call last):', - f' File \"{__file__}\", line {self.callable_line}, in get_exception', + f' File "{__file__}", line {self.callable_line}, in get_exception', ' callable()', - f' File \"{__file__}\", line {f.__code__.co_firstlineno + 1}, in f', + f' File "{__file__}", line {f.__code__.co_firstlineno + 1}, in f', ' raise MemoryError()'] self.assertEqual(actual, expected) + @requires_debug_ranges() class PurePythonTracebackErrorCaretTests( PurePythonExceptionFormattingMixin, diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 8d49c011a3f882..b915c063d0b456 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1031,6 +1031,7 @@ print_exception_recursive(struct exception_print_context *ctx, PyObject *value) 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) { - goto fallback; - } - // Try first with the stdlib traceback module PyObject *traceback_module = PyImport_ImportModule("traceback"); From 38ea7f6337528f4a38dd12246d50f8c590a6a672 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Mon, 16 Oct 2023 14:01:37 +0100 Subject: [PATCH 6/6] fixup! fixup! Update 2023-10-16-12-12-48.gh-issue-110912.uEJGi_.rst --- Lib/test/test_traceback.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 2d7e6eeb2f65c3..a0f7ad81bd540d 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -2815,6 +2815,7 @@ def f(): 'ZeroDivisionError: division by zero', '']) + class TestTracebackException_ExceptionGroups(unittest.TestCase): def setUp(self): super().setUp()