| 1 |
import os |
| 2 |
from rpython.rlib.objectmodel import we_are_translated |
| 3 |
from pypy.interpreter.mixedmodule import MixedModule |
| 4 |
from pypy.module.sys import initpath |
| 5 |
from pypy.module._frozen_importlib import interp_import |
| 6 |
|
| 7 |
lib_python = os.path.join(os.path.dirname(__file__), |
| 8 |
'..', '..', '..', 'lib-python', '3') |
| 9 |
|
| 10 |
class Module(MixedModule): |
| 11 |
interpleveldefs = { |
| 12 |
} |
| 13 |
|
| 14 |
appleveldefs = { |
| 15 |
} |
| 16 |
|
| 17 |
@staticmethod |
| 18 |
def _compile_bootstrap_module(space, name, w_name, w_dict, directory="importlib"): |
| 19 |
"""NOT_RPYTHON""" |
| 20 |
with open(os.path.join(lib_python, directory, name + '.py')) as fp: |
| 21 |
source = fp.read() |
| 22 |
pathname = "<frozen %s>" % (directory + "." + name).lstrip(".") |
| 23 |
code_w = Module._cached_compile(space, name, source, |
| 24 |
pathname, 'exec', 0) |
| 25 |
space.setitem(w_dict, space.wrap('__name__'), w_name) |
| 26 |
space.setitem(w_dict, space.wrap('__origname__'), w_name) |
| 27 |
space.setitem(w_dict, space.wrap('__builtins__'), |
| 28 |
space.wrap(space.builtin)) |
| 29 |
code_w.exec_code(space, w_dict, w_dict) |
| 30 |
|
| 31 |
def install(self): |
| 32 |
"""NOT_RPYTHON""" |
| 33 |
from pypy.module.imp import interp_imp |
| 34 |
|
| 35 |
super(Module, self).install() |
| 36 |
space = self.space |
| 37 |
|
| 38 |
w_mod = Module(space, space.wrap("_frozen_importlib_external")) |
| 39 |
|
| 40 |
space.setattr(w_mod, space.wrap('MAGIC_NUMBER'), |
| 41 |
interp_imp.get_magic(space)) |
| 42 |
self._compile_bootstrap_module( |
| 43 |
space, '_bootstrap_external', w_mod.w_name, w_mod.w_dict) |
| 44 |
space.sys.setmodule(w_mod) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
self._compile_bootstrap_module( |
| 49 |
space, '_bootstrap', self.w_name, self.w_dict) |
| 50 |
|
| 51 |
self.w_import = space.wrap(interp_import.interp___import__) |
| 52 |
|
| 53 |
@staticmethod |
| 54 |
def _cached_compile(space, name, source, *args): |
| 55 |
from rpython.config.translationoption import CACHE_DIR |
| 56 |
from pypy.module.marshal import interp_marshal |
| 57 |
from pypy.interpreter.pycode import default_magic |
| 58 |
|
| 59 |
cachename = os.path.join(CACHE_DIR, 'frozen_importlib_%d%s' % ( |
| 60 |
default_magic, name)) |
| 61 |
try: |
| 62 |
if space.config.translating: |
| 63 |
raise IOError("don't use the cache when translating pypy") |
| 64 |
with open(cachename, 'rb') as f: |
| 65 |
previous = f.read(len(source) + 1) |
| 66 |
if previous != source + '\x00': |
| 67 |
raise IOError("source changed") |
| 68 |
w_bin = space.newbytes(f.read()) |
| 69 |
code_w = interp_marshal.loads(space, w_bin) |
| 70 |
except IOError: |
| 71 |
|
| 72 |
ec = space.getexecutioncontext() |
| 73 |
code_w = ec.compiler.compile(source, *args) |
| 74 |
w_bin = interp_marshal.dumps( |
| 75 |
space, code_w) |
| 76 |
content = source + '\x00' + space.bytes_w(w_bin) |
| 77 |
with open(cachename, 'wb') as f: |
| 78 |
f.write(content) |
| 79 |
return code_w |
| 80 |
|
| 81 |
def startup(self, space): |
| 82 |
"""Copy our __import__ to builtins.""" |
| 83 |
if not we_are_translated(): |
| 84 |
self.startup_at_translation_time_only(space) |
| 85 |
|
| 86 |
self.space.builtin.setdictvalue_dont_introduce_cell( |
| 87 |
'__import__', self.w_import) |
| 88 |
|
| 89 |
def startup_at_translation_time_only(self, space): |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
w_install = self.getdictvalue(space, '_install') |
| 96 |
space.call_function(w_install, |
| 97 |
space.getbuiltinmodule('sys'), |
| 98 |
space.getbuiltinmodule('_imp')) |
| 99 |
w_install_external = self.getdictvalue( |
| 100 |
space, '_install_external_importers') |
| 101 |
space.call_function(w_install_external) |