Commit ce8ff413 authored by Brett Cannon's avatar Brett Cannon

Issue #18157: stop using imp.load_module() in imp.

parent b8f06cc1
...@@ -53,6 +53,7 @@ Richard Chamberlain, for the first implementation of textdoc. ...@@ -53,6 +53,7 @@ Richard Chamberlain, for the first implementation of textdoc.
import builtins import builtins
import imp import imp
import importlib._bootstrap
import importlib.machinery import importlib.machinery
import inspect import inspect
import io import io
...@@ -269,18 +270,17 @@ def importfile(path): ...@@ -269,18 +270,17 @@ def importfile(path):
"""Import a Python source file or compiled file given its path.""" """Import a Python source file or compiled file given its path."""
magic = imp.get_magic() magic = imp.get_magic()
with open(path, 'rb') as file: with open(path, 'rb') as file:
if file.read(len(magic)) == magic: is_bytecode = magic == file.read(len(magic))
kind = imp.PY_COMPILED filename = os.path.basename(path)
else: name, ext = os.path.splitext(filename)
kind = imp.PY_SOURCE if is_bytecode:
file.seek(0) loader = importlib._bootstrap.SourcelessFileLoader(name, path)
filename = os.path.basename(path) else:
name, ext = os.path.splitext(filename) loader = importlib._bootstrap.SourceFileLoader(name, path)
try: try:
module = imp.load_module(name, file, path, (ext, 'r', kind)) return loader.load_module(name)
except: except:
raise ErrorDuringImport(path, sys.exc_info()) raise ErrorDuringImport(path, sys.exc_info())
return module
def safeimport(path, forceload=0, cache={}): def safeimport(path, forceload=0, cache={}):
"""Import a module; handle errors; return None if the module isn't found. """Import a module; handle errors; return None if the module isn't found.
......
...@@ -120,6 +120,8 @@ Core and Builtins ...@@ -120,6 +120,8 @@ Core and Builtins
Library Library
------- -------
- Issue #18157: Stop using imp.load_module() in pydoc.
- Issue #16102: Make uuid._netbios_getnode() work again on Python 3. - Issue #16102: Make uuid._netbios_getnode() work again on Python 3.
- Issue #17134: Add ssl.enum_cert_store() as interface to Windows' cert store. - Issue #17134: Add ssl.enum_cert_store() as interface to Windows' cert store.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment