Skip to content

Commit

Permalink
pythongh-112414: Add additional unit tests for calling repr() on a …
Browse files Browse the repository at this point in the history
…namespace package (pythonGH-112475)

(cherry picked from commit cf20540)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
  • Loading branch information
2 people authored and miss-islington committed Nov 28, 2023
1 parent 4f71f16 commit 39d5c89
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 0 additions & 4 deletions Lib/test/test_importlib/import_/test___loader__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ def test___loader__(self):
with util.uncache('blah'), util.import_state(meta_path=[loader]):
module = self.__import__('blah')
self.assertEqual(loader, module.__loader__)
expected_repr_pattern = (
r"<module 'blah' \(<test\.test_importlib\..*SpecLoaderMock object at .+>\)>"
)
self.assertRegex(repr(module), expected_repr_pattern)


(Frozen_SpecTests,
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_module/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Test the module type
import importlib.machinery
import unittest
import weakref
from test.support import gc_collect
Expand Down Expand Up @@ -264,6 +265,35 @@ def test_module_repr_source(self):
self.assertEqual(r[-len(ends_with):], ends_with,
'{!r} does not end with {!r}'.format(r, ends_with))

def test_module_repr_with_namespace_package(self):
m = ModuleType('foo')
loader = importlib.machinery.NamespaceLoader('foo', ['bar'], 'baz')
spec = importlib.machinery.ModuleSpec('foo', loader)
m.__loader__ = loader
m.__spec__ = spec
self.assertEqual(repr(m), "<module 'foo' (namespace) from ['bar']>")

def test_module_repr_with_namespace_package_and_custom_loader(self):
m = ModuleType('foo')
loader = BareLoader()
spec = importlib.machinery.ModuleSpec('foo', loader)
m.__loader__ = loader
m.__spec__ = spec
expected_repr_pattern = r"<module 'foo' \(<.*\.BareLoader object at .+>\)>"
self.assertRegex(repr(m), expected_repr_pattern)
self.assertNotIn('from', repr(m))

def test_module_repr_with_fake_namespace_package(self):
m = ModuleType('foo')
loader = BareLoader()
loader._path = ['spam']
spec = importlib.machinery.ModuleSpec('foo', loader)
m.__loader__ = loader
m.__spec__ = spec
expected_repr_pattern = r"<module 'foo' \(<.*\.BareLoader object at .+>\)>"
self.assertRegex(repr(m), expected_repr_pattern)
self.assertNotIn('from', repr(m))

def test_module_finalization_at_shutdown(self):
# Module globals and builtins should still be available during shutdown
rc, out, err = assert_python_ok("-c", "from test.test_module import final_a")
Expand Down

0 comments on commit 39d5c89

Please sign in to comment.