]> granicus.if.org Git - python/commitdiff
Issue #17099: Have importlib.find_loader() raise ValueError when
authorBrett Cannon <brett@python.org>
Wed, 13 Mar 2013 18:09:08 +0000 (11:09 -0700)
committerBrett Cannon <brett@python.org>
Wed, 13 Mar 2013 18:09:08 +0000 (11:09 -0700)
__loader__ is not set on a module. This brings the exception in line
with when __loader__ is None (which is equivalent to not having the
attribute defined).

Doc/library/importlib.rst
Lib/importlib/__init__.py
Lib/test/test_importlib/test_api.py
Misc/NEWS

index 1bd11f45ff1bb90dae623cdc9a890152e5f915cf..5360d3c11d31bd98aa3bacc4025e8140e59b5ff3 100644 (file)
@@ -90,7 +90,7 @@ Functions
 
    Find the loader for a module, optionally within the specified *path*. If the
    module is in :attr:`sys.modules`, then ``sys.modules[name].__loader__`` is
-   returned (unless the loader would be ``None``, in which case
+   returned (unless the loader would be ``None`` or is not set, in which case
    :exc:`ValueError` is raised). Otherwise a search using :attr:`sys.meta_path`
    is done. ``None`` is returned if no loader is found.
 
@@ -99,6 +99,12 @@ Functions
    will need to import all parent packages of the submodule and use the correct
    argument to *path*.
 
+   .. versionadded:: 3.3
+
+   .. versionchanged:: 3.4
+      If ``__loader__`` is not set, raise :exc:`ValueError`, just like when the
+      attribute is set to ``None``.
+
 .. function:: invalidate_caches()
 
    Invalidate the internal caches of finders stored at
index 22c90f24a7e46cecbbc87027a319576d7c32c006..d07f02ed746aba9dddb59db85c69f065993a731f 100644 (file)
@@ -68,6 +68,8 @@ def find_loader(name, path=None):
             return loader
     except KeyError:
         pass
+    except AttributeError:
+        raise ValueError('{}.__loader__ is not set'.format(name))
     return _bootstrap._find_module(name, path)
 
 
index f66e257ee99c9476b5ddb73439a78c1598b25a54..ac88b2bf9c1c674bc441ec021d8fb82b93199780 100644 (file)
@@ -116,6 +116,20 @@ class FindLoaderTests(unittest.TestCase):
             with self.assertRaises(ValueError):
                 importlib.find_loader(name)
 
+    def test_sys_modules_loader_is_not_set(self):
+        # Should raise ValueError
+        # Issue #17099
+        name = 'some_mod'
+        with util.uncache(name):
+            module = imp.new_module(name)
+            try:
+                del module.__loader__
+            except AttributeError:
+                pass
+            sys.modules[name] = module
+            with self.assertRaises(ValueError):
+                importlib.find_loader(name)
+
     def test_success(self):
         # Return the loader found on sys.meta_path.
         name = 'some_mod'
index 06e2232375839f3f9da169d66790c5e59e718e11..c5a58775e2735b021babf787abeb59f651bd70f3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -280,6 +280,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17099: Have importlib.find_loader() raise ValueError when __loader__
+  is not set, harmonizing with what happens when the attribute is set to None.
+
 - Expose the O_PATH constant in the os module if it is available.
 
 - Issue #17368: Fix an off-by-one error in the Python JSON decoder that caused