summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@microsoft.com>2023-05-05 00:00:07 (GMT)
committerGitHub <noreply@github.com>2023-05-05 00:00:07 (GMT)
commitce871fdc3a02e8441ad73b13f9fced308a9d9ad1 (patch)
tree2f0cda4ab28073d7e39bbdbeda3196002d87699d /Lib
parentfa86a77589a06661fcebb806d36f3a7450e2aecf (diff)
downloadcpython-ce871fdc3a02e8441ad73b13f9fced308a9d9ad1.zip
cpython-ce871fdc3a02e8441ad73b13f9fced308a9d9ad1.tar.gz
cpython-ce871fdc3a02e8441ad73b13f9fced308a9d9ad1.tar.bz2
GH-104142: Fix _Py_RefcntAdd to respect immortality (GH-104143)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_builtin.py43
1 files changed, 25 insertions, 18 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 04dd8ff..821710a 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -2372,24 +2372,31 @@ class ShutdownTest(unittest.TestCase):
@cpython_only
class ImmortalTests(unittest.TestCase):
- def test_immortal(self):
- none_refcount = sys.getrefcount(None)
- true_refcount = sys.getrefcount(True)
- false_refcount = sys.getrefcount(False)
- smallint_refcount = sys.getrefcount(100)
-
- # Assert that all of these immortal instances have large ref counts.
- self.assertGreater(none_refcount, 2 ** 15)
- self.assertGreater(true_refcount, 2 ** 15)
- self.assertGreater(false_refcount, 2 ** 15)
- self.assertGreater(smallint_refcount, 2 ** 15)
-
- # Confirm that the refcount doesn't change even with a new ref to them.
- l = [None, True, False, 100]
- self.assertEqual(sys.getrefcount(None), none_refcount)
- self.assertEqual(sys.getrefcount(True), true_refcount)
- self.assertEqual(sys.getrefcount(False), false_refcount)
- self.assertEqual(sys.getrefcount(100), smallint_refcount)
+
+ if sys.maxsize < (1 << 32):
+ IMMORTAL_REFCOUNT = (1 << 30) - 1
+ else:
+ IMMORTAL_REFCOUNT = (1 << 32) - 1
+
+ IMMORTALS = (None, True, False, Ellipsis, NotImplemented, *range(-5, 257))
+
+ def assert_immortal(self, immortal):
+ with self.subTest(immortal):
+ self.assertEqual(sys.getrefcount(immortal), self.IMMORTAL_REFCOUNT)
+
+ def test_immortals(self):
+ for immortal in self.IMMORTALS:
+ self.assert_immortal(immortal)
+
+ def test_list_repeat_respect_immortality(self):
+ refs = list(self.IMMORTALS) * 42
+ for immortal in self.IMMORTALS:
+ self.assert_immortal(immortal)
+
+ def test_tuple_repeat_respect_immortality(self):
+ refs = tuple(self.IMMORTALS) * 42
+ for immortal in self.IMMORTALS:
+ self.assert_immortal(immortal)
class TestType(unittest.TestCase):