| 1 |
from pypy.module.imp import importing |
| 2 |
from rpython.rlib import streamio |
| 3 |
from pypy.interpreter.error import oefmt |
| 4 |
from pypy.interpreter.gateway import unwrap_spec |
| 5 |
from pypy.interpreter.pycode import PyCode |
| 6 |
from pypy.module._io.interp_iobase import W_IOBase |
| 7 |
from pypy.interpreter.streamutil import wrap_streamerror |
| 8 |
from pypy.interpreter.error import OperationError |
| 9 |
|
| 10 |
|
| 11 |
def extension_suffixes(space): |
| 12 |
suffixes_w = [] |
| 13 |
so_ext = importing.get_so_extension(space) |
| 14 |
suffixes_w.append(space.newtext(so_ext)) |
| 15 |
return space.newlist(suffixes_w) |
| 16 |
|
| 17 |
def get_magic(space): |
| 18 |
x = importing.get_pyc_magic(space) |
| 19 |
a = x & 0xff |
| 20 |
x >>= 8 |
| 21 |
b = x & 0xff |
| 22 |
x >>= 8 |
| 23 |
c = x & 0xff |
| 24 |
x >>= 8 |
| 25 |
d = x & 0xff |
| 26 |
return space.newbytes(chr(a) + chr(b) + chr(c) + chr(d)) |
| 27 |
|
| 28 |
def get_tag(space): |
| 29 |
"""get_tag() -> string |
| 30 |
Return the magic tag for .pyc files.""" |
| 31 |
return space.newtext(importing.PYC_TAG) |
| 32 |
|
| 33 |
def get_file(space, w_file, filename, filemode): |
| 34 |
if space.is_none(w_file): |
| 35 |
try: |
| 36 |
return streamio.open_file_as_stream(filename, filemode) |
| 37 |
except streamio.StreamErrors as e: |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
raise wrap_streamerror(space, e) |
| 42 |
else: |
| 43 |
w_iobase = space.interp_w(W_IOBase, w_file) |
| 44 |
|
| 45 |
|
| 46 |
fd = space.int_w(space.call_method(w_iobase, 'fileno')) |
| 47 |
return streamio.fdopen_as_stream(fd, filemode) |
| 48 |
|
| 49 |
def create_dynamic(space, w_spec, w_file=None): |
| 50 |
if not importing.has_so_extension(space): |
| 51 |
raise oefmt(space.w_ImportError, "Not implemented") |
| 52 |
from pypy.module.cpyext.api import create_extension_module |
| 53 |
|
| 54 |
return create_extension_module(space, w_spec) |
| 55 |
|
| 56 |
def create_builtin(space, w_spec): |
| 57 |
w_name = space.getattr(w_spec, space.newtext("name")) |
| 58 |
name = space.text0_w(w_name) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
reuse = space.finditem(space.sys.get('modules'), w_name) is not None |
| 65 |
return space.getbuiltinmodule(name, force_init=True, reuse=reuse) |
| 66 |
|
| 67 |
def exec_dynamic(space, w_mod): |
| 68 |
from pypy.module.cpyext.api import exec_extension_module |
| 69 |
exec_extension_module(space, w_mod) |
| 70 |
|
| 71 |
def exec_builtin(space, w_mod): |
| 72 |
return |
| 73 |
|
| 74 |
def init_frozen(space, w_name): |
| 75 |
return None |
| 76 |
|
| 77 |
def is_builtin(space, w_name): |
| 78 |
try: |
| 79 |
name = space.text0_w(w_name) |
| 80 |
except OperationError: |
| 81 |
return space.newint(0) |
| 82 |
|
| 83 |
if name not in space.builtin_modules: |
| 84 |
return space.newint(0) |
| 85 |
if space.finditem(space.sys.get('modules'), w_name) is not None: |
| 86 |
return space.newint(-1) |
| 87 |
return space.newint(1) |
| 88 |
|
| 89 |
def is_frozen(space, w_name): |
| 90 |
name = space.text_w(w_name) |
| 91 |
if name in ("_frozen_importlib", "_frozen_importlib_external", "zipimport", |
| 92 |
|
| 93 |
"__hello__", "__phello__", "_phello__.spam"): |
| 94 |
return space.w_True |
| 95 |
return space.w_False |
| 96 |
|
| 97 |
def use_frozen(space): |
| 98 |
"""private function that allows never using frozen modules""" |
| 99 |
from pypy.module.imp.state import State |
| 100 |
state = space.fromcache(State) |
| 101 |
override = state.override_frozen_modules |
| 102 |
if override > 0: |
| 103 |
return True |
| 104 |
elif override < 0: |
| 105 |
return False |
| 106 |
|
| 107 |
return True |
| 108 |
|
| 109 |
|
| 110 |
def list_frozen_module_names(space): |
| 111 |
always_freeze = space.newlist([space.newtext(s) for s in [ |
| 112 |
"_frozen_importlib", "_frozen_importlib_external", "zipimport", |
| 113 |
]]) |
| 114 |
|
| 115 |
frozen = always_freeze |
| 116 |
|
| 117 |
if use_frozen(space): |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
pass |
| 123 |
return frozen |
| 124 |
|
| 125 |
def get_frozen_object(space, w_name): |
| 126 |
raise oefmt(space.w_ImportError, |
| 127 |
"No such frozen object named %R", w_name) |
| 128 |
|
| 129 |
def is_frozen_package(space, w_name): |
| 130 |
return space.w_False |
| 131 |
|
| 132 |
def find_frozen(space, w_name): |
| 133 |
return space.w_None |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
def lock_held(space): |
| 140 |
if space.config.objspace.usemodules.thread: |
| 141 |
return space.newbool(importing.getimportlock(space).lock_held_by_anyone()) |
| 142 |
else: |
| 143 |
return space.w_False |
| 144 |
|
| 145 |
def acquire_lock(space): |
| 146 |
if space.config.objspace.usemodules.thread: |
| 147 |
importing.getimportlock(space).acquire_lock() |
| 148 |
|
| 149 |
def release_lock(space): |
| 150 |
if space.config.objspace.usemodules.thread: |
| 151 |
importing.getimportlock(space).release_lock(silent_after_fork=False) |
| 152 |
|
| 153 |
def reinit_lock(space): |
| 154 |
if space.config.objspace.usemodules.thread: |
| 155 |
importing.getimportlock(space).reinit_lock() |
| 156 |
|
| 157 |
@unwrap_spec(pathname='fsencode') |
| 158 |
def fix_co_filename(space, w_code, pathname): |
| 159 |
code_w = space.interp_w(PyCode, w_code) |
| 160 |
importing.update_code_filenames(space, code_w, pathname) |
| 161 |
|
| 162 |
|
| 163 |
@unwrap_spec(magic=int, content='bytes') |
| 164 |
def source_hash(space, magic, content): |
| 165 |
from rpython.rlib.rsiphash import siphash24_with_key |
| 166 |
from rpython.rlib.rarithmetic import r_uint64 |
| 167 |
h = siphash24_with_key(content, r_uint64(magic)) |
| 168 |
res = [b"x"] * 8 |
| 169 |
for i in range(8): |
| 170 |
res[i] = chr(h & 0xff) |
| 171 |
h >>= 8 |
| 172 |
assert not h |
| 173 |
return space.newbytes(b"".join(res)) |