[3.13] gh-117482: Fix the Slot Wrapper Inheritance Tests (gh-122249)

The tests were only checking cases where the slot wrapper was present in the initial case.  They were missing when the slot wrapper was added in the additional initializations.  This fixes that.

(cherry-picked from commit 490e0ad83a, AKA gh-122248)
This commit is contained in:
Eric Snow 2024-07-29 10:25:02 -06:00 committed by GitHub
parent b5e8b10de7
commit 10cf7d6d00
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 131 additions and 51 deletions

View file

@ -417,29 +417,47 @@ class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
self.assertEqual(out, '20000101\n' * INIT_LOOPS)
def test_static_types_inherited_slots(self):
slots = []
script = ['import sys']
from test.test_types import iter_builtin_types, iter_own_slot_wrappers
for cls in iter_builtin_types():
for slot in iter_own_slot_wrappers(cls):
slots.append((cls, slot))
attr = f'{cls.__name__}.{slot}'
script.append(f'print("{attr}:", {attr}, file=sys.stderr)')
script.append('')
script = os.linesep.join(script)
script = textwrap.dedent("""
import test.support
with contextlib.redirect_stderr(io.StringIO()) as stderr:
exec(script)
expected = stderr.getvalue().splitlines()
results = {}
def add(cls, slot, own):
value = getattr(cls, slot)
try:
subresults = results[cls.__name__]
except KeyError:
subresults = results[cls.__name__] = {}
subresults[slot] = [repr(value), own]
out, err = self.run_embedded_interpreter("test_repeated_init_exec", script)
for cls in test.support.iter_builtin_types():
for slot, own in test.support.iter_slot_wrappers(cls):
add(cls, slot, own)
""")
ns = {}
exec(script, ns, ns)
all_expected = ns['results']
del ns
script += textwrap.dedent("""
import json
import sys
text = json.dumps(results)
print(text, file=sys.stderr)
""")
out, err = self.run_embedded_interpreter(
"test_repeated_init_exec", script, script)
results = err.split('--- Loop #')[1:]
results = [res.rpartition(' ---\n')[-1] for res in results]
self.maxDiff = None
for i, result in enumerate(results, start=1):
with self.subTest(loop=i):
self.assertEqual(result.splitlines(), expected)
for i, text in enumerate(results, start=1):
result = json.loads(text)
for classname, expected in all_expected.items():
with self.subTest(loop=i, cls=classname):
slots = result.pop(classname)
self.assertEqual(slots, expected)
self.assertEqual(result, {})
self.assertEqual(out, '')