Skip to content

Commit 5b92c8b

Browse files
committed
Move PyErr_WriteUnraisable call
1 parent ef00dde commit 5b92c8b

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

mypyc/codegen/emitclass.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -842,15 +842,20 @@ def generate_dealloc_for_class(
842842
emitter.emit_line("static void")
843843
emitter.emit_line(f"{dealloc_func_name}({cl.struct_name(emitter.names)} *self)")
844844
emitter.emit_line("{")
845-
emitter.emit_line("int finalize_error = 0;")
846-
emitter.emit_line("PyObject *type, *value, *traceback;")
847-
emitter.emit_line("PyErr_Fetch(&type, &value, &traceback);")
848845
if has_tp_finalize:
849846
emitter.emit_line("if (!PyObject_GC_IsFinalized((PyObject *)self)) {")
847+
emitter.emit_line("PyObject *type, *value, *traceback;")
848+
emitter.emit_line("PyErr_Fetch(&type, &value, &traceback);")
850849
emitter.emit_line("PyObject_CallFinalizerFromDealloc((PyObject *)self);")
850+
# CPython interpreter uses PyErr_WriteUnraisable: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_WriteUnraisable
851+
# However, the message is slightly different due to the way mypyc compiles classes.
852+
# CPython interpreter prints: Exception ignored in: <function F.__del__ at 0x100aed940>
853+
# mypyc prints: Exception ignored in: <slot wrapper '__del__' of 'F' objects>
854+
emitter.emit_line("if (PyErr_Occurred() != NULL) {")
855+
emitter.emit_line("PyErr_WriteUnraisable((PyObject *)self);")
856+
emitter.emit_line("}")
857+
emitter.emit_line("PyErr_Restore(type, value, traceback);")
851858
emitter.emit_line("}")
852-
emitter.emit_line("if (PyErr_Occurred() != NULL) finalize_error = 1;")
853-
emitter.emit_line("PyErr_Restore(type, value, traceback);")
854859
emitter.emit_line("PyObject_GC_UnTrack(self);")
855860
if cl.reuse_freed_instance:
856861
emit_reuse_dealloc(cl, emitter)
@@ -859,13 +864,6 @@ def generate_dealloc_for_class(
859864
emitter.emit_line(f"{clear_func_name}(self);")
860865
emitter.emit_line("Py_TYPE(self)->tp_free((PyObject *)self);")
861866
emitter.emit_line("CPy_TRASHCAN_END(self)")
862-
# # CPython interpreter uses PyErr_WriteUnraisable: https://docs.python.org/3/c-api/exceptions.html#c.PyErr_WriteUnraisable
863-
# # However, the message is slightly different due to the way mypyc compiles classes.
864-
# # CPython interpreter prints: Exception ignored in: <function F.__del__ at 0x100aed940>
865-
# # mypyc prints: Exception ignored in: <slot wrapper '__del__' of 'F' objects>
866-
emitter.emit_line("if (finalize_error == 1) {")
867-
emitter.emit_line("PyErr_WriteUnraisable((PyObject *)self);")
868-
emitter.emit_line("}")
869867
emitter.emit_line("}")
870868

871869

0 commit comments

Comments
 (0)