| 1 |
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault |
| 2 |
from pypy.interpreter.error import OperationError |
| 3 |
from pypy.interpreter.baseobjspace import SpaceCache |
| 4 |
|
| 5 |
|
| 6 |
class FrozenCache(SpaceCache): |
| 7 |
def __init__(self, space): |
| 8 |
mod = space.getbuiltinmodule('_frozen_importlib') |
| 9 |
self.w_frozen_import = mod.get('__import__') |
| 10 |
assert self.w_frozen_import is not None |
| 11 |
|
| 12 |
class FastPathGiveUp(Exception): |
| 13 |
pass |
| 14 |
|
| 15 |
def _gcd_import(space, name): |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
w_modules = space.sys.get('modules') |
| 22 |
w_module = space.finditem_str(w_modules, name) |
| 23 |
if w_module is None: |
| 24 |
raise FastPathGiveUp |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
try: |
| 29 |
w_spec = space.getattr(w_module, space.newtext("__spec__")) |
| 30 |
except OperationError as e: |
| 31 |
if not e.match(space, space.w_AttributeError): |
| 32 |
raise |
| 33 |
raise FastPathGiveUp |
| 34 |
try: |
| 35 |
w_initializing = space.getattr(w_spec, space.newtext("_initializing")) |
| 36 |
except OperationError as e: |
| 37 |
if not e.match(space, space.w_AttributeError): |
| 38 |
raise |
| 39 |
|
| 40 |
|
| 41 |
else: |
| 42 |
if space.is_true(w_initializing): |
| 43 |
raise FastPathGiveUp |
| 44 |
return w_module |
| 45 |
|
| 46 |
|
| 47 |
@unwrap_spec(w_globals=WrappedDefault(None), w_locals=WrappedDefault(None), w_fromlist=WrappedDefault(()), |
| 48 |
w_level=WrappedDefault(0)) |
| 49 |
def interp___import__(space, w_name, w_globals, w_locals, w_fromlist, |
| 50 |
w_level): |
| 51 |
""" |
| 52 |
Import a module. Because this function is meant for use by the Python |
| 53 |
interpreter and not for general use, it is better to use |
| 54 |
importlib.import_module() to programmatically import a module. |
| 55 |
|
| 56 |
The globals argument is only used to determine the context; |
| 57 |
they are not modified. The locals argument is unused. The fromlist |
| 58 |
should be a list of names to emulate ``from name import ...'', or an |
| 59 |
empty list to emulate ``import name''. |
| 60 |
When importing a module from a package, note that __import__('A.B', ...) |
| 61 |
returns package A when fromlist is empty, but its submodule B when |
| 62 |
fromlist is not empty. The level argument is used to determine whether to |
| 63 |
perform absolute or relative imports: 0 is absolute, while a positive number |
| 64 |
is the number of parent directories to search relative to the current module.""" |
| 65 |
level = space.int_w(w_level) |
| 66 |
if level == 0: |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
try: |
| 72 |
name = space.text_w(w_name) |
| 73 |
w_mod = _gcd_import(space, name) |
| 74 |
have_fromlist = space.is_true(w_fromlist) |
| 75 |
if not have_fromlist: |
| 76 |
dotindex = name.find(".") |
| 77 |
if dotindex < 0: |
| 78 |
return w_mod |
| 79 |
return _gcd_import(space, name[:dotindex]) |
| 80 |
except FastPathGiveUp: |
| 81 |
pass |
| 82 |
else: |
| 83 |
assert have_fromlist |
| 84 |
w_path = space.findattr(w_mod, space.newtext("__path__")) |
| 85 |
if w_path is not None: |
| 86 |
|
| 87 |
w_importlib = space.getbuiltinmodule('_frozen_importlib') |
| 88 |
return space.call_method(w_importlib, "_handle_fromlist", |
| 89 |
w_mod, w_fromlist, |
| 90 |
space.w_default_importlib_import) |
| 91 |
else: |
| 92 |
return w_mod |
| 93 |
try: |
| 94 |
return space.call_function( |
| 95 |
space.fromcache(FrozenCache).w_frozen_import, w_name, w_globals, w_locals, w_fromlist, |
| 96 |
w_level) |
| 97 |
except OperationError as e: |
| 98 |
e.remove_traceback_module_frames( |
| 99 |
'<frozen importlib._bootstrap>', |
| 100 |
'<frozen importlib._bootstrap_external>', |
| 101 |
'<builtin>/frozen importlib._bootstrap_external') |
| 102 |
raise |
| 103 |
interp___import__ = interp2app(interp___import__, |
| 104 |
app_name='__import__') |