mirror of
https://github.com/python/cpython.git
synced 2025-08-25 03:04:55 +00:00
(merge 3.2) Issue #12451: pydoc: importfile() now opens the Python script in
binary mode, instead of text mode using the locale encoding, to avoid encoding issues.
This commit is contained in:
commit
5f9a995ad7
2 changed files with 15 additions and 14 deletions
26
Lib/pydoc.py
26
Lib/pydoc.py
|
@ -250,20 +250,18 @@ class ErrorDuringImport(Exception):
|
|||
def importfile(path):
|
||||
"""Import a Python source file or compiled file given its path."""
|
||||
magic = imp.get_magic()
|
||||
file = open(path, 'r')
|
||||
if file.read(len(magic)) == magic:
|
||||
kind = imp.PY_COMPILED
|
||||
else:
|
||||
kind = imp.PY_SOURCE
|
||||
file.close()
|
||||
filename = os.path.basename(path)
|
||||
name, ext = os.path.splitext(filename)
|
||||
file = open(path, 'r')
|
||||
try:
|
||||
module = imp.load_module(name, file, path, (ext, 'r', kind))
|
||||
except:
|
||||
raise ErrorDuringImport(path, sys.exc_info())
|
||||
file.close()
|
||||
with open(path, 'rb') as file:
|
||||
if file.read(len(magic)) == magic:
|
||||
kind = imp.PY_COMPILED
|
||||
else:
|
||||
kind = imp.PY_SOURCE
|
||||
file.seek(0)
|
||||
filename = os.path.basename(path)
|
||||
name, ext = os.path.splitext(filename)
|
||||
try:
|
||||
module = imp.load_module(name, file, path, (ext, 'r', kind))
|
||||
except:
|
||||
raise ErrorDuringImport(path, sys.exc_info())
|
||||
return module
|
||||
|
||||
def safeimport(path, forceload=0, cache={}):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue