Skip to content

Commit 9498782

Browse files
Garvit Khatribrettcannon
authored andcommitted
bpo-29851: Have importlib.reload() raise ImportError if the module's spec is not found (pythonGH-972)
1 parent 3480ef9 commit 9498782

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

Doc/library/importlib.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ Functions
203203
classes.
204204

205205
.. versionadded:: 3.4
206+
.. versionchanged:: 3.7
207+
:exc:`ModuleNotFoundError` is raised when the module being reloaded lacks
208+
a :class:`ModuleSpec`.
206209

207210

208211
:mod:`importlib.abc` -- Abstract base classes related to import

Lib/importlib/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ def reload(module):
164164
pkgpath = None
165165
target = module
166166
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)
167169
_bootstrap._exec(spec, module)
168170
# The module may have replaced itself in sys.modules!
169171
return sys.modules[name]

Lib/test/test_importlib/test_api.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ def find_module(name, path=None):
197197

198198
class ReloadTests:
199199

200-
"""Test module reloading for builtin and extension modules."""
201-
202200
def test_reload_modules(self):
203201
for mod in ('tokenize', 'time', 'marshal'):
204202
with self.subTest(module=mod):
@@ -361,6 +359,18 @@ def test_reload_submodule(self):
361359
reloaded = self.init.reload(ham)
362360
self.assertIs(reloaded, ham)
363361

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+
364374

365375
(Frozen_ReloadTests,
366376
Source_ReloadTests

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ Robert Kern
774774
Jim Kerr
775775
Magnus Kessler
776776
Lawrence Kesteloot
777+
Garvit Khatri
777778
Vivek Khera
778779
Dhiru Kholia
779780
Akshit Khurana

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ Library
604604
- bpo-10379: locale.format_string now supports the 'monetary' keyword argument,
605605
and locale.format is deprecated.
606606

607+
- bpo-29851: importlib.reload() now raises ModuleNotFoundError if the
608+
module lacks a spec.
609+
607610
- Issue #28556: Various updates to typing module: typing.Counter, typing.ChainMap,
608611
improved ABC caching, etc. Original PRs by Jelle Zijlstra, Ivan Levkivskyi,
609612
Manuel Krebber, and Łukasz Langa.

0 commit comments

Comments
 (0)