Skip to content
Merged
Changes from all commits
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
49 changes: 34 additions & 15 deletions Lib/test/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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__ = {}
Expand All @@ -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()