[Python-checkins] cpython (merge 3.4 -> default): Merge from 3.4.

eric.snow python-checkins at python.org
Sat Apr 19 08:17:49 CEST 2014


http://hg.python.org/cpython/rev/ffb1845e96e6
changeset:   90410:ffb1845e96e6
parent:      90408:c82dcad83438
parent:      90409:bc4eb1b3db5d
user:        Eric Snow <ericsnowcurrently at gmail.com>
date:        Sat Apr 19 00:14:54 2014 -0600
summary:
  Merge from 3.4.

files:
  Lib/pkgutil.py           |   2 ++
  Lib/test/test_pkgutil.py |  19 ++++++++++++++++++-
  Misc/NEWS                |   2 ++
  3 files changed, 22 insertions(+), 1 deletions(-)


diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -461,6 +461,8 @@
         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
--- 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 @@
         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:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -129,6 +129,8 @@
   :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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list