Skip to content

Commit d7c67e0

Browse files
[3.12] gh-117482: Expand Tests for Slot Wrappers of Inherited Slots of Static Builtin Types (gh-122197)
(cherry picked from commit 33d32fa, AKA gh-122192)
1 parent cde9a26 commit d7c67e0

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

Lib/test/test_embed.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from collections import namedtuple
77
import contextlib
8+
import io
89
import json
910
import os
1011
import os.path
@@ -389,6 +390,33 @@ def test_ucnhash_capi_reset(self):
389390
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
390391
self.assertEqual(out, '9\n' * INIT_LOOPS)
391392

393+
def test_static_types_inherited_slots(self):
394+
slots = []
395+
script = ['import sys']
396+
from test.test_types import iter_builtin_types, iter_own_slot_wrappers
397+
for cls in iter_builtin_types():
398+
for slot in iter_own_slot_wrappers(cls):
399+
slots.append((cls, slot))
400+
attr = f'{cls.__name__}.{slot}'
401+
script.append(f'print("{attr}:", {attr}, file=sys.stderr)')
402+
script.append('')
403+
script = os.linesep.join(script)
404+
405+
with contextlib.redirect_stderr(io.StringIO()) as stderr:
406+
exec(script)
407+
expected = stderr.getvalue().splitlines()
408+
409+
out, err = self.run_embedded_interpreter("test_repeated_init_exec", script)
410+
results = err.split('--- Loop #')[1:]
411+
results = [res.rpartition(' ---\n')[-1] for res in results]
412+
413+
self.maxDiff = None
414+
for i, result in enumerate(results, start=1):
415+
with self.subTest(loop=i):
416+
self.assertEqual(result.splitlines(), expected)
417+
self.assertEqual(out, '')
418+
419+
392420
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
393421
maxDiff = 4096
394422
UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape')

Lib/test/test_types.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Python test set -- part 6, built-in types
22

33
from test.support import run_with_locale, cpython_only, MISSING_C_DOCSTRINGS
4+
from test.test_import import no_rerun
45
import collections.abc
56
from collections import namedtuple
67
import copy
@@ -28,6 +29,26 @@ def clear_typing_caches():
2829
f()
2930

3031

32+
def iter_builtin_types():
33+
for obj in __builtins__.values():
34+
if not isinstance(obj, type):
35+
continue
36+
cls = obj
37+
if cls.__module__ != 'builtins':
38+
continue
39+
yield cls
40+
41+
42+
@cpython_only
43+
def iter_own_slot_wrappers(cls):
44+
for name, value in vars(cls).items():
45+
if not name.startswith('__') or not name.endswith('__'):
46+
continue
47+
if 'slot wrapper' not in str(value):
48+
continue
49+
yield name
50+
51+
3152
class TypesTests(unittest.TestCase):
3253

3354
def test_truth_values(self):
@@ -2264,27 +2285,39 @@ def setUpClass(cls):
22642285
raise unittest.SkipTest('subinterpreters required')
22652286

22662287
@cpython_only
2288+
@no_rerun('channels (and queues) might have a refleak; see gh-122199')
22672289
def test_slot_wrappers(self):
22682290
rch, sch = interpreters.create_channel()
22692291

2270-
# For now it's sufficient to check int.__str__.
2271-
# See https://github.com/python/cpython/issues/117482
2272-
# and https://github.com/python/cpython/pull/117660.
2273-
script = textwrap.dedent(f'''
2274-
text = repr(int.__str__)
2275-
sch = interpreters.SendChannel({sch.id})
2276-
sch.send_nowait(text)
2277-
''')
2292+
slots = []
2293+
script = ''
2294+
for cls in iter_builtin_types():
2295+
for slot in iter_own_slot_wrappers(cls):
2296+
slots.append((cls, slot))
2297+
attr = f'{cls.__name__}.{slot}'
2298+
script += textwrap.dedent(f"""
2299+
sch.send_nowait('{attr}: ' + repr({attr}))
2300+
""")
22782301

22792302
exec(script)
2280-
expected = rch.recv()
2303+
all_expected = []
2304+
for cls, slot in slots:
2305+
result = rch.recv()
2306+
assert result.startswith(f'{cls.__name__}.{slot}: '), (cls, slot, result)
2307+
all_expected.append(result)
22812308

22822309
interp = interpreters.create()
2283-
interp.run('from test.support import interpreters')
2310+
interp.run(textwrap.dedent(f"""
2311+
from test.support import interpreters
2312+
sch = interpreters.SendChannel({sch.id})
2313+
"""))
22842314
interp.run(script)
2285-
results = rch.recv()
22862315

2287-
self.assertEqual(results, expected)
2316+
for i, _ in enumerate(slots):
2317+
with self.subTest(cls=cls, slot=slot):
2318+
expected = all_expected[i]
2319+
result = rch.recv()
2320+
self.assertEqual(result, expected)
22882321

22892322

22902323
if __name__ == '__main__':

0 commit comments

Comments
 (0)