From 18150fa70f0409efaa9a703a5d98e3406a3cfaef Mon Sep 17 00:00:00 2001 From: Andrey Maltsev Date: Tue, 4 Apr 2023 10:09:18 +0000 Subject: [PATCH] Update test_module.py from Cpython v3.11.2 --- Lib/test/test_module.py | 49 ++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/Lib/test/test_module.py b/Lib/test/test_module.py index 1f7e2b96fd..b921fc6f4e 100644 --- a/Lib/test/test_module.py +++ b/Lib/test/test_module.py @@ -18,14 +18,13 @@ class BareLoader: class ModuleTests(unittest.TestCase): - # TODO: RUSTPYTHON - @unittest.expectedFailure + def test_uninitialized(self): # An uninitialized module has no __dict__ or __name__, # and __doc__ is None foo = ModuleType.__new__(ModuleType) - self.assertTrue(foo.__dict__ is None) - self.assertRaises(TypeError, dir, foo) + self.assertTrue(isinstance(foo.__dict__, dict)) + self.assertEqual(dir(foo), []) try: s = foo.__name__ self.fail("__name__ = %s" % repr(s)) @@ -335,18 +334,7 @@ def test_setting_annotations(self): else: del foo.__dict__['__annotations__'] - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_annotations_getset_raises(self): - # module has no dict, all operations fail - foo = ModuleType.__new__(ModuleType) - with self.assertRaises(TypeError): - print(foo.__annotations__) - with self.assertRaises(TypeError): - foo.__annotations__ = {} - with self.assertRaises(TypeError): - del foo.__annotations__ - # double delete foo = ModuleType("foo") foo.__annotations__ = {} @@ -361,8 +349,39 @@ def test_annotations_are_created_correctly(self): self.assertFalse("__annotations__" in ann_module4.__dict__) + def test_repeated_attribute_pops(self): + # Repeated accesses to module attribute will be specialized + # Check that popping the attribute doesn't break it + m = ModuleType("test") + d = m.__dict__ + count = 0 + for _ in range(100): + m.attr = 1 + count += m.attr # Might be specialized + d.pop("attr") + self.assertEqual(count, 100) + # frozen and namespace module reprs are tested in importlib. + def test_subclass_with_slots(self): + # In 3.11alpha this crashed, as the slots weren't NULLed. + + class ModuleWithSlots(ModuleType): + __slots__ = ("a", "b") + + def __init__(self, name): + super().__init__(name) + + m = ModuleWithSlots("name") + with self.assertRaises(AttributeError): + m.a + with self.assertRaises(AttributeError): + m.b + m.a, m.b = 1, 2 + self.assertEqual(m.a, 1) + self.assertEqual(m.b, 2) + + if __name__ == '__main__': unittest.main()