Skip to content

Commit 34e4862

Browse files
fix: set Py_TPFLAGS_MANAGED_WEAKREF flag on python>=3.12
1 parent 7da4e53 commit 34e4862

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

mypyc/codegen/emitclass.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ def emit_line() -> None:
293293
"};",
294294
)
295295
if emitter.capi_version < (3, 12):
296+
# versions >= 3.12 do not define tp_weaklistoffset, but set Py_TPFLAGS_MANAGED_WEAKREF flag instead
296297
fields["tp_weaklistoffset"] = base_size
297298
else:
298299
fields["tp_basicsize"] = base_size
@@ -354,6 +355,9 @@ def emit_line() -> None:
354355
fields["tp_call"] = "PyVectorcall_Call"
355356
if has_managed_dict(cl, emitter):
356357
flags.append("Py_TPFLAGS_MANAGED_DICT")
358+
if cl.supports_weakref and emitter.capi_version >= (3, 12):
359+
flags.append("Py_TPFLAGS_MANAGED_WEAKREF")
360+
357361
fields["tp_flags"] = " | ".join(flags)
358362

359363
emitter.emit_line(f"static PyTypeObject {emitter.type_struct_name(cl)}_template_ = {{")

0 commit comments

Comments
 (0)