Skip to content

Commit 9cd9c5b

Browse files
committed
Improve tests for type flags to prevent regressions
1 parent 407de3c commit 9cd9c5b

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Lib/test/test_class.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ def __init__(self, arg):
859859

860860
from _testinternalcapi import has_inline_values
861861

862+
Py_TPFLAGS_INLINE_VALUES = (1 << 2)
862863
Py_TPFLAGS_MANAGED_DICT = (1 << 4)
863864

864865
class NoManagedDict:
@@ -876,19 +877,31 @@ def __init__(self):
876877
self.c = 3
877878
self.d = 4
878879

879-
class TestNoManagedValues(unittest.TestCase):
880-
def test_flags(self):
881-
self.assertEqual(NoManagedDict.__flags__ & Py_TPFLAGS_MANAGED_DICT, 0)
880+
class VarSizedSubclass(tuple):
881+
pass
882882

883-
def test_no_inline_values_for_slots_class(self):
884-
c = NoManagedDict()
885-
self.assertFalse(has_inline_values(c))
886883

887884
class TestInlineValues(unittest.TestCase):
888885

889-
def test_flags(self):
890-
self.assertEqual(Plain.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT)
891-
self.assertEqual(WithAttrs.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT)
886+
def test_no_flags_for_slots_class(self):
887+
flags = NoManagedDict.__flags__
888+
self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, 0)
889+
self.assertEqual(flags & Py_TPFLAGS_INLINE_VALUES, 0)
890+
self.assertFalse(has_inline_values(NoManagedDict()))
891+
892+
def test_both_flags_for_regular_class(self):
893+
for cls in (Plain, WithAttrs):
894+
with self.subTest(cls=cls.__name__):
895+
flags = cls.__flags__
896+
self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT)
897+
self.assertEqual(flags & Py_TPFLAGS_INLINE_VALUES, Py_TPFLAGS_INLINE_VALUES)
898+
self.assertTrue(has_inline_values(cls()))
899+
900+
def test_managed_dict_only_for_varsized_subclass(self):
901+
flags = VarSizedSubclass.__flags__
902+
self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT)
903+
self.assertEqual(flags & Py_TPFLAGS_INLINE_VALUES, 0)
904+
self.assertFalse(has_inline_values(VarSizedSubclass()))
892905

893906
def test_has_inline_values(self):
894907
c = Plain()

0 commit comments

Comments
 (0)