From eef5554ca5c15f4519be6d5e1dc154ca792876e5 Mon Sep 17 00:00:00 2001 From: Xavier de Gaye Date: Sat, 1 Jul 2017 15:41:50 +0200 Subject: [PATCH] bpo-30817: Fix PyErr_PrintEx() when no memory --- Lib/test/test_exceptions.py | 19 ++++++++++++++++++- .../2017-07-01-15-11-13.bpo-30817.j7ZvN_.rst | 2 ++ Python/pythonrun.c | 12 +++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-07-01-15-11-13.bpo-30817.j7ZvN_.rst diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 3378ceb701c0442..3372bc67dec392b 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -10,7 +10,7 @@ from test.support import (TESTFN, captured_stderr, check_impl_detail, check_warnings, cpython_only, gc_collect, run_unittest, - no_tracing, unlink, import_module) + no_tracing, unlink, import_module, script_helper) class NaiveException(Exception): def __init__(self, x): @@ -1069,6 +1069,23 @@ def test_unhandled(self): self.assertIn("test message", report) self.assertTrue(report.endswith("\n")) + @cpython_only + def test_memory_error_in_PyErr_PrintEx(self): + code = """if 1: + import _testcapi + class C(): pass + _testcapi.set_nomemory(0, %d) + C() + """ + + # Issue #30817: Abort in PyErr_PrintEx() when no memory. + # Span a large range of tests as the CPython code always evolves with + # changes that add or remove memory allocations. + for i in range(1, 20): + rc, out, err = script_helper.assert_python_failure("-c", code % i) + self.assertIn(rc, (1, 120)) + self.assertIn(b'MemoryError', err) + class ImportErrorTests(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-07-01-15-11-13.bpo-30817.j7ZvN_.rst b/Misc/NEWS.d/next/Core and Builtins/2017-07-01-15-11-13.bpo-30817.j7ZvN_.rst new file mode 100644 index 000000000000000..1e7bb8fdace2785 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-07-01-15-11-13.bpo-30817.j7ZvN_.rst @@ -0,0 +1,2 @@ +`PyErr_PrintEx()` does not ignore now when an exception is set after calling +`_PySys_SetObjectId()`. diff --git a/Python/pythonrun.c b/Python/pythonrun.c index f31b3ee5a5dcd15..4b44c6b6ef464aa 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -626,9 +626,15 @@ PyErr_PrintEx(int set_sys_last_vars) return; /* Now we know v != NULL too */ if (set_sys_last_vars) { - _PySys_SetObjectId(&PyId_last_type, exception); - _PySys_SetObjectId(&PyId_last_value, v); - _PySys_SetObjectId(&PyId_last_traceback, tb); + if (_PySys_SetObjectId(&PyId_last_type, exception) < 0) { + PyErr_Clear(); + } + if (_PySys_SetObjectId(&PyId_last_value, v) < 0) { + PyErr_Clear(); + } + if (_PySys_SetObjectId(&PyId_last_traceback, tb) < 0) { + PyErr_Clear(); + } } hook = _PySys_GetObjectId(&PyId_excepthook); if (hook) {