Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01b0475

Browse files
committed
Issue #18115: Abstract out managing the cleanup of modules to use in
loaders where C code provides the loaded module.
1 parent 6d26eba commit 01b0475

2 files changed

Lines changed: 2491 additions & 2461 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,24 @@ def _verbose_message(message, *args, verbosity=1):
475475
print(message.format(*args), file=sys.stderr)
476476

477477

478+
class _ManageReload:
479+
480+
def __init__(self, name):
481+
self._name = name
482+
483+
def __enter__(self):
484+
self._is_reload = self._name in sys.modules
485+
486+
def __exit__(self, *args):
487+
if any(arg is not None for arg in args) and not self._is_reload:
488+
try:
489+
del sys.modules[self._name]
490+
except KeyError:
491+
pass
492+
493+
478494
# Written as a class only because contextlib is not available.
479-
class _ModuleManager:
495+
class _ModuleManager(_ManageReload):
480496

481497
"""Context manager which returns the module to be loaded.
482498
@@ -490,12 +506,12 @@ def __init__(self, name, *, reset_name=True):
490506
The reset_name argument specifies whether to unconditionally reset
491507
the __name__ attribute if the module is found to be a reload.
492508
"""
493-
self._name = name
509+
super().__init__(name)
494510
self._reset_name = reset_name
495511

496512
def __enter__(self):
513+
super().__enter__()
497514
self._module = sys.modules.get(self._name)
498-
self._is_reload = self._module is not None
499515
if not self._is_reload:
500516
# This must be done before open() is called as the 'io' module
501517
# implicitly imports 'locale' and would otherwise trigger an
@@ -510,14 +526,12 @@ def __enter__(self):
510526
self._module.__name__ = self._name
511527
except AttributeError:
512528
pass
513-
514529
return self._module
515530

516531
def __exit__(self, *args):
517532
self._module.__initializing__ = False
518533
del self._module
519-
if any(arg is not None for arg in args) and not self._is_reload:
520-
del sys.modules[self._name]
534+
super().__exit__(*args)
521535

522536

523537
def module_to_load(name, *, reset_name=True):
@@ -741,13 +755,8 @@ def find_module(cls, fullname, path=None):
741755
@_requires_builtin
742756
def load_module(cls, fullname):
743757
"""Load a built-in module."""
744-
is_reload = fullname in sys.modules
745-
try:
758+
with _ManageReload(fullname):
746759
return _call_with_frames_removed(_imp.init_builtin, fullname)
747-
except:
748-
if not is_reload and fullname in sys.modules:
749-
del sys.modules[fullname]
750-
raise
751760

752761
@classmethod
753762
@_requires_builtin
@@ -792,16 +801,11 @@ def find_module(cls, fullname, path=None):
792801
@_requires_frozen
793802
def load_module(cls, fullname):
794803
"""Load a frozen module."""
795-
is_reload = fullname in sys.modules
796-
try:
804+
with _ManageReload(fullname):
797805
m = _call_with_frames_removed(_imp.init_frozen, fullname)
798806
# Let our own module_repr() method produce a suitable repr.
799807
del m.__file__
800808
return m
801-
except:
802-
if not is_reload and fullname in sys.modules:
803-
del sys.modules[fullname]
804-
raise
805809

806810
@classmethod
807811
@_requires_frozen
@@ -1147,18 +1151,13 @@ def __init__(self, name, path):
11471151
@set_loader
11481152
def load_module(self, fullname):
11491153
"""Load an extension module."""
1150-
is_reload = fullname in sys.modules
1151-
try:
1154+
with _ManageReload(fullname):
11521155
module = _call_with_frames_removed(_imp.load_dynamic,
11531156
fullname, self.path)
11541157
_verbose_message('extension module loaded from {!r}', self.path)
11551158
if self.is_package(fullname) and not hasattr(module, '__path__'):
11561159
module.__path__ = [_path_split(self.path)[0]]
11571160
return module
1158-
except:
1159-
if not is_reload and fullname in sys.modules:
1160-
del sys.modules[fullname]
1161-
raise
11621161

11631162
def is_package(self, fullname):
11641163
"""Return True if the extension module is a package."""

0 commit comments

Comments
 (0)