Debian Codegraph @

pypy/module/_hpy_universal/interp_module.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype, rffi
2 from rpython.rlib.objectmodel import specialize
3 from rpython.rlib.rarithmetic import widen
4 from pypy.interpreter.error import oefmt
5 from pypy.interpreter.executioncontext import ExecutionContext
6 from pypy.interpreter.module import Module, init_extra_module_attrs
7 from pypy.module._hpy_universal.apiset import API, DEBUG
8 from pypy.module._hpy_universal import interp_extfunc, llapi
9 from pypy.module._hpy_universal.state import State
10 from pypy.module._hpy_universal.interp_cpy_compat import attach_legacy_methods
11
12 NON_DEFAULT_MESSAGE = ("This is not allowed because custom "
13 "HPy_mod_create slot cannot return a builtin module "
14 "object and cannot make any use of any other data "
15 "defined in the HPyModuleDef. Either do not define "
16 "HPy_mod_create slot and let the runtime create a builtin "
17 "module object from the provided HPyModuleDef, or do not define "
18 "anything else but the HPy_mod_create slot.")
19
20 @specialize.arg(0)
21 def hpymod_create(handles, modname, hpydef):
22 space = handles.space
23 w_mod = Module(space, space.newtext(modname))
24 kinds = llapi.cts.gettype("HPyDef_Kind")
25 slots = llapi.cts.gettype("HPySlot_Slot")
26 #
27 if hpydef.c_size < 0:
28 raise oefmt(space.w_SystemError,
29 "HPy does not permit HPyModuleDef.size < 0")
30 elif hpydef.c_size > 0:
31 raise oefmt(space.w_SystemError,
32 "Module state is not supported yet in HPy, set "
33 "HPyModuleDef.size = 0 if module state is not needed")
34 # add the functions defined in hpydef.c_legacy_methods
35 if hpydef.c_legacy_methods:
36 if space.config.objspace.hpy_cpyext_API:
37 pymethods = rffi.cast(rffi.VOIDP, hpydef.c_legacy_methods)
38 attach_legacy_methods(space, pymethods, w_mod, modname, None)
39 else:
40 raise oefmt(space.w_RuntimeError,
41 "Module %s contains legacy methods, but _hpy_universal "
42 "was compiled without cpyext support", modname)
43 #
44 # add the native HPy module-level defines
45 if hpydef.c_defines:
46 create_func = llapi.cts.cast("HPySlot *", 0)
47 found_non_create = False
48 p = hpydef.c_defines
49 i = 0
50 while p[i]:
51 # hpy native methods
52 kind = widen(p[i].c_kind)
53 if kind == kinds.HPyDef_Kind_Slot:
54 hpyslot = llapi.cts.cast("HPySlot *", p[i].c_meth)
55 slot_num = rffi.cast(lltype.Signed, hpyslot.c_slot)
56 if slot_num == slots.HPy_mod_create:
57 if create_func:
58 raise oefmt(space.w_SystemError,
59 "Multiple definitions of the HPy_mod_create "
60 "slot in HPyModuleDef.defines.")
61 create_func = hpyslot
62 elif slot_num != slots.HPy_mod_exec:
63 raise oefmt(space.w_SystemError,
64 "Unsupported slot in HPyModuleDef.defines (value: %d).",
65 slot_num)
66 else:
67 found_non_create = True
68 else:
69 hpymeth = p[i].c_meth
70 name = rffi.constcharp2str(hpymeth.c_name)
71 sig = rffi.cast(lltype.Signed, hpymeth.c_signature)
72 doc = get_doc(hpymeth.c_doc)
73 w_extfunc = handles.w_ExtensionFunction(
74 space, handles, name, sig, doc, hpymeth.c_impl, w_mod)
75 space.setattr(w_mod, space.newtext(w_extfunc.name), w_extfunc)
76 found_non_create = True
77 i += 1
78 if create_func:
79 if found_non_create:
80 raise oefmt(space.w_SystemError,
81 "HPyModuleDef defines a HPy_mod_create slot and some "
82 "other slots or methods. %s", NON_DEFAULT_MESSAGE)
83 if (hpydef.c_legacy_methods or hpydef.c_size > 0 or
84 hpydef.c_doc or hpydef.c_globals):
85 raise oefmt(space.w_SystemError,
86 "HPyModuleDef defines a HPy_mod_create slot and some "
87 "of the other fields are not set to their default "
88 "value. %s", NON_DEFAULT_MESSAGE)
89 # fast-call directly to W_ExtensionFunctionMixin.call_o
90 func = llapi.cts.cast("HPyFunc_o", create_func.c_impl)
91 with handles.using(w_mod) as h_arg:
92 h = func(handles.get_ctx(), h_arg, h_arg)
93 if h == 0:
94 space.fromcache(State).raise_current_exception()
95 w_result = handles.consume(h)
96 if w_result and isinstance(w_result, type(w_mod)):
97 raise oefmt(space.w_SystemError,
98 "HPy_mod_create slot returned a builtin module object. "
99 "This is currently not supported.")
100 # Throw away the w_mod and return the result of mod_create()
101 return w_result
102 if hpydef.c_doc:
103 w_doc = space.newtext(rffi.constcharp2str(hpydef.c_doc))
104 else:
105 w_doc = space.w_None
106 space.setattr(w_mod, space.newtext('__doc__'), w_doc)
107 space.setattr(w_mod, space.newtext('__file__'), space.w_None)
108 init_extra_module_attrs(space, w_mod)
109 return w_mod
110
111 @specialize.arg(0)
112 def hpymod_exec_def(handles, w_mod, hpydef):
113 """ Traverse the hpydef, and execute any HPy_mod_exec slot
114 """
115 kinds = llapi.cts.gettype("HPyDef_Kind")
116 slots = llapi.cts.gettype("HPySlot_Slot")
117 space = handles.space
118 if hpydef.c_defines:
119 p = hpydef.c_defines
120 i = 0
121 while p[i]:
122 # hpy native methods
123 kind = widen(p[i].c_kind)
124 if kind == kinds.HPyDef_Kind_Slot:
125 hpyslot = llapi.cts.cast("HPySlot *", p[i].c_meth)
126 slot_num = rffi.cast(lltype.Signed, hpyslot.c_slot)
127 if slot_num == slots.HPy_mod_exec:
128 # fast-call directly to W_ExtensionFunctionMixin.call_o
129 func = llapi.cts.cast("HPyFunc_o", hpyslot.c_impl)
130 with handles.using(w_mod) as h_arg:
131 result = func(handles.get_ctx(), h_arg, h_arg)
132 if result != 0:
133 space.fromcache(State).raise_current_exception()
134 i += 1
135
136 def get_doc(c_doc):
137 if not c_doc:
138 return None
139 return rffi.constcharp2str(c_doc)
140
141 @API.func("HPy HPyGlobal_Load(HPyContext *ctx, HPyGlobal global)")
142 def HPyGlobal_Load(space, handles, ctx, h_global):
143 state = State.get(space)
144 d_globals = state.global_handles
145 if h_global not in d_globals:
146 if h_global:
147 w_global = handles.deref(h_global)
148 w_s = space.repr(w_global)
149 s = space.text_w(w_s)
150 else:
151 s = "<None>"
152 raise oefmt(space.w_ValueError, "unknown HPyGlobal* in HPyGlobal_Load (%s)", s)
153 return handles.new(d_globals[h_global])
154
155 @API.func("void HPyGlobal_Store(HPyContext *ctx, HPyGlobal *global, HPy h)")
156 def HPyGlobal_Store(space, handles, ctx, p_global, h_obj):
157 if h_obj:
158 w_obj = handles.deref(h_obj)
159 else:
160 w_obj = space.w_None
161 state = State.get(space)
162 d_globals = state.global_handles
163 # Release a potential already existing p_global[0]
164 if p_global[0] in d_globals:
165 d_globals.pop(p_global[0])
166 h_new = handles.new(w_obj)
167 d_globals[h_new] = w_obj
168 p_global[0] = h_new