From f1a7c637eda9c90a87cb52534629667f1d4b5f7a Mon Sep 17 00:00:00 2001 From: Lincoln Clarete Date: Mon, 30 Nov 2026 17:56:28 -0500 Subject: [PATCH] fix: Match the object header layout of free-threaded builds Free-threaded builds of CPython (3.13+, `Py_GIL_DISABLED') replace `ob_refcnt' with a split thread-local/shared reference count and a few more header fields, so `PyObject' is 32 bytes instead of 16. Our ctypes mirror hardcoded the regular layout, which shifted every `PyTypeObject' slot we read or write by 16 bytes. Among other things, that made the static built-in check read garbage instead of `tp_flags', so `PyType_Modified' was still called on `int' and friends, aborting on builds with assertions enabled. On release builds, cursing dunder methods silently wrote into the wrong slots. Pick the header layout based on `Py_GIL_DISABLED', add a test that checks our struct lines up with the running interpreter, and run the debug test job on a free-threaded build as well. Reported in #88. --- .github/workflows/ci.yml | 2 +- forbiddenfruit/__init__.py | 29 +++++++++++++++++++++++++---- tests/unit/test_forbidden_fruit.py | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/forbiddenfruit/__init__.py b/forbiddenfruit/__init__.py index e128b5f..6d101e1 100644 --- a/forbiddenfruit/__init__.py +++ b/forbiddenfruit/__init__.py @@ -182,10 +182,31 @@ class PyAsyncMethods(ctypes.Structure): pass -PyObject._fields_ = [ - ('ob_refcnt', Py_ssize_t), - ('ob_type', ctypes.POINTER(PyTypeObject)), -] +try: + import sysconfig + _Py_GIL_DISABLED = bool(sysconfig.get_config_var('Py_GIL_DISABLED')) +except ImportError: + _Py_GIL_DISABLED = False + + +if _Py_GIL_DISABLED: + # Free-threaded builds (3.13+) replace `ob_refcnt' with a split + # thread-local/shared reference count and a few more header fields, + # doubling the size of `PyObject'. See `struct _object' in object.h. + PyObject._fields_ = [ + ('ob_tid', ctypes.c_size_t), + ('ob_flags', ctypes.c_uint16), + ('ob_mutex', ctypes.c_uint8), + ('ob_gc_bits', ctypes.c_uint8), + ('ob_ref_local', ctypes.c_uint32), + ('ob_ref_shared', Py_ssize_t), + ('ob_type', ctypes.POINTER(PyTypeObject)), + ] +else: + PyObject._fields_ = [ + ('ob_refcnt', Py_ssize_t), + ('ob_type', ctypes.POINTER(PyTypeObject)), + ] PyTypeObject._fields_ = [ # varhead diff --git a/tests/unit/test_forbidden_fruit.py b/tests/unit/test_forbidden_fruit.py index d7495f3..c991e5e 100644 --- a/tests/unit/test_forbidden_fruit.py +++ b/tests/unit/test_forbidden_fruit.py @@ -70,6 +70,22 @@ def test_reversing_a_builtin(): assert 'stuff' not in dir(str) +def test_type_object_layout_matches_the_interpreter(): + # Given the ctypes mirror of `PyTypeObject' forbiddenfruit pokes at + from forbiddenfruit import PyObject, PyTypeObject + import ctypes + + # Then its object header has the size of the running interpreter's, + # which differs between regular and free-threaded builds + assert ctypes.sizeof(PyObject) == object.__basicsize__ + + # And the slots it reads line up with the real ones + for klass in (int, str, dict, type, ffruit.Dummy): + tyobj = PyTypeObject.from_address(id(klass)) + assert tyobj.tp_basicsize == klass.__basicsize__ + assert tyobj.tp_flags == klass.__flags__ + + def test_cursing_a_builtin_reaches_its_subclasses(): # Given that I have subclasses of a built-in, one of them also built-in class MyInt(int):