diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py index 246980aa74c2b8..091646cc09521c 100644 --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -1054,19 +1054,16 @@ def disabling_thread(): original_status = gc.isenabled() - with warnings.catch_warnings(record=True) as w, gc.ensure_disabled(): - inside_status_before_thread = gc.isenabled() - thread = threading.Thread(target=disabling_thread) - thread.start() - inside_status_after_thread = gc.isenabled() + with self.assertWarnsRegex(RuntimeWarning, "enabled while another"): + with gc.ensure_disabled(): + inside_status_before_thread = gc.isenabled() + thread = threading.Thread(target=disabling_thread) + thread.start() + inside_status_after_thread = gc.isenabled() after_status = gc.isenabled() thread.join() - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[-1].category, RuntimeWarning)) - self.assertEqual("Garbage collector enabled while another thread is " - "inside gc.ensure_enabled", str(w[-1].message)) self.assertEqual(original_status, True) self.assertEqual(inside_status_before_thread, False) self.assertEqual(thread_original_status, False) diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index c057d25a12b059..b37ccd4846934d 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -1068,8 +1068,13 @@ gc_enable_impl(PyObject *module) /*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/ { if(_PyRuntime.gc.disabled_threads){ - PyErr_WarnEx(PyExc_RuntimeWarning, "Garbage collector enabled while another " - "thread is inside gc.ensure_enabled",1); + if (PyErr_WarnEx( + PyExc_RuntimeWarning, + "Garbage collector enabled while another " + "thread is inside gc.ensure_enabled", 1) < 0) + { + return NULL; + } } _PyRuntime.gc.enabled = 1; Py_RETURN_NONE; @@ -1530,8 +1535,13 @@ ensure_disabled__enter__method(ensure_disabled_object *self, PyObject *args) PyGILState_STATE gstate = PyGILState_Ensure(); ++_PyRuntime.gc.disabled_threads; self->previous_gc_state = _PyRuntime.gc.enabled; - gc_disable_impl(NULL); + + PyObject *ret = gc_disable_impl(NULL); PyGILState_Release(gstate); + if (ret == NULL) { + return NULL; + } + Py_DECREF(ret); Py_RETURN_NONE; } @@ -1540,12 +1550,19 @@ ensure_disabled__exit__method(ensure_disabled_object *self, PyObject *args) { PyGILState_STATE gstate = PyGILState_Ensure(); --_PyRuntime.gc.disabled_threads; - if(self->previous_gc_state){ - gc_enable_impl(NULL); - }else{ - gc_disable_impl(NULL); + + PyObject *ret; + if (self->previous_gc_state) { + ret = gc_enable_impl(NULL); + } + else { + ret = gc_disable_impl(NULL); } PyGILState_Release(gstate); + if (ret == NULL) { + return NULL; + } + Py_DECREF(ret); Py_RETURN_NONE; } @@ -1650,8 +1667,11 @@ PyInit_gc(void) if (PyType_Ready(&gc_ensure_disabled_type) < 0) return NULL; - if (PyModule_AddObject(m, "ensure_disabled", (PyObject*) &gc_ensure_disabled_type) < 0) + if (PyModule_AddObject(m, "ensure_disabled", + (PyObject*) &gc_ensure_disabled_type) < 0) + { return NULL; + } #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NULL