summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pkgutil.py2
-rw-r--r--Lib/test/test_pkgutil.py19
2 files changed, 20 insertions, 1 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index 58cccdc..e42b6eb 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -461,6 +461,8 @@ def get_loader(module_or_name):
loader = getattr(module, '__loader__', None)
if loader is not None:
return loader
+ if getattr(module, '__spec__', None) is None:
+ return None
fullname = module.__name__
else:
fullname = module_or_name
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py
index c4410a9..9704156 100644
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -1,4 +1,4 @@
-from test.support import run_unittest, unload, check_warnings
+from test.support import run_unittest, unload, check_warnings, CleanImport
import unittest
import sys
import importlib
@@ -345,6 +345,23 @@ class ImportlibMigrationTests(unittest.TestCase):
finally:
__loader__ = this_loader
+ def test_get_loader_handles_missing_spec_attribute(self):
+ name = 'spam'
+ mod = type(sys)(name)
+ del mod.__spec__
+ with CleanImport(name):
+ sys.modules[name] = mod
+ loader = pkgutil.get_loader(name)
+ self.assertIsNone(loader)
+
+ def test_get_loader_handles_spec_attribute_none(self):
+ name = 'spam'
+ mod = type(sys)(name)
+ mod.__spec__ = None
+ with CleanImport(name):
+ sys.modules[name] = mod
+ loader = pkgutil.get_loader(name)
+ self.assertIsNone(loader)
def test_find_loader_avoids_emulation(self):
with check_warnings() as w: