-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-31356: Fix multiple errors in gc.ensure_disabled() #5462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1068,8 +1068,13 @@ gc_enable_impl(PyObject *module) | |
/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/ | ||
{ | ||
if(_PyRuntime.gc.disabled_threads){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix the style of this line too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix the style of this line too. |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @serhiy-storchaka Do you understand this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here and below, the
blocks should be replaced with
The GIL must be held for the decref. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this is a Python API! So we release the gil inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't review the initial changes and know nothing about the correctness of other code. I'll check it carefully after beta1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this is just plain wrong. |
||
if (ret == NULL) { | ||
return NULL; | ||
} | ||
Py_DECREF(ret); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just return |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be better to chain exceptions, but this is an additional non-trivial code. We can do this in beta2. |
||
} | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What line emits a warning? It would be better to surround with assertWarnsRegex() only that line. Or it can be emitted by arbitrary line in the block?
Note that warnings emitted in other thread are not caught here.