mirror of
https://github.com/python/cpython.git
synced 2025-08-23 02:04:56 +00:00
Change import_fresh_module to work with packages.
This commit is contained in:
parent
d82a8876f0
commit
720f8dea19
1 changed files with 6 additions and 10 deletions
|
@ -84,19 +84,15 @@ def import_module(name, deprecated=False):
|
||||||
def _save_and_remove_module(name, orig_modules):
|
def _save_and_remove_module(name, orig_modules):
|
||||||
"""Helper function to save and remove a module from sys.modules
|
"""Helper function to save and remove a module from sys.modules
|
||||||
|
|
||||||
Return True if the module was in sys.modules, False otherwise.
|
|
||||||
Raise ImportError if the module can't be imported."""
|
Raise ImportError if the module can't be imported."""
|
||||||
saved = True
|
# try to import the module and raise an error if it can't be imported
|
||||||
try:
|
if name not in sys.modules:
|
||||||
orig_modules[name] = sys.modules[name]
|
|
||||||
except KeyError:
|
|
||||||
# try to import the module and raise an error if it can't be imported
|
|
||||||
__import__(name)
|
__import__(name)
|
||||||
saved = False
|
|
||||||
else:
|
|
||||||
del sys.modules[name]
|
del sys.modules[name]
|
||||||
return saved
|
for modname in list(sys.modules):
|
||||||
|
if modname == name or modname.startswith(name + '.'):
|
||||||
|
orig_modules[modname] = sys.modules[modname]
|
||||||
|
del sys.modules[modname]
|
||||||
|
|
||||||
def _save_and_block_module(name, orig_modules):
|
def _save_and_block_module(name, orig_modules):
|
||||||
"""Helper function to save and block a module in sys.modules
|
"""Helper function to save and block a module in sys.modules
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue