-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[mypyc] Fix object finalization #19749
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 2 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 |
|---|---|---|
|
|
@@ -844,7 +844,17 @@ def generate_dealloc_for_class( | |
| emitter.emit_line("{") | ||
| if has_tp_finalize: | ||
| emitter.emit_line("if (!PyObject_GC_IsFinalized((PyObject *)self)) {") | ||
| emitter.emit_line("Py_TYPE(self)->tp_finalize((PyObject *)self);") | ||
| emitter.emit_line("PyObject *type, *value, *traceback;") | ||
| emitter.emit_line("PyErr_Fetch(&type, &value, &traceback);") | ||
| emitter.emit_line("PyObject_CallFinalizerFromDealloc((PyObject *)self);") | ||
cdce8p marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # CPython interpreter uses PyErr_WriteUnraisable: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_WriteUnraisable | ||
| # However, the message is slightly different due to the way mypyc compiles classes. | ||
| # CPython interpreter prints: Exception ignored in: <function F.__del__ at 0x100aed940> | ||
| # mypyc prints: Exception ignored in: <slot wrapper '__del__' of 'F' objects> | ||
| emitter.emit_line("if (PyErr_Occurred() != NULL) {") | ||
| emitter.emit_line("PyErr_WriteUnraisable((PyObject *)self);") | ||
| emitter.emit_line("}") | ||
| emitter.emit_line("PyErr_Restore(type, value, traceback);") | ||
|
Member
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 would move this code to the
Collaborator
Author
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. This doesn't seem to work unfortunately. If I move it, other tests start to fail. E.g. So I think
Member
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. Hm, that's not making sense to me. Is the
Collaborator
Author
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. It is, but some cleanup functions from mypyc seem to require that the exception is set properly.
Member
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. Ok, that's a little messy. As long as they don't overwrite the exception, I guess it's fine. |
||
| emitter.emit_line("}") | ||
| emitter.emit_line("PyObject_GC_UnTrack(self);") | ||
| if cl.reuse_freed_instance: | ||
|
|
@@ -884,30 +894,13 @@ def generate_finalize_for_class( | |
| emitter.emit_line("static void") | ||
| emitter.emit_line(f"{finalize_func_name}(PyObject *self)") | ||
| emitter.emit_line("{") | ||
| emitter.emit_line("PyObject *type, *value, *traceback;") | ||
| emitter.emit_line("PyErr_Fetch(&type, &value, &traceback);") | ||
| emitter.emit_line( | ||
| "{}{}{}(self);".format( | ||
| emitter.get_group_prefix(del_method.decl), | ||
| NATIVE_PREFIX, | ||
| del_method.cname(emitter.names), | ||
| ) | ||
| ) | ||
| emitter.emit_line("if (PyErr_Occurred() != NULL) {") | ||
| emitter.emit_line('PyObject *del_str = PyUnicode_FromString("__del__");') | ||
| emitter.emit_line( | ||
| "PyObject *del_method = (del_str == NULL) ? NULL : _PyType_Lookup(Py_TYPE(self), del_str);" | ||
| ) | ||
| # CPython interpreter uses PyErr_WriteUnraisable: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_WriteUnraisable | ||
| # However, the message is slightly different due to the way mypyc compiles classes. | ||
| # CPython interpreter prints: Exception ignored in: <function F.__del__ at 0x100aed940> | ||
| # mypyc prints: Exception ignored in: <slot wrapper '__del__' of 'F' objects> | ||
| emitter.emit_line("PyErr_WriteUnraisable(del_method);") | ||
| emitter.emit_line("Py_XDECREF(del_method);") | ||
| emitter.emit_line("Py_XDECREF(del_str);") | ||
| emitter.emit_line("}") | ||
| # PyErr_Restore also clears exception raised in __del__. | ||
| emitter.emit_line("PyErr_Restore(type, value, traceback);") | ||
| emitter.emit_line("}") | ||
|
|
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.