Skip to content

Commit

Permalink
Clarify construction of deferred object
Browse files Browse the repository at this point in the history
  • Loading branch information
mpage committed Dec 24, 2024
1 parent b6ae487 commit 11a351d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,16 @@ def instantiate():
instantiate()


def make_deferred_ref_count_obj():
"""Create an object that uses deferred reference counting.
Only objects that use deferred refence counting may be stored in inline
caches in free-threaded builds. This constructs a new class named Foo,
which uses deferred reference counting.
"""
return type("Foo", (object,), {})


@threading_helper.requires_working_threading()
class TestRacesDoNotCrash(TestBase):
# Careful with these. Bigger numbers have a higher chance of catching bugs,
Expand Down Expand Up @@ -718,9 +728,7 @@ def write(items):
def test_load_attr_class(self):
def get_items():
class C:
# a must be set to an instance that uses deferred reference
# counting in free-threaded builds
a = type("Foo", (object,), {})
a = make_deferred_ref_count_obj()

items = []
for _ in range(self.ITEMS):
Expand All @@ -741,7 +749,7 @@ def write(items):
del item.a
except AttributeError:
pass
item.a = type("Foo", (object,), {})
item.a = make_deferred_ref_count_obj()

opname = "LOAD_ATTR_CLASS"
self.assert_races_do_not_crash(opname, get_items, read, write)
Expand All @@ -753,9 +761,7 @@ class Meta(type):
pass

class C(metaclass=Meta):
# a must be set to an instance that uses deferred reference
# counting in free-threaded builds
a = type("Foo", (object,), {})
a = make_deferred_ref_count_obj()

items = []
for _ in range(self.ITEMS):
Expand All @@ -776,7 +782,7 @@ def write(items):
del item.a
except AttributeError:
pass
item.a = type("Foo", (object,), {})
item.a = make_deferred_ref_count_obj()

opname = "LOAD_ATTR_CLASS_WITH_METACLASS_CHECK"
self.assert_races_do_not_crash(opname, get_items, read, write)
Expand Down

0 comments on commit 11a351d

Please sign in to comment.