diff options
Diffstat (limited to 'Lib/runpy.py')
-rw-r--r-- | Lib/runpy.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/Lib/runpy.py b/Lib/runpy.py index 39c0e9f7dd..1e0a2be4f0 100644 --- a/Lib/runpy.py +++ b/Lib/runpy.py @@ -13,7 +13,7 @@ importers when locating support scripts as well as when importing modules. import os import sys import importlib.machinery # importlib first so we can test #15386 via -m -import imp +import types from pkgutil import read_code, get_loader, get_importer __all__ = [ @@ -24,7 +24,7 @@ class _TempModule(object): """Temporarily replace a module in sys.modules with an empty namespace""" def __init__(self, mod_name): self.mod_name = mod_name - self.module = imp.new_module(mod_name) + self.module = types.ModuleType(mod_name) self._saved_module = [] def __enter__(self): @@ -223,7 +223,12 @@ def run_path(path_name, init_globals=None, run_name=None): run_name = "<run_path>" pkg_name = run_name.rpartition(".")[0] importer = get_importer(path_name) - if isinstance(importer, (type(None), imp.NullImporter)): + # Trying to avoid importing imp so as to not consume the deprecation warning. + is_NullImporter = False + if type(importer).__module__ == 'imp': + if type(importer).__name__ == 'NullImporter': + is_NullImporter = True + if isinstance(importer, type(None)) or is_NullImporter: # Not a valid sys.path entry, so run the code directly # execfile() doesn't help as we want to allow compiled files code, mod_loader = _get_code_from_file(run_name, path_name) |