|
5 | 5 | import importlib.machinery |
6 | 6 | import marshal |
7 | 7 | import os |
| 8 | +import io |
8 | 9 | import sys |
9 | 10 | import types |
10 | 11 | import warnings |
@@ -68,35 +69,32 @@ def _find_module(name, path=None): |
68 | 69 | # Some special cases: |
69 | 70 |
|
70 | 71 | if spec.loader is importlib.machinery.BuiltinImporter: |
71 | | - return None, None, ("", "", _C_BUILTIN) |
| 72 | + return None, None, ("", _C_BUILTIN) |
72 | 73 |
|
73 | 74 | if spec.loader is importlib.machinery.FrozenImporter: |
74 | | - return None, None, ("", "", _PY_FROZEN) |
| 75 | + return None, None, ("", _PY_FROZEN) |
75 | 76 |
|
76 | 77 | file_path = spec.origin |
77 | 78 |
|
78 | 79 | if spec.loader.is_package(name): |
79 | | - return None, os.path.dirname(file_path), ("", "", _PKG_DIRECTORY) |
| 80 | + return None, os.path.dirname(file_path), ("", _PKG_DIRECTORY) |
80 | 81 |
|
81 | 82 | if isinstance(spec.loader, importlib.machinery.SourceFileLoader): |
82 | 83 | kind = _PY_SOURCE |
83 | | - mode = "r" |
84 | 84 |
|
85 | 85 | elif isinstance(spec.loader, importlib.machinery.ExtensionFileLoader): |
86 | 86 | kind = _C_EXTENSION |
87 | | - mode = "rb" |
88 | 87 |
|
89 | 88 | elif isinstance(spec.loader, importlib.machinery.SourcelessFileLoader): |
90 | 89 | kind = _PY_COMPILED |
91 | | - mode = "rb" |
92 | 90 |
|
93 | 91 | else: # Should never happen. |
94 | | - return None, None, ("", "", _SEARCH_ERROR) |
| 92 | + return None, None, ("", _SEARCH_ERROR) |
95 | 93 |
|
96 | | - file = open(file_path, mode) |
| 94 | + file = io.open_code(file_path) |
97 | 95 | suffix = os.path.splitext(file_path)[-1] |
98 | 96 |
|
99 | | - return file, file_path, (suffix, mode, kind) |
| 97 | + return file, file_path, (suffix, kind) |
100 | 98 |
|
101 | 99 |
|
102 | 100 | class Module: |
@@ -160,15 +158,15 @@ def msgout(self, *args): |
160 | 158 |
|
161 | 159 | def run_script(self, pathname): |
162 | 160 | self.msg(2, "run_script", pathname) |
163 | | - with open(pathname) as fp: |
164 | | - stuff = ("", "r", _PY_SOURCE) |
| 161 | + with io.open_code(pathname) as fp: |
| 162 | + stuff = ("", _PY_SOURCE) |
165 | 163 | self.load_module('__main__', fp, pathname, stuff) |
166 | 164 |
|
167 | 165 | def load_file(self, pathname): |
168 | 166 | dir, name = os.path.split(pathname) |
169 | 167 | name, ext = os.path.splitext(name) |
170 | | - with open(pathname) as fp: |
171 | | - stuff = (ext, "r", _PY_SOURCE) |
| 168 | + with io.open_code(pathname) as fp: |
| 169 | + stuff = (ext, _PY_SOURCE) |
172 | 170 | self.load_module(name, fp, pathname, stuff) |
173 | 171 |
|
174 | 172 | def import_hook(self, name, caller=None, fromlist=None, level=-1): |
@@ -333,14 +331,14 @@ def import_module(self, partname, fqname, parent): |
333 | 331 | return m |
334 | 332 |
|
335 | 333 | def load_module(self, fqname, fp, pathname, file_info): |
336 | | - suffix, mode, type = file_info |
| 334 | + suffix, type = file_info |
337 | 335 | self.msgin(2, "load_module", fqname, fp and "fp", pathname) |
338 | 336 | if type == _PKG_DIRECTORY: |
339 | 337 | m = self.load_package(fqname, pathname) |
340 | 338 | self.msgout(2, "load_module ->", m) |
341 | 339 | return m |
342 | 340 | if type == _PY_SOURCE: |
343 | | - co = compile(fp.read()+'\n', pathname, 'exec') |
| 341 | + co = compile(fp.read()+b'\n', pathname, 'exec') |
344 | 342 | elif type == _PY_COMPILED: |
345 | 343 | try: |
346 | 344 | data = fp.read() |
@@ -504,7 +502,7 @@ def find_module(self, name, path, parent=None): |
504 | 502 |
|
505 | 503 | if path is None: |
506 | 504 | if name in sys.builtin_module_names: |
507 | | - return (None, None, ("", "", _C_BUILTIN)) |
| 505 | + return (None, None, ("", _C_BUILTIN)) |
508 | 506 |
|
509 | 507 | path = self.path |
510 | 508 |
|
|
0 commit comments