gh-131586: Avoid refcount contention in some "special" calls (#131588)

In the free threaded build, the `_PyObject_LookupSpecial()` call can lead to
reference count contention on the returned function object becuase it
doesn't use stackrefs. Refactor some of the callers to use
`_PyObject_MaybeCallSpecialNoArgs`, which uses stackrefs internally.

This fixes the scaling bottleneck in the "lookup_special" microbenchmark
in `ftscalingbench.py`. However, the are still some uses of
`_PyObject_LookupSpecial()` that need to be addressed in future PRs.
This commit is contained in:
Sam Gross 2025-03-26 14:38:47 -04:00 committed by GitHub
parent 3d4ac1a2c2
commit 67fbfb42bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 450 additions and 374 deletions

View file

@ -383,6 +383,10 @@ class BoolTest(unittest.TestCase):
__bool__ = None
self.assertRaises(TypeError, bool, B())
class C:
__len__ = None
self.assertRaises(TypeError, bool, C())
def test_real_and_imag(self):
self.assertEqual(True.real, 1)
self.assertEqual(True.imag, 0)

View file

@ -1746,6 +1746,11 @@ class BuiltinTest(ComplexesAreIdenticalMixin, unittest.TestCase):
a[0] = a
self.assertEqual(repr(a), '{0: {...}}')
def test_repr_blocked(self):
class C:
__repr__ = None
self.assertRaises(TypeError, repr, C())
def test_round(self):
self.assertEqual(round(0.0), 0.0)
self.assertEqual(type(round(0.0)), int)

View file

@ -573,6 +573,8 @@ class MathTests(unittest.TestCase):
#self.assertEqual(math.ceil(NINF), NINF)
#self.assertTrue(math.isnan(math.floor(NAN)))
class TestFloorIsNone(float):
__floor__ = None
class TestFloor:
def __floor__(self):
return 42
@ -588,6 +590,7 @@ class MathTests(unittest.TestCase):
self.assertEqual(math.floor(FloatLike(41.9)), 41)
self.assertRaises(TypeError, math.floor, TestNoFloor())
self.assertRaises(ValueError, math.floor, TestBadFloor())
self.assertRaises(TypeError, math.floor, TestFloorIsNone(3.5))
t = TestNoFloor()
t.__floor__ = lambda *args: args