]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-104142: Fix _Py_RefcntAdd to respect immortality (GH-104143)
authorBrandt Bucher <brandtbucher@microsoft.com>
Fri, 5 May 2023 00:00:07 +0000 (17:00 -0700)
committerGitHub <noreply@github.com>
Fri, 5 May 2023 00:00:07 +0000 (17:00 -0700)
Include/internal/pycore_object.h
Lib/test/test_builtin.py
Misc/NEWS.d/next/Core and Builtins/2023-05-02-18-29-49.gh-issue-104142._5Et6I.rst [new file with mode: 0644]

index b9e700ea280cbf871afb60f9bdcf15b0439bdcc5..500b3eece68055f9b41d17f6df52055146ab9f1f 100644 (file)
@@ -58,6 +58,9 @@ extern void _Py_DecRefTotal(PyInterpreterState *);
 // Increment reference count by n
 static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
 {
+    if (_Py_IsImmortal(op)) {
+        return;
+    }
 #ifdef Py_REF_DEBUG
     _Py_AddRefTotal(_PyInterpreterState_GET(), n);
 #endif
index 04dd8ff3070c99079c56af330d9a023dc041233b..821710a7fa32869b706dec15fff33b6f462cc580 100644 (file)
@@ -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):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-02-18-29-49.gh-issue-104142._5Et6I.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-02-18-29-49.gh-issue-104142._5Et6I.rst
new file mode 100644 (file)
index 0000000..6a19ae8
--- /dev/null
@@ -0,0 +1,2 @@
+Fix an issue where :class:`list` or :class:`tuple` repetition could fail to
+respect :pep:`683`.