]> granicus.if.org Git - python/commitdiff
bpo-29851: Have importlib.reload() raise ImportError if the module's spec is not...
authorGarvit Khatri <garvit.khatri@zomato.com>
Wed, 24 May 2017 22:19:50 +0000 (03:49 +0530)
committerBrett Cannon <brettcannon@users.noreply.github.com>
Wed, 24 May 2017 22:19:50 +0000 (15:19 -0700)
Doc/library/importlib.rst
Lib/importlib/__init__.py
Lib/test/test_importlib/test_api.py
Misc/ACKS
Misc/NEWS

index 1fd56983d0a9e911082d6106dfe0e577c2d73f31..1df613076da8cf00fd42a2b693764139ac171ce7 100644 (file)
@@ -203,6 +203,9 @@ Functions
    classes.
 
    .. versionadded:: 3.4
+   .. versionchanged:: 3.7
+       :exc:`ModuleNotFoundError` is raised when the module being reloaded lacks
+       a :class:`ModuleSpec`.
 
 
 :mod:`importlib.abc` -- Abstract base classes related to import
index 86febffb6d25f14fbd419a26b9416d6cdeed6bf1..cb37d246a7071b7d5105ab6b541c9a966a782745 100644 (file)
@@ -164,6 +164,8 @@ def reload(module):
             pkgpath = None
         target = module
         spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target)
+        if spec is None:
+            raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
         _bootstrap._exec(spec, module)
         # The module may have replaced itself in sys.modules!
         return sys.modules[name]
index b0a94aaff5b330e91e1c0eeac244e44c1a433165..50dbfe1eda4547961c697ab0ae622687d05f9fcc 100644 (file)
@@ -197,8 +197,6 @@ class FindLoaderPEP302Tests(FindLoaderTests):
 
 class ReloadTests:
 
-    """Test module reloading for builtin and extension modules."""
-
     def test_reload_modules(self):
         for mod in ('tokenize', 'time', 'marshal'):
             with self.subTest(module=mod):
@@ -361,6 +359,18 @@ class ReloadTests:
             reloaded = self.init.reload(ham)
             self.assertIs(reloaded, ham)
 
+    def test_module_missing_spec(self):
+        #Test that reload() throws ModuleNotFounderror when reloading
+        # a module who's missing a spec. (bpo-29851)
+        name = 'spam'
+        with test_util.uncache(name):
+            module = sys.modules[name] = types.ModuleType(name)
+            # Sanity check by attempting an import.
+            module = self.init.import_module(name)
+            self.assertIsNone(module.__spec__)
+            with self.assertRaises(ModuleNotFoundError):
+                self.init.reload(module)
+
 
 (Frozen_ReloadTests,
  Source_ReloadTests
index b72c40c3330bc21868c36af89dde4bb4121c078a..bee9eb0e136270d8664763a5da211bec913063e7 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -774,6 +774,7 @@ Robert Kern
 Jim Kerr
 Magnus Kessler
 Lawrence Kesteloot
+Garvit Khatri
 Vivek Khera
 Dhiru Kholia
 Akshit Khurana
index 6960238488245721de2bf2b9670df9901d58e37b..3efbb1789e1500c82a7b8370b763f49dbc3194fa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -604,6 +604,9 @@ Library
 - bpo-10379: locale.format_string now supports the 'monetary' keyword argument,
   and locale.format is deprecated.
 
+- bpo-29851: importlib.reload() now raises ModuleNotFoundError if the
+  module lacks a spec.
+
 - Issue #28556: Various updates to typing module: typing.Counter, typing.ChainMap,
   improved ABC caching, etc. Original PRs by Jelle Zijlstra, Ivan Levkivskyi,
   Manuel Krebber, and Ćukasz Langa.