summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-08-02 16:11:44 (GMT)
committerGitHub <noreply@github.com>2024-08-02 16:11:44 (GMT)
commit4b63cd170e5dd840bffc80922f09f2d69932ff5c (patch)
treeffecccece47cd87b293893edda840c2b65295b99 /Lib/test
parent7aca84e557d0a6d242f322c493d53947a56bde91 (diff)
downloadcpython-4b63cd170e5dd840bffc80922f09f2d69932ff5c.zip
cpython-4b63cd170e5dd840bffc80922f09f2d69932ff5c.tar.gz
cpython-4b63cd170e5dd840bffc80922f09f2d69932ff5c.tar.bz2
gh-122527: Fix a crash on deallocation of `PyStructSequence` (GH-122577)
The `PyStructSequence` destructor would crash if it was deallocated after its type's dictionary was cleared by the GC, because it couldn't compute the "real size" of the instance. This could occur with relatively straightforward code in the free-threaded build or with a reference cycle involving the type in the default build, due to differing orders in which `tp_clear()` was called. Account for the non-sequence fields in `tp_basicsize` and use that, along with `Py_SIZE()`, to compute the "real" size of a `PyStructSequence` in the dealloc function. This avoids the accesses to the type's dictionary during dealloc, which were unsafe.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_structseq.py13
-rw-r--r--Lib/test/test_sys.py3
2 files changed, 15 insertions, 1 deletions
diff --git a/Lib/test/test_structseq.py b/Lib/test/test_structseq.py
index 6aec63e..d0bc0bd 100644
--- a/Lib/test/test_structseq.py
+++ b/Lib/test/test_structseq.py
@@ -2,8 +2,10 @@ import copy
import os
import pickle
import re
+import textwrap
import time
import unittest
+from test.support import script_helper
class StructSeqTest(unittest.TestCase):
@@ -342,6 +344,17 @@ class StructSeqTest(unittest.TestCase):
with self.assertRaisesRegex(TypeError, error_message):
copy.replace(r, st_mode=1, error=2)
+ def test_reference_cycle(self):
+ # gh-122527: Check that a structseq that's part of a reference cycle
+ # with its own type doesn't crash. Previously, if the type's dictionary
+ # was cleared first, the structseq instance would crash in the
+ # destructor.
+ script_helper.assert_python_ok("-c", textwrap.dedent(r"""
+ import time
+ t = time.gmtime()
+ type(t).refcyle = t
+ """))
+
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 7e4bc98..709355e 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1823,7 +1823,8 @@ class SizeofTest(unittest.TestCase):
# symtable entry
# XXX
# sys.flags
- check(sys.flags, vsize('') + self.P * len(sys.flags))
+ # FIXME: The +1 will not be necessary once gh-122575 is fixed
+ check(sys.flags, vsize('') + self.P * (1 + len(sys.flags)))
def test_asyncgen_hooks(self):
old = sys.get_asyncgen_hooks()