Skip to content

Commit 3f49f42

Browse files
authored
Update test_module from 3.14.3 (#7552)
* Update `test_module` from 3.14.3 * Unmark passing test
1 parent 5afa349 commit 3f49f42

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

Lib/test/test_module/__init__.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Test the module type
2+
import importlib.machinery
23
import unittest
34
import weakref
45
from test.support import gc_collect
@@ -29,7 +30,7 @@ def test_uninitialized(self):
2930
self.fail("__name__ = %s" % repr(s))
3031
except AttributeError:
3132
pass
32-
self.assertEqual(foo.__doc__, ModuleType.__doc__)
33+
self.assertEqual(foo.__doc__, ModuleType.__doc__ or '')
3334

3435
def test_uninitialized_missing_getattr(self):
3536
# Issue 8297
@@ -102,8 +103,7 @@ def f():
102103
gc_collect()
103104
self.assertEqual(f().__dict__["bar"], 4)
104105

105-
# TODO: RUSTPYTHON
106-
@unittest.expectedFailure
106+
@unittest.expectedFailure # TODO: RUSTPYTHON
107107
def test_clear_dict_in_ref_cycle(self):
108108
destroyed = []
109109
m = ModuleType("foo")
@@ -152,15 +152,13 @@ def test_module_getattr_errors(self):
152152
if 'test.test_module.bad_getattr2' in sys.modules:
153153
del sys.modules['test.test_module.bad_getattr2']
154154

155-
# TODO: RUSTPYTHON
156-
@unittest.expectedFailure
155+
@unittest.expectedFailure # TODO: RUSTPYTHON
157156
def test_module_dir(self):
158157
import test.test_module.good_getattr as gga
159158
self.assertEqual(dir(gga), ['a', 'b', 'c'])
160159
del sys.modules['test.test_module.good_getattr']
161160

162-
# TODO: RUSTPYTHON
163-
@unittest.expectedFailure
161+
@unittest.expectedFailure # TODO: RUSTPYTHON
164162
def test_module_dir_errors(self):
165163
import test.test_module.bad_getattr as bga
166164
from test.test_module import bad_getattr2
@@ -270,11 +268,38 @@ def test_module_repr_source(self):
270268
self.assertEqual(r[-len(ends_with):], ends_with,
271269
'{!r} does not end with {!r}'.format(r, ends_with))
272270

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+
275300
def test_module_finalization_at_shutdown(self):
276301
# 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")
278303
self.assertFalse(err)
279304
lines = out.splitlines()
280305
self.assertEqual(set(lines), {

Lib/test/test_module/final_a.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Fodder for module finalization tests in test_module.
3+
"""
4+
5+
import shutil
6+
import test.test_module.final_b
7+
8+
x = 'a'
9+
10+
class C:
11+
def __del__(self):
12+
# Inspect module globals and builtins
13+
print("x =", x)
14+
print("final_b.x =", test.test_module.final_b.x)
15+
print("shutil.rmtree =", getattr(shutil.rmtree, '__name__', None))
16+
print("len =", getattr(len, '__name__', None))
17+
18+
c = C()
19+
_underscored = C()

Lib/test/test_module/final_b.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Fodder for module finalization tests in test_module.
3+
"""
4+
5+
import shutil
6+
import test.test_module.final_a
7+
8+
x = 'b'
9+
10+
class C:
11+
def __del__(self):
12+
# Inspect module globals and builtins
13+
print("x =", x)
14+
print("final_a.x =", test.test_module.final_a.x)
15+
print("shutil.rmtree =", getattr(shutil.rmtree, '__name__', None))
16+
print("len =", getattr(len, '__name__', None))
17+
18+
c = C()
19+
_underscored = C()

0 commit comments

Comments
 (0)