Debian Codegraph @

pypy/module/_hpy_universal/interp_extfunc.py

raw symbols SCIP
1 from rpython.rlib.objectmodel import import_from_mixin, specialize
2 from rpython.rtyper.lltypesystem import lltype, rffi
3 from pypy.interpreter.error import oefmt
4 from pypy.interpreter.baseobjspace import W_Root
5 from pypy.interpreter.typedef import TypeDef, interp_attrproperty, GetSetProperty
6 from pypy.interpreter.gateway import (
7 interp2app, interpindirect2app, unwrap_spec, descr_function_get)
8 from pypy.objspace.std.typeobject import W_TypeObject
9
10 from pypy.module._hpy_universal import llapi
11 from pypy.module._hpy_universal.state import State
12
13 SUPPORTED_SIGNATURES = (
14 llapi.HPyFunc_VARARGS,
15 llapi.HPyFunc_KEYWORDS,
16 llapi.HPyFunc_NOARGS,
17 llapi.HPyFunc_O
18 )
19
20 class W_AbstractExtensionFunction(W_Root):
21 _immutable_fields_ = ["sig", "name", "doc"]
22 # XXX: should we have separate classes for each sig?
23
24 def descr_call(self, space, __args__):
25 raise NotImplementedError
26
27 def fget_module(self, space):
28 if self.w_module is None:
29 return space.w_None
30 return self.w_module
31
32 def fset_module(self, space, w_module):
33 self.w_module = w_module
34
35 def fdel_module(self, space):
36 self.w_module = space.w_None
37
38 def get_txtsig(self, space):
39 from pypy.objspace.std.typeobject import extract_txtsig
40 rawdoc = self.doc
41 if rawdoc:
42 txtsig = extract_txtsig(rawdoc, self.name)
43 if txtsig is not None:
44 return space.newtext(txtsig)
45 return space.w_None
46
47 def get_doc(self, space):
48 from pypy.objspace.std.typeobject import extract_doc
49 rawdoc = self.doc
50 if rawdoc:
51 doc = extract_doc(rawdoc, self.name)
52 if doc is not None:
53 return space.newtext(doc)
54 return space.w_None
55
56 def get_name(self, space):
57 return space.newtext(self.name)
58
59
60 class W_ExtensionFunctionMixin(object):
61 _immutable_fields_ = ["sig", "name"]
62
63 @specialize.arg(2)
64 def __init__(self, space, handles, name, sig, doc, cfuncptr, w_self):
65 self.handles = handles
66 self.w_self = w_self
67 self.name = name
68 # W_PyCFunctionObject accepts an additional type_name
69 self.qualname = self.name
70 # W_PyCFunctionObject accepts an additional w_module
71 self.w_module = None
72 self.sig = sig
73 if self.sig not in SUPPORTED_SIGNATURES:
74 raise oefmt(space.w_ValueError, "Unsupported HPyMeth signature")
75 #raise oefmt(space.w_ValueError, "Unsupported HPyMeth.signature: %d", self.sig)
76 self.doc = doc
77 self.cfuncptr = cfuncptr
78
79 def call_noargs(self, space, h_self):
80 func = llapi.cts.cast('HPyFunc_noargs', self.cfuncptr)
81 next_ctx = self.handles.debug_before_call()
82 h_result = func(next_ctx, h_self)
83 self.handles.debug_after_call(next_ctx)
84 if not h_result:
85 space.fromcache(State).raise_current_exception()
86 return self.handles.consume(h_result)
87
88 def call_o(self, space, h_self, w_arg):
89 with self.handles.using(w_arg) as h_arg:
90 func = llapi.cts.cast('HPyFunc_o', self.cfuncptr)
91 next_ctx = self.handles.debug_before_call()
92 h_result = func(next_ctx, h_self, h_arg)
93 self.handles.debug_after_call(next_ctx)
94 if not h_result:
95 space.fromcache(State).raise_current_exception()
96 return self.handles.consume(h_result)
97
98 def call_varargs_kw(self, space, h_self, __args__, skip_args, has_keywords):
99 # this function is more or less the equivalent of
100 # ctx_CallRealFunctionFromTrampoline in cpython-universal
101 n = n_args = len(__args__.arguments_w) - skip_args
102 if has_keywords and __args__.keyword_names_w:
103 n += len(__args__.keyword_names_w)
104
105 # XXX this looks inefficient: ideally, we would like the equivalent of
106 # alloca(): do we have it in RPython? The alternative is to wrap
107 # arguments_w in a tuple, convert to handle and pass it to a C
108 # function whichs calls alloca() and the forwards everything to the
109 # functpr
110 with lltype.scoped_alloc(rffi.CArray(llapi.HPy), n) as args_h:
111 i = 0
112 k = 0
113 while i < n_args:
114 args_h[i] = self.handles.new(__args__.arguments_w[i + skip_args])
115 i += 1
116 if has_keywords:
117 while i < n:
118 args_h[i] = self.handles.new(__args__.keywords_w[k])
119 i += 1
120 k += 1
121 try:
122 if has_keywords:
123 if k > 0:
124 w_kw = space.newtuple(__args__.keyword_names_w)
125 h_kw = self.handles.new(w_kw)
126 try:
127 h_result = self.call_keywords(space, h_self, args_h, n_args, h_kw)
128 finally:
129 self.handles.close(h_kw)
130 else:
131 h_result = self.call_keywords(space, h_self, args_h, n)
132 else:
133 h_result = self.call_varargs(space, h_self, args_h, n)
134 finally:
135 for i in range(n):
136 self.handles.close(args_h[i])
137
138 if not h_result:
139 space.fromcache(State).raise_current_exception()
140 return self.handles.consume(h_result)
141
142 def call_varargs(self, space, h_self, args_h, n):
143 fptr = llapi.cts.cast('HPyFunc_varargs', self.cfuncptr)
144 next_ctx = self.handles.debug_before_call()
145 result = fptr(next_ctx, h_self, args_h, n)
146 self.handles.debug_after_call(next_ctx)
147 return result
148
149 def call_keywords(self, space, h_self, args_h, n, h_kw=0):
150 # XXX: if there are no keywords, should we pass HPy_NULL or an empty
151 # dict?
152 fptr = llapi.cts.cast('HPyFunc_keywords', self.cfuncptr)
153 next_ctx = self.handles.debug_before_call()
154 result = fptr(next_ctx, h_self, args_h, n, h_kw)
155 self.handles.debug_after_call(next_ctx)
156 return result
157
158 def descr_call(self, space, __args__):
159 with self.handles.using(self.w_self) as h_self:
160 return self.call(space, h_self, __args__)
161
162 def call(self, space, h_self, __args__, skip_args=0):
163 sig = self.sig
164 length = len(__args__.arguments_w) - skip_args
165
166 if sig == llapi.HPyFunc_KEYWORDS:
167 return self.call_varargs_kw(space, h_self, __args__, skip_args, has_keywords=True)
168
169 if __args__.keyword_names_w:
170 raise oefmt(space.w_TypeError,
171 "%s() takes no keyword arguments", self.name)
172
173 if sig == llapi.HPyFunc_NOARGS:
174 if length == 0:
175 return self.call_noargs(space, h_self)
176 raise oefmt(space.w_TypeError,
177 "%s() takes no arguments", self.name)
178
179 if sig == llapi.HPyFunc_O:
180 if length != 1:
181 raise oefmt(space.w_TypeError,
182 "%s() takes exactly one argument (%d given)",
183 self.name, length)
184 return self.call_o(space, h_self, __args__.arguments_w[skip_args])
185
186 if sig == llapi.HPyFunc_VARARGS:
187 return self.call_varargs_kw(space, h_self, __args__, skip_args, has_keywords=False)
188 else: # shouldn't happen!
189 raise oefmt(space.w_RuntimeError, "unknown calling convention")
190
191 class W_ExtensionFunction_u(W_AbstractExtensionFunction):
192 import_from_mixin(W_ExtensionFunctionMixin)
193
194 class W_ExtensionFunction_d(W_AbstractExtensionFunction):
195 import_from_mixin(W_ExtensionFunctionMixin)
196
197 class W_ExtensionFunction_t(W_AbstractExtensionFunction):
198 import_from_mixin(W_ExtensionFunctionMixin)
199
200
201 W_AbstractExtensionFunction.typedef = TypeDef(
202 'extension_function',
203 __call__ = interpindirect2app(W_AbstractExtensionFunction.descr_call),
204 __doc__ = GetSetProperty(W_AbstractExtensionFunction.get_doc),
205 __name__ = GetSetProperty(W_AbstractExtensionFunction.get_name),
206 __text_signature__ = GetSetProperty(W_AbstractExtensionFunction.get_txtsig),
207 __module__ = GetSetProperty(W_AbstractExtensionFunction.fget_module,
208 W_AbstractExtensionFunction.fset_module,
209 W_AbstractExtensionFunction.fdel_module),
210 __qualname__ = interp_attrproperty('qualname',
211 cls=W_AbstractExtensionFunction, wrapfn="newtext_or_none"),
212 )
213 W_AbstractExtensionFunction.typedef.acceptable_as_base_class = False
214
215 class W_AbstractExtensionMethod(W_Root):
216 def descr_call(self, space, __args__):
217 raise NotImplementedError
218
219 class W_ExtensionMethodMixin(object):
220 import_from_mixin(W_ExtensionFunctionMixin)
221 def __init__(self, space, handles, name, sig, doc, cfuncptr, w_objclass):
222 W_ExtensionFunctionMixin.__init__.__func__(self, space, handles, name, sig, doc,
223 cfuncptr, space.w_None)
224 self.w_objclass = w_objclass
225
226 def descr_call(self, space, __args__):
227 # XXX: basically a copy of cpyext's W_PyCMethodObject.descr_call()
228 if len(__args__.arguments_w) == 0:
229 w_objclass = self.w_objclass
230 assert isinstance(w_objclass, W_TypeObject)
231 raise oefmt(space.w_TypeError,
232 "descriptor '%8' of '%s' object needs an argument",
233 self.name, self.w_objclass.getname(space))
234 w_instance = __args__.arguments_w[0]
235 # XXX: needs a stricter test
236 if not space.isinstance_w(w_instance, self.w_objclass):
237 w_objclass = self.w_objclass
238 assert isinstance(w_objclass, W_TypeObject)
239 raise oefmt(space.w_TypeError,
240 "descriptor '%8' requires a '%s' object but received a '%T'",
241 self.name, w_objclass.name, w_instance)
242 #
243 with self.handles.using(w_instance) as h_instance:
244 return self.call(space, h_instance, __args__, skip_args=1)
245
246 class W_ExtensionMethod_u(W_AbstractExtensionMethod):
247 import_from_mixin(W_ExtensionMethodMixin)
248
249 class W_ExtensionMethod_d(W_AbstractExtensionMethod):
250 import_from_mixin(W_ExtensionMethodMixin)
251
252 class W_ExtensionMethod_t(W_AbstractExtensionMethod):
253 import_from_mixin(W_ExtensionMethodMixin)
254
255 W_AbstractExtensionMethod.typedef = TypeDef(
256 'method_descriptor_',
257 __get__ = interp2app(descr_function_get),
258 __call__ = interpindirect2app(W_AbstractExtensionMethod.descr_call),
259 __doc__ = interp_attrproperty('doc', cls=W_AbstractExtensionMethod,
260 wrapfn="newtext_or_none"),
261 )
262 W_AbstractExtensionMethod.typedef.acceptable_as_base_class = False