bpo-29851: Have importlib.reload() raise ImportError if the module's … · python/cpython@9498782 (original) (raw)

5 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -203,6 +203,9 @@ Functions
203 203 classes.
204 204
205 205 .. versionadded:: 3.4
206 + .. versionchanged:: 3.7
207 +:exc:`ModuleNotFoundError` is raised when the module being reloaded lacks
208 + a :class:`ModuleSpec`.
206 209
207 210
208 211 :mod:`importlib.abc` -- Abstract base classes related to import
Original file line number Diff line number Diff line change
@@ -164,6 +164,8 @@ def reload(module):
164 164 pkgpath = None
165 165 target = module
166 166 spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target)
167 +if spec is None:
168 +raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
167 169 _bootstrap._exec(spec, module)
168 170 # The module may have replaced itself in sys.modules!
169 171 return sys.modules[name]
Original file line number Diff line number Diff line change
@@ -197,8 +197,6 @@ def find_module(name, path=None):
197 197
198 198 class ReloadTests:
199 199
200 -"""Test module reloading for builtin and extension modules."""
201 -
202 200 def test_reload_modules(self):
203 201 for mod in ('tokenize', 'time', 'marshal'):
204 202 with self.subTest(module=mod):
@@ -361,6 +359,18 @@ def test_reload_submodule(self):
361 359 reloaded = self.init.reload(ham)
362 360 self.assertIs(reloaded, ham)
363 361
362 +def test_module_missing_spec(self):
363 +#Test that reload() throws ModuleNotFounderror when reloading
364 +# a module who's missing a spec. (bpo-29851)
365 +name = 'spam'
366 +with test_util.uncache(name):
367 +module = sys.modules[name] = types.ModuleType(name)
368 +# Sanity check by attempting an import.
369 +module = self.init.import_module(name)
370 +self.assertIsNone(module.__spec__)
371 +with self.assertRaises(ModuleNotFoundError):
372 +self.init.reload(module)
373 +
364 374
365 375 (Frozen_ReloadTests,
366 376 Source_ReloadTests
Original file line number Diff line number Diff line change
@@ -774,6 +774,7 @@ Robert Kern
774 774 Jim Kerr
775 775 Magnus Kessler
776 776 Lawrence Kesteloot
777 +Garvit Khatri
777 778 Vivek Khera
778 779 Dhiru Kholia
779 780 Akshit Khurana
Original file line number Diff line number Diff line change
@@ -604,6 +604,9 @@ Library
604 604 - bpo-10379: locale.format_string now supports the 'monetary' keyword argument,
605 605 and locale.format is deprecated.
606 606
607 +- bpo-29851: importlib.reload() now raises ModuleNotFoundError if the
608 + module lacks a spec.
609 +
607 610 - Issue #28556: Various updates to typing module: typing.Counter, typing.ChainMap,
608 611 improved ABC caching, etc. Original PRs by Jelle Zijlstra, Ivan Levkivskyi,
609 612 Manuel Krebber, and Łukasz Langa.