8000 gh-117482: Expand Tests for Slot Wrappers of Inherited Slots of Static Builtin Types by ericsnowcurrently · Pull Request #122192 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117482: Expand Tests for Slot Wrappers of Inherited Slots of Static Builtin Types #122192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make test_slot_wrappers exhaustive.
  • Loading branch information
ericsnowcurrently committed Jul 23, 2024
commit dc3f581895b651c67cc79f10015655858b2f5a89
49 changes: 39 additions & 10 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ def clear_typing_caches():
f()


def iter_builtin_types():
for obj in __builtins__.values():
if not isinstance(obj, type):
continue
cls = obj
if cls.__module__ != 'builtins':
continue
yield cls


@cpython_only
def iter_own_slot_wrappers(cls):
for name, value in vars(cls).items():
if not name.startswith('__') or not name.endswith('__'):
continue
if 'slot wrapper' not in str(value):
continue
yield name


class TypesTests(unittest.TestCase):

def test_truth_values(self):
Expand Down Expand Up @@ -2361,24 +2381,33 @@ def setUpClass(cls):
def test_slot_wrappers(self):
rch, sch = interpreters.channels.create()

# For now it's sufficient to check int.__str__.
# See https://github.com/python/cpython/issues/117482
# and https://github.com/python/cpython/pull/117660.
script = textwrap.dedent('''
text = repr(int.__str__)
sch.send_nowait(text)
''')
slots = []
script = ''
for cls in iter_builtin_types():
for slot in iter_own_slot_wrappers(cls):
slots.append((cls, slot))
script += textwrap.dedent(f"""
text = repr({cls.__name__}.{slot})
sch.send_nowait(({cls.__name__!r}, {slot!r}, text))
""")

exec(script)
expected = rch.recv()
all_expected = []
for cls, slot in slots:
result = rch.recv()
assert result == (cls.__name__, slot, result[2]), (cls, slot, result)
all_expected.append(result)

interp = interpreters.create()
interp.exec('from test.support import interpreters')
interp.prepare_main(sch=sch)
interp.exec(script)
results = rch.recv()

self.assertEqual(results, expected)
for i, _ in enumerate(slots):
with self.subTest(cls=cls, slot=slot):
expected = all_expected[i]
result = rch.recv()
self.assertEqual(result, expected)


if __name__ == '__main__':
Expand Down
0