From b8f6ffd6dfed5a36dbe884a4f8c330360b3fba8f Mon Sep 17 00:00:00 2001 From: Lincoln Clarete Date: Mon, 30 Nov 2026 10:56:03 -0500 Subject: [PATCH 1/3] fix: Don't call PyType_Modified on static built-in types From CPython 3.13 on, `type_modified_unlocked' asserts it's never called on static built-in types (int, str, dict, ...), so cursing any of them aborts the interpreter on builds with assertions enabled. For those types, invalidate the caches by hand instead: clear `tp_version_tag' on all subclasses first and then on the type itself, which is what PyType_Modified would have done. Lookups will then miss the caches and assign fresh version tags as needed. Reported in #78. --- forbiddenfruit/__init__.py | 34 ++++++++++++++++++++++++++---- tests/unit/test_forbidden_fruit.py | 22 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/forbiddenfruit/__init__.py b/forbiddenfruit/__init__.py index 217cc04..e128b5f 100644 --- a/forbiddenfruit/__init__.py +++ b/forbiddenfruit/__init__.py @@ -257,6 +257,32 @@ class PyAsyncMethods(ctypes.Structure): } +# Flag CPython sets on built-in types that are statically allocated +# (int, str, dict, ...). From 3.13 on, `PyType_Modified' asserts it's +# never called on them, see `type_modified_unlocked' in typeobject.c. +_Py_TPFLAGS_STATIC_BUILTIN = 1 << 1 + + +def _type_modified(klass): + """Invalidate cached attribute lookups for `klass' and its subclasses + + Mirrors what `PyType_Modified' does, without tripping the + assertion it has for static built-in types on CPython >= 3.13. + """ + tyobj = PyTypeObject.from_address(id(klass)) + if sys.version_info < (3, 13) or \ + not (tyobj.tp_flags or 0) & _Py_TPFLAGS_STATIC_BUILTIN: + ctypes.pythonapi.PyType_Modified(ctypes.py_object(klass)) + return + if tyobj.tp_version_tag == 0: + return + # a type's tag must be cleared after all of its subclasses' tags + for subclass in type.__subclasses__(klass): + _type_modified(subclass) + # 0 isn't a valid tag, so the next lookup assigns a fresh one + tyobj.tp_version_tag = 0 + + def patchable_builtin(klass): refs = gc.get_referents(klass.__dict__) assert len(refs) == 1 @@ -420,7 +446,7 @@ def wrapper(*args, **kwargs): if klass not in tp_vectorcall_dict: tp_vectorcall_dict[klass] = tyobj.tp_vectorcall tyobj.tp_vectorcall = None - ctypes.pythonapi.PyType_Modified(ctypes.py_object(klass)) + _type_modified(klass) def _revert_special(klass, attr): tp_as_name, impl_method = override_dict[attr] @@ -449,7 +475,7 @@ def _revert_special(klass, attr): if attr == '__new__' and klass in tp_vectorcall_dict: # restore the fast-path constructor we disabled while cursing __new__ tyobj.tp_vectorcall = tp_vectorcall_dict.pop(klass) - ctypes.pythonapi.PyType_Modified(ctypes.py_object(klass)) + _type_modified(klass) def curse(klass, attr, value, hide_from_dir=False): @@ -503,7 +529,7 @@ def curse(klass, attr, value, hide_from_dir=False): except AttributeError: pass - ctypes.pythonapi.PyType_Modified(ctypes.py_object(klass)) + _type_modified(klass) if hide_from_dir: __hidden_elements__[klass.__name__].append(attr) @@ -538,7 +564,7 @@ def reverse(klass, attr): dikt = patchable_builtin(klass) del dikt[attr] - ctypes.pythonapi.PyType_Modified(ctypes.py_object(klass)) + _type_modified(klass) def curses(klass, name): diff --git a/tests/unit/test_forbidden_fruit.py b/tests/unit/test_forbidden_fruit.py index 3bb1275..d7495f3 100644 --- a/tests/unit/test_forbidden_fruit.py +++ b/tests/unit/test_forbidden_fruit.py @@ -70,6 +70,28 @@ def test_reversing_a_builtin(): assert 'stuff' not in dir(str) +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): + pass + + # And that looking an attribute up on them already failed once, so + # the miss may be sitting in the type caches + for obj in (1, True, MyInt(3)): + assert not hasattr(obj, 'hot_stuff') + + # When I curse the parent class + curse(int, 'hot_stuff', property(lambda s: 'hot')) + + # Then I see the new attribute on all of them + assert (1).hot_stuff == True.hot_stuff == MyInt(3).hot_stuff == 'hot' + + # And when I bless it, it's gone from all of them again + reverse(int, 'hot_stuff') + for obj in (1, True, MyInt(3)): + assert not hasattr(obj, 'hot_stuff') + + def test_dir_filtering(): # Given that I curse the `str` built-in asking the curse to hide it from # the built-in `dir()` function