]> granicus.if.org Git - python/commitdiff
bpo-30817: Fix PyErr_PrintEx() when no memory (#2526)
authorxdegaye <xdegaye@gmail.com>
Mon, 23 Oct 2017 16:08:41 +0000 (18:08 +0200)
committerGitHub <noreply@github.com>
Mon, 23 Oct 2017 16:08:41 +0000 (18:08 +0200)
Lib/test/test_exceptions.py
Misc/NEWS.d/next/Core and Builtins/2017-07-01-15-11-13.bpo-30817.j7ZvN_.rst [new file with mode: 0644]
Python/pythonrun.c

index ad4bc093841d49eedc62149bbd38db673e86fbc8..cef8d44ffaaa9156a72dde235cb962bd29ae84d2 100644 (file)
@@ -10,7 +10,7 @@ import errno
 
 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):
@@ -1097,6 +1097,23 @@ class ExceptionTests(unittest.TestCase):
                     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)
+
     def test_yield_in_nested_try_excepts(self):
         #Issue #25612
         class MainError(Exception):
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 (file)
index 0000000..f50aeef
--- /dev/null
@@ -0,0 +1,2 @@
+`PyErr_PrintEx()` clears now the ignored exception that may be raised by
+`_PySys_SetObjectId()`, for example when no memory.
index 25e2da44d95343f2338c3cf164d5c3a13f8a8922..17ec182b74cc3f7dfea17be4f4e312b454edf5f1 100644 (file)
@@ -630,9 +630,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) {