| 1 |
from rpython.rtyper.lltypesystem import rffi |
| 2 |
from .apiset import API |
| 3 |
|
| 4 |
@API.func("HPy HPyImport_ImportModule(HPyContext *ctx, const char *name)") |
| 5 |
def HPyImport_ImportModule(space, handles, ctx, name): |
| 6 |
w_name = space.newtext(rffi.constcharp2str(name)) |
| 7 |
w_module = import_name(space, w_name) |
| 8 |
return handles.new(w_module) |
| 9 |
|
| 10 |
def import_name(space, w_name): |
| 11 |
caller = space.getexecutioncontext().gettopframe_nohidden() |
| 12 |
|
| 13 |
if caller is not None: |
| 14 |
w_globals = caller.get_w_globals() |
| 15 |
w_builtin = space.getitem(w_globals, space.newtext('__builtins__')) |
| 16 |
else: |
| 17 |
|
| 18 |
w_builtin = space.getbuiltinmodule('builtins') |
| 19 |
w_globals = space.newdict() |
| 20 |
space.setitem(w_globals, space.newtext("__builtins__"), w_builtin) |
| 21 |
|
| 22 |
|
| 23 |
if space.isinstance_w(w_builtin, space.w_dict): |
| 24 |
w_import = space.getitem(w_builtin, space.newtext("__import__")) |
| 25 |
else: |
| 26 |
w_import = space.getattr(w_builtin, space.newtext("__import__")) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
w_module = space.call_function( |
| 31 |
w_import, w_name, w_globals, w_globals, |
| 32 |
space.newlist([space.newtext("__doc__")])) |
| 33 |
return w_module |