File tree 4 files changed +16
-3
lines changed
4 files changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -824,10 +824,16 @@ def _module_repr_from_spec(spec):
824
824
"""Return the repr to use for the module."""
825
825
name = '?' if spec .name is None else spec .name
826
826
if spec .origin is None :
827
- if spec .loader is None :
827
+ loader = spec .loader
828
+ if loader is None :
828
829
return f'<module { name !r} >'
830
+ elif (
831
+ _bootstrap_external is not None
832
+ and isinstance (loader , _bootstrap_external .NamespaceLoader )
833
+ ):
834
+ return f'<module { name !r} (namespace) from { list (loader ._path )} >'
829
835
else :
830
- return f'<module { name !r} (namespace) from { list ( spec . loader . _path ) } >'
836
+ return f'<module { name !r} ({ loader !r } ) >'
831
837
else :
832
838
if spec .has_location :
833
839
return f'<module { name !r} from { spec .origin !r} >'
Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ def test___loader__(self):
23
23
with util .uncache ('blah' ), util .import_state (meta_path = [loader ]):
24
24
module = self .__import__ ('blah' )
25
25
self .assertEqual (loader , module .__loader__ )
26
+ expected_repr_pattern = (
27
+ r"<module 'blah' \(<test\.test_importlib\..*SpecLoaderMock object at .+>\)>"
28
+ )
29
+ self .assertRegex (repr (module ), expected_repr_pattern )
26
30
27
31
28
32
(Frozen_SpecTests ,
Original file line number Diff line number Diff line change @@ -80,7 +80,7 @@ def test_cant_import_other(self):
80
80
81
81
def test_simple_repr (self ):
82
82
import foo .one
83
- assert repr (foo ).startswith ("<module 'foo' (namespace) from [" )
83
+ self . assertTrue ( repr (foo ).startswith ("<module 'foo' (namespace) from [" ) )
84
84
85
85
86
86
class DynamicPathNamespacePackage (NamespacePackageTest ):
Original file line number Diff line number Diff line change
1
+ Fix regression in Python 3.12 where calling :func: `repr ` on a module that
2
+ had been imported using a custom :term: `loader ` could fail with
3
+ :exc: `AttributeError `. Patch by Alex Waygood.
You can’t perform that action at this time.
0 commit comments