|
1 | 1 | # Test the module type |
| 2 | +import importlib.machinery |
2 | 3 | import unittest |
3 | 4 | import weakref |
4 | 5 | from test.support import gc_collect |
@@ -29,7 +30,7 @@ def test_uninitialized(self): |
29 | 30 | self.fail("__name__ = %s" % repr(s)) |
30 | 31 | except AttributeError: |
31 | 32 | pass |
32 | | - self.assertEqual(foo.__doc__, ModuleType.__doc__) |
| 33 | + self.assertEqual(foo.__doc__, ModuleType.__doc__ or '') |
33 | 34 |
|
34 | 35 | def test_uninitialized_missing_getattr(self): |
35 | 36 | # Issue 8297 |
@@ -102,8 +103,7 @@ def f(): |
102 | 103 | gc_collect() |
103 | 104 | self.assertEqual(f().__dict__["bar"], 4) |
104 | 105 |
|
105 | | - # TODO: RUSTPYTHON |
106 | | - @unittest.expectedFailure |
| 106 | + @unittest.expectedFailure # TODO: RUSTPYTHON |
107 | 107 | def test_clear_dict_in_ref_cycle(self): |
108 | 108 | destroyed = [] |
109 | 109 | m = ModuleType("foo") |
@@ -152,15 +152,13 @@ def test_module_getattr_errors(self): |
152 | 152 | if 'test.test_module.bad_getattr2' in sys.modules: |
153 | 153 | del sys.modules['test.test_module.bad_getattr2'] |
154 | 154 |
|
155 | | - # TODO: RUSTPYTHON |
156 | | - @unittest.expectedFailure |
| 155 | + @unittest.expectedFailure # TODO: RUSTPYTHON |
157 | 156 | def test_module_dir(self): |
158 | 157 | import test.test_module.good_getattr as gga |
159 | 158 | self.assertEqual(dir(gga), ['a', 'b', 'c']) |
160 | 159 | del sys.modules['test.test_module.good_getattr'] |
161 | 160 |
|
162 | | - # TODO: RUSTPYTHON |
163 | | - @unittest.expectedFailure |
| 161 | + @unittest.expectedFailure # TODO: RUSTPYTHON |
164 | 162 | def test_module_dir_errors(self): |
165 | 163 | import test.test_module.bad_getattr as bga |
166 | 164 | from test.test_module import bad_getattr2 |
@@ -270,11 +268,38 @@ def test_module_repr_source(self): |
270 | 268 | self.assertEqual(r[-len(ends_with):], ends_with, |
271 | 269 | '{!r} does not end with {!r}'.format(r, ends_with)) |
272 | 270 |
|
273 | | - # TODO: RUSTPYTHON |
274 | | - @unittest.expectedFailure |
| 271 | + def test_module_repr_with_namespace_package(self): |
| 272 | + m = ModuleType('foo') |
| 273 | + loader = importlib.machinery.NamespaceLoader('foo', ['bar'], 'baz') |
| 274 | + spec = importlib.machinery.ModuleSpec('foo', loader) |
| 275 | + m.__loader__ = loader |
| 276 | + m.__spec__ = spec |
| 277 | + self.assertEqual(repr(m), "<module 'foo' (namespace) from ['bar']>") |
| 278 | + |
| 279 | + def test_module_repr_with_namespace_package_and_custom_loader(self): |
| 280 | + m = ModuleType('foo') |
| 281 | + loader = BareLoader() |
| 282 | + spec = importlib.machinery.ModuleSpec('foo', loader) |
| 283 | + m.__loader__ = loader |
| 284 | + m.__spec__ = spec |
| 285 | + expected_repr_pattern = r"<module 'foo' \(<.*\.BareLoader object at .+>\)>" |
| 286 | + self.assertRegex(repr(m), expected_repr_pattern) |
| 287 | + self.assertNotIn('from', repr(m)) |
| 288 | + |
| 289 | + def test_module_repr_with_fake_namespace_package(self): |
| 290 | + m = ModuleType('foo') |
| 291 | + loader = BareLoader() |
| 292 | + loader._path = ['spam'] |
| 293 | + spec = importlib.machinery.ModuleSpec('foo', loader) |
| 294 | + m.__loader__ = loader |
| 295 | + m.__spec__ = spec |
| 296 | + expected_repr_pattern = r"<module 'foo' \(<.*\.BareLoader object at .+>\)>" |
| 297 | + self.assertRegex(repr(m), expected_repr_pattern) |
| 298 | + self.assertNotIn('from', repr(m)) |
| 299 | + |
275 | 300 | def test_module_finalization_at_shutdown(self): |
276 | 301 | # Module globals and builtins should still be available during shutdown |
277 | | - rc, out, err = assert_python_ok("-c", "from test import final_a") |
| 302 | + rc, out, err = assert_python_ok("-c", "from test.test_module import final_a") |
278 | 303 | self.assertFalse(err) |
279 | 304 | lines = out.splitlines() |
280 | 305 | self.assertEqual(set(lines), { |
|
0 commit comments