Skip to content

Commit 3237088

Browse files
ericsnowcurrentlypull[bot]
authored andcommitted
gh-117482: Expand Tests for Slot Wrappers of Inherited Slots of Static Builtin Types (gh-122192)
1 parent e399970 commit 3237088

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

Lib/test/test_embed.py

+27
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
@@ -415,6 +416,32 @@ def test_datetime_reset_strptime(self):
415416
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
416417
self.assertEqual(out, '20000101\n' * INIT_LOOPS)
417418

419+
def test_static_types_inherited_slots(self):
420+
slots = []
421+
script = ['import sys']
422+
from test.test_types import iter_builtin_types, iter_own_slot_wrappers
423+
for cls in iter_builtin_types():
424+
for slot in iter_own_slot_wrappers(cls):
425+
slots.append((cls, slot))
426+
attr = f'{cls.__name__}.{slot}'
427+
script.append(f'print("{attr}:", {attr}, file=sys.stderr)')
428+
script.append('')
429+
script = os.linesep.join(script)
430+
431+
with contextlib.redirect_stderr(io.StringIO()) as stderr:
432+
exec(script)
433+
expected = stderr.getvalue().splitlines()
434+
435+
out, err = self.run_embedded_interpreter("test_repeated_init_exec", script)
436+
results = err.split('--- Loop #')[1:]
437+
results = [res.rpartition(' ---\n')[-1] for res in results]
438+
439+
self.maxDiff = None
440+
for i, result in enumerate(results, start=1):
441+
with self.subTest(loop=i):
442+
self.assertEqual(result.splitlines(), expected)
443+
self.assertEqual(out, '')
444+
418445

419446
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
420447
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

Lib/test/test_types.py

+39-10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ def clear_typing_caches():
2929
f()
3030

3131

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+
3252
class TypesTests(unittest.TestCase):
3353

3454
def test_truth_values(self):
@@ -2362,24 +2382,33 @@ def setUpClass(cls):
23622382
def test_slot_wrappers(self):
23632383
rch, sch = interpreters.channels.create()
23642384

2365-
# For now it's sufficient to check int.__str__.
2366-
# See https://github.com/python/cpython/issues/117482
2367-
# and https://github.com/python/cpython/pull/117660.
2368-
script = textwrap.dedent('''
2369-
text = repr(int.__str__)
2370-
sch.send_nowait(text)
2371-
''')
2385+
slots = []
2386+
script = ''
2387+
for cls in iter_builtin_types():
2388+
for slot in iter_own_slot_wrappers(cls):
2389+
slots.append((cls, slot))
2390+
script += textwrap.dedent(f"""
2391+
text = repr({cls.__name__}.{slot})
2392+
sch.send_nowait(({cls.__name__!r}, {slot!r}, text))
2393+
""")
23722394

23732395
exec(script)
2374-
expected = rch.recv()
2396+
all_expected = []
2397+
for cls, slot in slots:
2398+
result = rch.recv()
2399+
assert result == (cls.__name__, slot, result[2]), (cls, slot, result)
2400+
all_expected.append(result)
23752401

23762402
interp = interpreters.create()
23772403
interp.exec('from test.support import interpreters')
23782404
interp.prepare_main(sch=sch)
23792405
interp.exec(script)
2380-
results = rch.recv()
23812406

2382-
self.assertEqual(results, expected)
2407+
for i, _ in enumerate(slots):
2408+
with self.subTest(cls=cls, slot=slot):
2409+
expected = all_expected[i]
2410+
result = rch.recv()
2411+
self.assertEqual(result, expected)
23832412

23842413

23852414
if __name__ == '__main__':

0 commit comments

Comments
 (0)