From 407de3c43fe30597a35ca21ad4af7344d63df478 Mon Sep 17 00:00:00 2001 From: Jiseok CHOI Date: Fri, 11 Jul 2025 20:25:58 +0900 Subject: [PATCH 1/5] gh-136535: Tests: Correct `Py_TPFLAGS_MANAGED_DICT` in `test_class.py` The `Py_TPFLAGS_MANAGED_DICT` constant in `Lib/test/test_class.py` was incorrectly set to (1 << 2) instead of the correct (1 << 4) from object.h. --- Lib/test/test_class.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 8c7a62a74ba90e..6629205800dc5a 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -859,7 +859,10 @@ def __init__(self, arg): from _testinternalcapi import has_inline_values -Py_TPFLAGS_MANAGED_DICT = (1 << 2) +Py_TPFLAGS_MANAGED_DICT = (1 << 4) + +class NoManagedDict: + __slots__ = ('a',) class Plain: pass @@ -873,6 +876,13 @@ def __init__(self): self.c = 3 self.d = 4 +class TestNoManagedValues(unittest.TestCase): + def test_flags(self): + self.assertEqual(NoManagedDict.__flags__ & Py_TPFLAGS_MANAGED_DICT, 0) + + def test_no_inline_values_for_slots_class(self): + c = NoManagedDict() + self.assertFalse(has_inline_values(c)) class TestInlineValues(unittest.TestCase): From 9cd9c5b13cb3345431176a59f92eda08631c668f Mon Sep 17 00:00:00 2001 From: Jiseok CHOI Date: Sun, 13 Jul 2025 12:40:58 +0900 Subject: [PATCH 2/5] Improve tests for type flags to prevent regressions --- Lib/test/test_class.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 6629205800dc5a..ada429398e2da1 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -859,6 +859,7 @@ def __init__(self, arg): from _testinternalcapi import has_inline_values +Py_TPFLAGS_INLINE_VALUES = (1 << 2) Py_TPFLAGS_MANAGED_DICT = (1 << 4) class NoManagedDict: @@ -876,19 +877,31 @@ def __init__(self): self.c = 3 self.d = 4 -class TestNoManagedValues(unittest.TestCase): - def test_flags(self): - self.assertEqual(NoManagedDict.__flags__ & Py_TPFLAGS_MANAGED_DICT, 0) +class VarSizedSubclass(tuple): + pass - def test_no_inline_values_for_slots_class(self): - c = NoManagedDict() - self.assertFalse(has_inline_values(c)) class TestInlineValues(unittest.TestCase): - def test_flags(self): - self.assertEqual(Plain.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT) - self.assertEqual(WithAttrs.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT) + def test_no_flags_for_slots_class(self): + flags = NoManagedDict.__flags__ + self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, 0) + self.assertEqual(flags & Py_TPFLAGS_INLINE_VALUES, 0) + self.assertFalse(has_inline_values(NoManagedDict())) + + def test_both_flags_for_regular_class(self): + for cls in (Plain, WithAttrs): + with self.subTest(cls=cls.__name__): + flags = cls.__flags__ + self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT) + self.assertEqual(flags & Py_TPFLAGS_INLINE_VALUES, Py_TPFLAGS_INLINE_VALUES) + self.assertTrue(has_inline_values(cls())) + + def test_managed_dict_only_for_varsized_subclass(self): + flags = VarSizedSubclass.__flags__ + self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT) + self.assertEqual(flags & Py_TPFLAGS_INLINE_VALUES, 0) + self.assertFalse(has_inline_values(VarSizedSubclass())) def test_has_inline_values(self): c = Plain() From b39ca1a4cc7d68476582ae5ffda5addea411b23a Mon Sep 17 00:00:00 2001 From: Jiseok CHOI Date: Sun, 13 Jul 2025 14:05:52 +0900 Subject: [PATCH 3/5] revive `test_flags` --- Lib/test/test_class.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index ada429398e2da1..1b2b481fae8e24 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -883,6 +883,10 @@ class VarSizedSubclass(tuple): class TestInlineValues(unittest.TestCase): + def test_flags(self): + self.assertEqual(Plain.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT) + self.assertEqual(WithAttrs.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT) + def test_no_flags_for_slots_class(self): flags = NoManagedDict.__flags__ self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, 0) From fd8e8e617181752246346fc793d6860656b7a7cc Mon Sep 17 00:00:00 2001 From: Jiseok CHOI Date: Sun, 13 Jul 2025 14:06:58 +0900 Subject: [PATCH 4/5] PEP 8 --- Lib/test/test_class.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 1b2b481fae8e24..c3c21271014ca3 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -865,6 +865,7 @@ def __init__(self, arg): class NoManagedDict: __slots__ = ('a',) + class Plain: pass @@ -877,6 +878,7 @@ def __init__(self): self.c = 3 self.d = 4 + class VarSizedSubclass(tuple): pass From 63aff5630a9859ce7f36d089622a96dcb1e600d2 Mon Sep 17 00:00:00 2001 From: Jiseok CHOI Date: Sun, 13 Jul 2025 15:12:23 +0900 Subject: [PATCH 5/5] Revert "revive `test_flags`" This reverts commit b39ca1a4cc7d68476582ae5ffda5addea411b23a. --- Lib/test/test_class.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index c3c21271014ca3..64222555166a2e 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -885,10 +885,6 @@ class VarSizedSubclass(tuple): class TestInlineValues(unittest.TestCase): - def test_flags(self): - self.assertEqual(Plain.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT) - self.assertEqual(WithAttrs.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT) - def test_no_flags_for_slots_class(self): flags = NoManagedDict.__flags__ self.assertEqual(flags & Py_TPFLAGS_MANAGED_DICT, 0)