Skip to content

Commit 54ca559

Browse files
authored
gh-134248 test_getallocatedblocks pre-check to ignore immortalized strings (#134871)
When sanity checking against gettotalrefcount(), we exclude the blocks for immortalized strings since their references are not tracked/reported. This now matches refleak.py's book-keeping using the same functions.
1 parent 3612d8f commit 54ca559

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

Lib/test/test_sys.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,23 +1135,12 @@ def test_getallocatedblocks(self):
11351135
b = sys.getallocatedblocks()
11361136
self.assertLessEqual(b, a)
11371137
try:
1138-
# While we could imagine a Python session where the number of
1139-
# multiple buffer objects would exceed the sharing of references,
1140-
# it is unlikely to happen in a normal test run.
1141-
#
1142-
# In free-threaded builds each code object owns an array of
1143-
# pointers to copies of the bytecode. When the number of
1144-
# code objects is a large fraction of the total number of
1145-
# references, this can cause the total number of allocated
1146-
# blocks to exceed the total number of references.
1147-
#
1148-
# For some reason, iOS seems to trigger the "unlikely to happen"
1149-
# case reliably under CI conditions. It's not clear why; but as
1150-
# this test is checking the behavior of getallocatedblock()
1151-
# under garbage collection, we can skip this pre-condition check
1152-
# for now. See GH-130384.
1153-
if not support.Py_GIL_DISABLED and not support.is_apple_mobile:
1154-
self.assertLess(a, sys.gettotalrefcount())
1138+
# The reported blocks will include immortalized strings, but the
1139+
# total ref count will not. This will sanity check that among all
1140+
# other objects (those eligible for garbage collection) there
1141+
# are more references being tracked than allocated blocks.
1142+
interned_immortal = sys.getunicodeinternedsize(_only_immortal=True)
1143+
self.assertLess(a - interned_immortal, sys.gettotalrefcount())
11551144
except AttributeError:
11561145
# gettotalrefcount() not available
11571146
pass

0 commit comments

Comments
 (0)