]> granicus.if.org Git - python/commitdiff
Issue #21200: Return None from pkgutil.get_loader() when __spec__ is missing.
authorEric Snow <ericsnowcurrently@gmail.com>
Sat, 19 Apr 2014 06:13:23 +0000 (00:13 -0600)
committerEric Snow <ericsnowcurrently@gmail.com>
Sat, 19 Apr 2014 06:13:23 +0000 (00:13 -0600)
Lib/pkgutil.py
Lib/test/test_pkgutil.py
Misc/NEWS

index 58cccdc974c53fc59a9d5eb1d85c7573c5507eaf..e42b6eb71f6b12edbaef993591658fdb98ec6e08 100644 (file)
@@ -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
index c4410a9041eb20a39180940a59dde5e88c46fd46..9704156a524e3983a5dfa18a5925da678e4ef736 100644 (file)
@@ -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:
index 7ef569d90b8bad6aacc1b1abb8f2fcd85ff1f76b..3e121afc2bd70bd2423ba2866e70b1be03091d10 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -86,6 +86,8 @@ Library
   :func:`tempfile.NamedTemporaryFile`, close the file descriptor if
   :func:`io.open` fails
 
+- Issue #21200: Return None from pkgutil.get_loader() when __spec__ is missing.
+
 - Issue #21013: Enhance ssl.create_default_context() when used for server side
   sockets to provide better security by default.