| 1 |
from __future__ import with_statement |
| 2 |
import pytest |
| 3 |
from rpython.tool.udir import udir |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class AppTestImpModule: |
| 8 |
|
| 9 |
|
| 10 |
spaceconfig = { |
| 11 |
'usemodules': ['binascii', 'imp', 'time', 'struct', |
| 12 |
'_cffi_backend'], |
| 13 |
} |
| 14 |
|
| 15 |
def setup_class(cls): |
| 16 |
cls.w_file_module = cls.space.wrap(__file__) |
| 17 |
latin1 = udir.join('latin1.py') |
| 18 |
latin1.write("# -*- coding: iso-8859-1 -*\n") |
| 19 |
fake_latin1 = udir.join('fake_latin1.py') |
| 20 |
fake_latin1.write("print('-*- coding: iso-8859-1 -*')") |
| 21 |
cls.w_udir = cls.space.wrap(str(udir)) |
| 22 |
|
| 23 |
def w__py_file(self): |
| 24 |
fname = self.udir + '/@TEST.py' |
| 25 |
f = open(fname, 'w') |
| 26 |
f.write('MARKER = 42\n') |
| 27 |
f.close() |
| 28 |
return fname |
| 29 |
|
| 30 |
def w__pyc_file(self): |
| 31 |
import marshal, imp |
| 32 |
co = compile("marker=42", "x.py", "exec") |
| 33 |
fname = self.udir + '/@TEST.pyc' |
| 34 |
f = open(fname, 'wb') |
| 35 |
f.write(imp.get_magic()) |
| 36 |
f.write(b'\x00\x00\x00\x00') |
| 37 |
f.write(b'\x00\x00\x00\x00') |
| 38 |
f.write(b'\x00\x00\x00\x00') |
| 39 |
marshal.dump(co, f) |
| 40 |
f.close() |
| 41 |
return fname |
| 42 |
|
| 43 |
def test_find_module(self): |
| 44 |
import os, imp |
| 45 |
file, pathname, description = imp.find_module('cmd') |
| 46 |
assert file is not None |
| 47 |
file.close() |
| 48 |
assert os.path.exists(pathname) |
| 49 |
pathname = pathname.lower() |
| 50 |
assert pathname.endswith('.py') |
| 51 |
assert description in imp.get_suffixes() |
| 52 |
|
| 53 |
def test_find_module_with_encoding(self): |
| 54 |
import sys, imp |
| 55 |
sys.path.insert(0, self.udir) |
| 56 |
try: |
| 57 |
file, pathname, description = imp.find_module('latin1') |
| 58 |
assert file.encoding == 'iso-8859-1' |
| 59 |
|
| 60 |
file, pathname, description = imp.find_module('fake_latin1') |
| 61 |
assert file.encoding == 'utf-8' |
| 62 |
finally: |
| 63 |
del sys.path[0] |
| 64 |
|
| 65 |
def test_create_dynamic(self): |
| 66 |
import _imp |
| 67 |
PATH = 'this/path/does/not/exist' |
| 68 |
class FakeSpec: |
| 69 |
origin = PATH |
| 70 |
def __init__(self, name): |
| 71 |
self.name = name |
| 72 |
|
| 73 |
excinfo = raises(ImportError, _imp.create_dynamic, FakeSpec('foo')) |
| 74 |
assert excinfo.value.name == 'foo' |
| 75 |
assert excinfo.value.path == PATH |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
raises(TypeError, _imp.create_dynamic, FakeSpec(b'foo')) |
| 84 |
|
| 85 |
def test_suffixes(self): |
| 86 |
import imp |
| 87 |
for suffix, mode, type in imp.get_suffixes(): |
| 88 |
if type == imp.PY_SOURCE: |
| 89 |
assert suffix in ('.py', '.pyw') |
| 90 |
assert mode == 'r' |
| 91 |
elif type == imp.PY_COMPILED: |
| 92 |
assert suffix == '.pyc' |
| 93 |
assert mode == 'rb' |
| 94 |
elif type == imp.C_EXTENSION: |
| 95 |
assert suffix.endswith(('.pyd', '.so')) |
| 96 |
assert mode == 'rb' |
| 97 |
else: |
| 98 |
assert False, ("Unknown type", suffix, mode, type) |
| 99 |
|
| 100 |
def test_ext_suffixes(self): |
| 101 |
import _imp |
| 102 |
for suffix in _imp.extension_suffixes(): |
| 103 |
|
| 104 |
assert suffix.endswith(('.pyd', '.so')) |
| 105 |
|
| 106 |
def test_obscure_functions(self): |
| 107 |
import imp |
| 108 |
mod = imp.new_module('hi') |
| 109 |
assert mod.__name__ == 'hi' |
| 110 |
mod = imp.init_builtin('hello.world.this.is.never.a.builtin.module.name') |
| 111 |
assert mod is None |
| 112 |
mod = imp.init_frozen('hello.world.this.is.never.a.frozen.module.name') |
| 113 |
assert mod is None |
| 114 |
assert imp.is_builtin('sys') |
| 115 |
assert not imp.is_builtin('hello.world.this.is.never.a.builtin.module.name') |
| 116 |
assert not imp.is_frozen('hello.world.this.is.never.a.frozen.module.name') |
| 117 |
|
| 118 |
def test_find_frozen(self): |
| 119 |
import _imp |
| 120 |
assert _imp.find_frozen('hello.what.now') is None |
| 121 |
|
| 122 |
def test_is_builtin(self): |
| 123 |
import sys, imp |
| 124 |
for name in sys.builtin_module_names: |
| 125 |
assert imp.is_builtin(name) |
| 126 |
mod = imp.init_builtin(name) |
| 127 |
assert mod |
| 128 |
assert mod.__spec__ |
| 129 |
test_is_builtin.dont_track_allocations = True |
| 130 |
|
| 131 |
def test_load_module_py(self): |
| 132 |
import imp |
| 133 |
fn = self._py_file() |
| 134 |
descr = ('.py', 'r', imp.PY_SOURCE) |
| 135 |
f = open(fn, 'r') |
| 136 |
mod = imp.load_module('test_imp_extra_AUTO1', f, fn, descr) |
| 137 |
f.close() |
| 138 |
assert mod.MARKER == 42 |
| 139 |
import test_imp_extra_AUTO1 |
| 140 |
assert mod is test_imp_extra_AUTO1 |
| 141 |
|
| 142 |
def test_load_module_pyc_1(self): |
| 143 |
import os, imp |
| 144 |
fn = self._pyc_file() |
| 145 |
try: |
| 146 |
descr = ('.pyc', 'rb', imp.PY_COMPILED) |
| 147 |
f = open(fn, 'rb') |
| 148 |
mod = imp.load_module('test_imp_extra_AUTO2', f, fn, descr) |
| 149 |
f.close() |
| 150 |
assert mod.marker == 42 |
| 151 |
import test_imp_extra_AUTO2 |
| 152 |
assert mod is test_imp_extra_AUTO2 |
| 153 |
finally: |
| 154 |
os.unlink(fn) |
| 155 |
|
| 156 |
def test_load_source(self): |
| 157 |
import imp |
| 158 |
fn = self._py_file() |
| 159 |
mod = imp.load_source('test_imp_extra_AUTO3', fn) |
| 160 |
assert mod.MARKER == 42 |
| 161 |
import test_imp_extra_AUTO3 |
| 162 |
assert mod is test_imp_extra_AUTO3 |
| 163 |
|
| 164 |
def test_load_module_pyc_2(self): |
| 165 |
import os, imp |
| 166 |
fn = self._pyc_file() |
| 167 |
try: |
| 168 |
mod = imp.load_compiled('test_imp_extra_AUTO4', fn) |
| 169 |
assert mod.marker == 42 |
| 170 |
import test_imp_extra_AUTO4 |
| 171 |
assert mod is test_imp_extra_AUTO4 |
| 172 |
finally: |
| 173 |
os.unlink(fn) |
| 174 |
|
| 175 |
def test_load_broken_pyc(self): |
| 176 |
import imp |
| 177 |
fn = self._py_file() |
| 178 |
try: |
| 179 |
imp.load_compiled('test_imp_extra_AUTO5', fn) |
| 180 |
except ImportError: |
| 181 |
pass |
| 182 |
else: |
| 183 |
raise Exception("expected an ImportError") |
| 184 |
|
| 185 |
def test_load_module_in_sys_modules(self): |
| 186 |
import imp |
| 187 |
fn = self._py_file() |
| 188 |
f = open(fn, 'rb') |
| 189 |
descr = ('.py', 'r', imp.PY_SOURCE) |
| 190 |
mod = imp.load_module('test_imp_extra_AUTO6', f, fn, descr) |
| 191 |
f.close() |
| 192 |
f = open(fn, 'rb') |
| 193 |
mod2 = imp.load_module('test_imp_extra_AUTO6', f, fn, descr) |
| 194 |
f.close() |
| 195 |
assert mod2 is mod |
| 196 |
|
| 197 |
def test_nullimporter(self): |
| 198 |
import os, imp |
| 199 |
importer = imp.NullImporter("path") |
| 200 |
assert importer.find_module(1) is None |
| 201 |
raises(ImportError, imp.NullImporter, os.getcwd()) |
| 202 |
|
| 203 |
def test_path_importer_cache(self): |
| 204 |
import os |
| 205 |
import sys |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
sys.path_importer_cache.clear() |
| 211 |
import sys |
| 212 |
|
| 213 |
|
| 214 |
lib_pypy = os.path.abspath( |
| 215 |
os.path.join(self.file_module, "..", "..", "..", "..", "..", "lib_pypy") |
| 216 |
) |
| 217 |
|
| 218 |
assert sys.path_importer_cache.get(lib_pypy) is None |
| 219 |
|
| 220 |
def test_rewrite_pyc_check_code_name(self): |
| 221 |
|
| 222 |
from os import chmod |
| 223 |
from os.path import join |
| 224 |
from sys import modules, path |
| 225 |
from shutil import rmtree |
| 226 |
from tempfile import mkdtemp |
| 227 |
code = b"""if 1: |
| 228 |
import sys |
| 229 |
code_filename = sys._getframe().f_code.co_filename |
| 230 |
module_filename = __file__ |
| 231 |
constant = 1 |
| 232 |
def func(): |
| 233 |
pass |
| 234 |
func_filename = func.__code__.co_filename |
| 235 |
""" |
| 236 |
|
| 237 |
module_name = "unlikely_module_name" |
| 238 |
dir_name = mkdtemp(prefix='pypy_test') |
| 239 |
file_name = join(dir_name, module_name + '.py') |
| 240 |
with open(file_name, "wb") as f: |
| 241 |
f.write(code) |
| 242 |
compiled_name = file_name + ("c" if __debug__ else "o") |
| 243 |
chmod(file_name, 0o777) |
| 244 |
|
| 245 |
|
| 246 |
sys_path = path[:] |
| 247 |
orig_module = modules.pop(module_name, None) |
| 248 |
assert modules.get(module_name) == None |
| 249 |
path.insert(0, dir_name) |
| 250 |
|
| 251 |
|
| 252 |
import py_compile |
| 253 |
py_compile.compile(file_name, dfile="another_module.py") |
| 254 |
__import__(module_name, globals(), locals()) |
| 255 |
mod = modules.get(module_name) |
| 256 |
|
| 257 |
try: |
| 258 |
|
| 259 |
assert mod != orig_module |
| 260 |
assert mod.module_filename == file_name |
| 261 |
assert mod.code_filename == file_name |
| 262 |
assert mod.func_filename == file_name |
| 263 |
finally: |
| 264 |
|
| 265 |
path[:] = sys_path |
| 266 |
if orig_module is not None: |
| 267 |
modules[module_name] = orig_module |
| 268 |
else: |
| 269 |
try: |
| 270 |
del modules[module_name] |
| 271 |
except KeyError: |
| 272 |
pass |
| 273 |
rmtree(dir_name, True) |
| 274 |
|
| 275 |
def test_builtin_reimport(self): |
| 276 |
|
| 277 |
import sys, marshal |
| 278 |
|
| 279 |
old = marshal.loads |
| 280 |
marshal.loads = 42 |
| 281 |
|
| 282 |
|
| 283 |
saved = sys.modules.pop('marshal') |
| 284 |
__import__('marshal') |
| 285 |
sys.modules['marshal'] = saved |
| 286 |
|
| 287 |
assert marshal.loads == 42 |
| 288 |
import marshal |
| 289 |
assert marshal.loads == 42 |
| 290 |
marshal.loads = old |
| 291 |
|
| 292 |
def test_builtin_reimport_mess(self): |
| 293 |
|
| 294 |
import sys |
| 295 |
import time as time1 |
| 296 |
|
| 297 |
old = time1.process_time |
| 298 |
try: |
| 299 |
time1.process_time = 42 |
| 300 |
|
| 301 |
|
| 302 |
saved = sys.modules.pop('time') |
| 303 |
assert time1 is saved |
| 304 |
time2 = __import__('time') |
| 305 |
assert time2 is not time1 |
| 306 |
assert time2 is sys.modules['time'] |
| 307 |
assert time2.process_time is old |
| 308 |
|
| 309 |
import time as time3 |
| 310 |
assert time3 is time2 |
| 311 |
assert time3.process_time is old |
| 312 |
|
| 313 |
sys.modules['time'] = time1 |
| 314 |
import time as time4 |
| 315 |
assert time4 is time1 |
| 316 |
assert time4.process_time == 42 |
| 317 |
finally: |
| 318 |
time1.process_time = old |
| 319 |
|
| 320 |
def test_get_tag(self): |
| 321 |
import imp |
| 322 |
import sys |
| 323 |
if not hasattr(sys, 'pypy_version_info'): |
| 324 |
skip('This test is PyPy-only') |
| 325 |
assert imp.get_tag() == 'pypy%d%d' % (sys.version_info[:2]) |
| 326 |
|
| 327 |
def test_unicode_in_sys_path(self): |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
import sys |
| 332 |
if sys.getfilesystemencoding().lower() == 'utf-8': |
| 333 |
sys.path.insert(0, u'\xef') |
| 334 |
with raises(ImportError): |
| 335 |
import impossible_module |
| 336 |
|
| 337 |
def test_source_hash(self): |
| 338 |
import _imp |
| 339 |
res = _imp.source_hash(1, b"abcdef") |
| 340 |
assert type(res) is bytes |
| 341 |
assert res == b'\xd8^\xafF=\xaain' |
| 342 |
res2 = _imp.source_hash(1, b"abcdefg") |
| 343 |
assert res != res2 |
| 344 |
|
| 345 |
def test_check_hash_based_pycs(self): |
| 346 |
import _imp |
| 347 |
assert _imp.check_hash_based_pycs == "default" |
| 348 |
|