Debian Codegraph @

pypy/module/_hpy_universal/interp_slot.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype, rffi
2 from rpython.rlib.rarithmetic import widen
3 from rpython.rlib.unroll import unrolling_iterable
4 from rpython.rlib.objectmodel import specialize, import_from_mixin
5 from rpython.rlib import jit
6
7 from pypy.interpreter.error import oefmt
8 from pypy.interpreter.baseobjspace import W_Root
9 from pypy.interpreter.gateway import descr_function_get
10 from pypy.interpreter.typedef import TypeDef, interp2app
11 from pypy.objspace.std.typeobject import W_TypeObject
12 from pypy.module._hpy_universal import llapi
13 from .state import State
14
15 HPySlot_Slot = llapi.cts.gettype('HPySlot_Slot')
16 HPy_RichCmpOp = llapi.cts.gettype('HPy_RichCmpOp')
17
18 _WRAPPER_CACHE = {}
19
20 class W_SlotWrapper(W_Root):
21 _immutable_fields_ = ["slot"]
22
23 def __init__(self, slot, method_name, cfuncptr, w_objclass):
24 self.slot = slot
25 self.name = method_name
26 self.cfuncptr = cfuncptr
27 self.w_objclass = w_objclass
28
29 def check_args(self, space, __args__, arity):
30 length = len(__args__.arguments_w)
31 if length != arity:
32 raise oefmt(space.w_TypeError, "expected %d arguments, got %d",
33 arity, length)
34 if __args__.keyword_names_w:
35 raise oefmt(space.w_TypeError,
36 "wrapper %s doesn't take any keyword arguments",
37 self.name)
38
39 def check_argsv(self, space, __args__, min, max):
40 length = len(__args__.arguments_w)
41 if not min <= length <= max:
42 raise oefmt(space.w_TypeError, "expected %d-%d arguments, got %d",
43 min, max, length)
44 if __args__.keyword_names_w:
45 raise oefmt(space.w_TypeError,
46 "wrapper %s doesn't take any keyword arguments",
47 self.name)
48
49 def descr_call(self, space, __args__):
50 # XXX: basically a copy of cpyext's W_PyCMethodObject.descr_call()
51 if len(__args__.arguments_w) == 0:
52 w_objclass = self.w_objclass
53 assert isinstance(w_objclass, W_TypeObject)
54 raise oefmt(space.w_TypeError,
55 "descriptor '%8' of '%s' object needs an argument",
56 self.name, self.w_objclass.getname(space))
57 w_instance = __args__.arguments_w[0]
58 # XXX: needs a stricter test
59 if not space.isinstance_w(w_instance, self.w_objclass):
60 w_objclass = self.w_objclass
61 assert isinstance(w_objclass, W_TypeObject)
62 raise oefmt(space.w_TypeError,
63 "descriptor '%8' requires a '%s' object but received a '%T'",
64 self.name, w_objclass.name, w_instance)
65 #
66 return self.call(space, __args__)
67
68 def call(self, space, __args__):
69 raise oefmt(space.w_RuntimeError, "bad slot wrapper")
70
71 W_SlotWrapper.typedef = TypeDef(
72 'slot_wrapper',
73 __get__ = interp2app(descr_function_get),
74 __call__ = interp2app(W_SlotWrapper.descr_call),
75 )
76 W_SlotWrapper.typedef.acceptable_as_base_class = False
77
78 # ~~~~~~~~~~ concrete W_SlotWrapper subclasses ~~~~~~~~~~~~~
79 # these are the equivalent of the various functions wrap_* inside CPython's typeobject.c
80
81 class W_wrap_binaryfunc(object):
82 def call(self, space, __args__):
83 func = llapi.cts.cast("HPyFunc_binaryfunc", self.cfuncptr)
84 self.check_args(space, __args__, 2)
85 w_self = __args__.arguments_w[0]
86 w_other = __args__.arguments_w[1]
87 with self.handles.using(w_self, w_other) as (h_self, h_other):
88 h_result = func(self.handles.get_ctx(), h_self, h_other)
89 if not h_result:
90 space.fromcache(State).raise_current_exception()
91 return self.handles.consume(h_result)
92
93 @specialize.memo()
94 def get_cmp_wrapper_cls(handles, methname, OP):
95 try:
96 return _WRAPPER_CACHE[handles, methname]
97 except KeyError:
98 pass
99 class wrapper(W_SlotWrapper):
100 def call(self, space, __args__):
101 func = llapi.cts.cast("HPyFunc_richcmpfunc", self.cfuncptr)
102 self.check_args(space, __args__, 2)
103 w_self = __args__.arguments_w[0]
104 w_other = __args__.arguments_w[1]
105 with handles.using(w_self, w_other) as (h_self, h_other):
106 # rffi doesn't allow casting to an enum, we need to use int
107 # instead
108 h_result = func(
109 handles.get_ctx(), h_self, h_other, rffi.cast(rffi.INT_real, OP))
110 if not h_result:
111 space.fromcache(State).raise_current_exception()
112 return handles.consume(h_result)
113 suffix = '_d' if handles.is_debug else '_u'
114 wrapper.__name__ = 'W_wrap_richcmp%s%s' % (methname, suffix)
115 _WRAPPER_CACHE[handles, methname] = wrapper
116 return wrapper
117
118 CMP_OPNAMES = ['eq', 'ne', 'lt', 'le', 'gt', 'ge']
119 CMP_ENUM_VALUES = [
120 getattr(HPy_RichCmpOp, 'HPy_%s' % opname.upper()) for opname in CMP_OPNAMES]
121 CMP_SLOTS = unrolling_iterable([
122 ('__%s__' % opname, opval)
123 for opname, opval in zip(CMP_OPNAMES, CMP_ENUM_VALUES)])
124
125 class W_wrap_voidfunc(object):
126 def call(self, space, __args__):
127 # cheat: it should be a void func here.
128 # Note this function cannot error
129 func = llapi.cts.cast("HPyFunc_unaryfunc", self.cfuncptr)
130 self.check_args(space, __args__, 1)
131 w_self = __args__.arguments_w[0]
132 with self.handles.using(w_self) as h_self:
133 h_result = func(self.handles.get_ctx(), h_self)
134
135 class W_wrap_unaryfunc(object):
136 def call(self, space, __args__):
137 func = llapi.cts.cast("HPyFunc_unaryfunc", self.cfuncptr)
138 self.check_args(space, __args__, 1)
139 w_self = __args__.arguments_w[0]
140 with self.handles.using(w_self) as h_self:
141 h_result = func(self.handles.get_ctx(), h_self)
142 if not h_result:
143 space.fromcache(State).raise_current_exception()
144 return self.handles.consume(h_result)
145
146 class W_wrap_ternaryfunc(object):
147 def call(self, space, __args__):
148 # Literaly quote of the corresponding CPython comment:
149 # Note: This wrapper only works for __pow__()
150 #
151 func = llapi.cts.cast("HPyFunc_ternaryfunc", self.cfuncptr)
152 self.check_argsv(space, __args__, 2, 3)
153 n = len(__args__.arguments_w)
154 w_self = __args__.arguments_w[0]
155 w1 = __args__.arguments_w[1]
156 if n == 2:
157 w2 = space.w_None
158 else:
159 w2 = __args__.arguments_w[2]
160 with self.handles.using(w_self, w1, w2) as (h_self, h1, h2):
161 h_result = func(self.handles.get_ctx(), h_self, h1, h2)
162 if not h_result:
163 space.fromcache(State).raise_current_exception()
164 return self.handles.consume(h_result)
165
166 class W_wrap_indexargfunc(object):
167 def call(self, space, __args__):
168 func = llapi.cts.cast("HPyFunc_ssizeargfunc", self.cfuncptr)
169 self.check_args(space, __args__, 2)
170 w_self = __args__.arguments_w[0]
171 w_idx = __args__.arguments_w[1]
172 idx = space.int_w(space.index(w_idx))
173 with self.handles.using(w_self) as h_self:
174 h_result = func(self.handles.get_ctx(), h_self, idx)
175 if not h_result:
176 space.fromcache(State).raise_current_exception()
177 return self.handles.consume(h_result)
178
179 class W_wrap_inquirypred(object):
180 def call(self, space, __args__):
181 func = llapi.cts.cast("HPyFunc_inquiry", self.cfuncptr)
182 self.check_args(space, __args__, 1)
183 w_self = __args__.arguments_w[0]
184 with self.handles.using(w_self) as h_self:
185 res = func(self.handles.get_ctx(), h_self)
186 res = rffi.cast(lltype.Signed, res)
187 if res == -1:
188 space.fromcache(State).raise_current_exception()
189 return space.newbool(bool(res))
190
191 class W_wrap_lenfunc(object):
192 def call(self, space, __args__):
193 func = llapi.cts.cast("HPyFunc_lenfunc", self.cfuncptr)
194 self.check_args(space, __args__, 1)
195 w_self = __args__.arguments_w[0]
196 with self.handles.using(w_self) as h_self:
197 result = func(self.handles.get_ctx(), h_self)
198 if widen(result) == -1:
199 space.fromcache(State).raise_current_exception()
200 return space.newint(result)
201
202 class W_wrap_hashfunc(object):
203 def call(self, space, __args__):
204 func = llapi.cts.cast("HPyFunc_hashfunc", self.cfuncptr)
205 self.check_args(space, __args__, 1)
206 w_self = __args__.arguments_w[0]
207 with self.handles.using(w_self) as h_self:
208 result = func(self.handles.get_ctx(), h_self)
209 if widen(result) == -1:
210 operror = space.fromcache(State).clear_exception()
211 if operror:
212 raise operror
213 return space.newint(result)
214
215 class W_wrap_call(object):
216 def call(self, space, __args__):
217 w_func = self.handles.w_ExtensionMethod(space, self.handles, "__call__",
218 llapi.HPyFunc_KEYWORDS, "", self.cfuncptr, self.w_objclass)
219 return space.call_args(w_func, __args__)
220
221 class W_wrap_call_at_offset(object):
222 def call(self, space, __args__):
223 from .interp_type import W_HPyObject
224 w_obj = __args__.arguments_w[0]
225 assert isinstance(w_obj, W_HPyObject)
226 storage = w_obj._hpy_get_raw_storage(space)
227 if not storage:
228 raise oefmt(space.w_TypeError, "non-HPy object in __call__")
229 addr = rffi.cast(lltype.Signed, storage) + self.offset
230 callfunc = llapi.cts.cast('HPyCallFunction*', addr)
231 cfuncptr = llapi.cts.cast('HPyCFunction', callfunc.c_impl)
232 w_func = self.handles.w_ExtensionMethod(space, self.handles, "__call__",
233 llapi.HPyFunc_KEYWORDS, "", cfuncptr, self.w_objclass)
234 return space.call_args(w_func, __args__)
235
236 def sq_getindex(space, w_sequence, w_idx):
237 """
238 This is equivalent to CPython's typeobject.c:getindex().
239 We call it sq_getindex because it's used only by sq_* slots.
240 """
241 idx = space.int_w(space.index(w_idx))
242 if idx < 0 and space.lookup(w_sequence, '__len__'):
243 # It is worth noting that we are doing the lookup of __len__ twice,
244 # one above and one inside space.len_w. The JIT should optimize it
245 # away, but it might be a minor slowdown for interpreted code.
246 n = space.len_w(w_sequence)
247 idx += n
248 return idx
249
250 class W_wrap_mp_item(object):
251 def call(self, space, __args__):
252 func = llapi.cts.cast("HPyFunc_ssizeargfunc", self.cfuncptr)
253 self.check_args(space, __args__, 2)
254 w_self = __args__.arguments_w[0]
255 w_key = __args__.arguments_w[1]
256 with self.handles.using(w_self, w_key) as (h_self, h_key):
257 h_result = func(self.handles.get_ctx(), h_self, h_key)
258 if not h_result:
259 space.fromcache(State).raise_current_exception()
260 return self.handles.consume(h_result)
261
262 class W_wrap_sq_item(object):
263 def call(self, space, __args__):
264 func = llapi.cts.cast("HPyFunc_ssizeargfunc", self.cfuncptr)
265 self.check_args(space, __args__, 2)
266 w_self = __args__.arguments_w[0]
267 w_idx = __args__.arguments_w[1]
268 idx = sq_getindex(space, w_self, w_idx)
269 with self.handles.using(w_self) as h_self:
270 h_result = func(self.handles.get_ctx(), h_self, idx)
271 if not h_result:
272 space.fromcache(State).raise_current_exception()
273 return self.handles.consume(h_result)
274
275 class W_wrap_mp_setitem(object):
276 def call(self, space, __args__):
277 func = llapi.cts.cast("HPyFunc_ssizeobjargproc", self.cfuncptr)
278 self.check_args(space, __args__, 3)
279 w_self = __args__.arguments_w[0]
280 w_key = __args__.arguments_w[1]
281 w_value = __args__.arguments_w[2]
282 with self.handles.using(w_self, w_key, w_value) as (h_self, h_key, h_value):
283 result = func(self.handles.get_ctx(), h_self, h_key, h_value)
284 if widen(result) == -1:
285 space.fromcache(State).raise_current_exception()
286 return space.w_None
287
288 class W_wrap_sq_setitem(object):
289 def call(self, space, __args__):
290 func = llapi.cts.cast("HPyFunc_ssizeobjargproc", self.cfuncptr)
291 self.check_args(space, __args__, 3)
292 w_self = __args__.arguments_w[0]
293 w_idx = __args__.arguments_w[1]
294 idx = sq_getindex(space, w_self, w_idx)
295 w_value = __args__.arguments_w[2]
296 with self.handles.using(w_self, w_value) as (h_self, h_value):
297 result = func(self.handles.get_ctx(), h_self, idx, h_value)
298 if widen(result) == -1:
299 space.fromcache(State).raise_current_exception()
300 return space.w_None
301
302 class W_wrap_mp_delitem(object):
303 def call(self, space, __args__):
304 func = llapi.cts.cast("HPyFunc_ssizeobjargproc", self.cfuncptr)
305 self.check_args(space, __args__, 2)
306 w_self = __args__.arguments_w[0]
307 w_key = __args__.arguments_w[1]
308 with self.handles.using(w_self, w_key) as (h_self, h_key):
309 result = func(self.handles.get_ctx(), h_self, h_key, llapi.HPy_NULL)
310 if widen(result) == -1:
311 space.fromcache(State).raise_current_exception()
312 return space.w_None
313
314 class W_wrap_sq_delitem(object):
315 def call(self, space, __args__):
316 func = llapi.cts.cast("HPyFunc_ssizeobjargproc", self.cfuncptr)
317 self.check_args(space, __args__, 2)
318 w_self = __args__.arguments_w[0]
319 w_idx = __args__.arguments_w[1]
320 idx = sq_getindex(space, w_self, w_idx)
321 with self.handles.using(w_self) as h_self:
322 result = func(self.handles.get_ctx(), h_self, idx, llapi.HPy_NULL)
323 if widen(result) == -1:
324 space.fromcache(State).raise_current_exception()
325 return space.w_None
326
327 class W_wrap_objobjproc(object):
328 def call(self, space, __args__):
329 func = llapi.cts.cast("HPyFunc_objobjproc", self.cfuncptr)
330 self.check_args(space, __args__, 2)
331 w_self = __args__.arguments_w[0]
332 w_key = __args__.arguments_w[1]
333 with self.handles.using(w_self, w_key) as (h_self, h_key):
334 res = func(self.handles.get_ctx(), h_self, h_key)
335 res = widen(res)
336 if res == -1:
337 space.fromcache(State).raise_current_exception()
338 return space.newbool(bool(res))
339
340 class W_wrap_getbuffer(object):
341 rbp = llapi.cts.cast('HPyFunc_releasebufferproc', 0)
342
343 def call(self, space, __args__):
344 func = llapi.cts.cast("HPyFunc_getbufferproc", self.cfuncptr)
345 self.check_args(space, __args__, 2)
346 w_self = __args__.arguments_w[0]
347 w_flags = __args__.arguments_w[1]
348 flags = rffi.cast(rffi.INT_real, space.int_w(w_flags))
349 with lltype.scoped_alloc(llapi.cts.gettype('HPy_buffer')) as hpybuf:
350 with self.handles.using(w_self) as h_self:
351 res = func(self.handles.get_ctx(), h_self, hpybuf, flags)
352 if widen(res) < 0:
353 space.fromcache(State).raise_current_exception()
354 buf_ptr = hpybuf.c_buf
355 w_obj = self.handles.consume(hpybuf.c_obj.c__i)
356 size = hpybuf.c_len
357 ndim = widen(hpybuf.c_ndim)
358 shape = None
359 if hpybuf.c_shape:
360 shape = [hpybuf.c_shape[i] for i in range(ndim)]
361 strides = None
362 if hpybuf.c_strides:
363 strides = [hpybuf.c_strides[i] for i in range(ndim)]
364 if hpybuf.c_format:
365 format = rffi.charp2str(hpybuf.c_format)
366 else:
367 format = 'B'
368 view = self.handles.HPyBuffer(
369 buf_ptr, size, w_obj,
370 itemsize=hpybuf.c_itemsize,
371 readonly=widen(hpybuf.c_readonly),
372 ndim=widen(hpybuf.c_ndim), format=format, shape=shape,
373 strides=strides)
374 if self.rbp:
375 # XXX: we're assuming w_self and w_obj have the same type!
376 view.releasebufferproc = self.rbp
377 self.handles.BUFFER_FQ.register_finalizer(view)
378 return view.wrap(space)
379
380
381 # remaining wrappers to write
382 ## wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
383 ## wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
384 ## wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
385 ## wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
386 ## wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
387 ## wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
388 ## wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
389 ## wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
390 ## wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
391 ## wrap_del(PyObject *self, PyObject *args, void *wrapped)
392 ## wrap_next(PyObject *self, PyObject *args, void *wrapped)
393 ## wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
394 ## wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
395 ## wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
396
397 class W_wrap_init(object):
398 def call(self, space, __args__):
399 with self.handles.using(__args__.arguments_w[0]) as h_self:
400 n = len(__args__.arguments_w) - 1
401 with lltype.scoped_alloc(rffi.CArray(llapi.HPy), n) as args_h:
402 i = 0
403 while i < n:
404 args_h[i] = self.handles.new(__args__.arguments_w[i + 1])
405 i += 1
406 h_kw = 0
407 if __args__.keyword_names_w:
408 w_kw = space.newdict()
409 for i in range(len(__args__.keyword_names_w)):
410 w_key = __args__.keyword_names_w[i]
411 w_value = __args__.keywords_w[i]
412 space.setitem(w_kw, w_key, w_value)
413 h_kw = self.handles.new(w_kw)
414 fptr = llapi.cts.cast('HPyFunc_initproc', self.cfuncptr)
415 try:
416 result = fptr(self.handles.get_ctx(), h_self, args_h, n, h_kw)
417 finally:
418 if h_kw:
419 self.handles.close(h_kw)
420 for i in range(n):
421 self.handles.close(args_h[i])
422 if rffi.cast(lltype.Signed, result) < 0:
423 space.fromcache(State).raise_current_exception()
424 return space.w_None
425
426 @specialize.memo()
427 def get_slot_cls(handles, mixin):
428 try:
429 return _WRAPPER_CACHE[handles, mixin]
430 except KeyError:
431 pass
432
433 _handles = handles
434 class wrapper(W_SlotWrapper):
435 import_from_mixin(mixin)
436 handles = _handles
437
438 wrapper.__name__ = mixin.__name__ + handles.cls_suffix
439 _WRAPPER_CACHE[handles, mixin] = wrapper
440 return wrapper
441
442 @specialize.memo()
443 def get_tp_new_wrapper_cls(handles):
444 try:
445 return _WRAPPER_CACHE[handles, 'new']
446 except KeyError:
447 pass
448
449 class W_tp_new_wrapper(handles.w_ExtensionFunction):
450 """
451 Special case for HPy_tp_new. Note that is not NOT a SlotWrapper.
452
453 This is the equivalent of CPython's tp_new_wrapper: the difference is that
454 CPython's tp_new_wrapper is a regular PyMethodDef which is wrapped inside
455 a PyCFunction, while here we have our own type.
456 """
457
458 def __init__(self, cfuncptr, w_type):
459 handles.w_ExtensionFunction.__init__(
460 self, handles.space, handles, '__new__',
461 llapi.HPyFunc_KEYWORDS, None, cfuncptr, w_self=w_type)
462
463 def call(self, space, h_self, __args__, skip_args=0):
464 assert space is handles.space
465 assert skip_args == 0
466 # NOTE: h_self contains the type for which we are calling __new__, but
467 # here is ignored. In CPython's tp_new_wrapper it is only used to fish
468 # the ->tp_new to call, but here we already have the cfuncptr
469 #
470 # XXX: tp_new_wrapper does additional checks, we should write tests
471 # and implement the same checks
472 w_self = __args__.arguments_w[0]
473 with handles.using(w_self) as h_self:
474 return self.call_varargs_kw(space, h_self, __args__,
475 skip_args=1, has_keywords=True)
476 W_tp_new_wrapper.__name__ += handles.cls_suffix
477 _WRAPPER_CACHE[handles, 'new'] = W_tp_new_wrapper
478 return W_tp_new_wrapper
479
480
481 # the following table shows how to map C-level slots into Python-level
482 # __methods__. Note that if a C-level slot corresponds to multiple
483 # __methods__, it appears multiple times (e.g. sq_ass_item corresponds to both
484 # __setitem__ and __delitem__).
485 SLOTS = unrolling_iterable([
486 # CPython slots
487 ('bf_getbuffer', '__buffer__', W_wrap_getbuffer),
488 ('mp_ass_subscript', '__setitem__', W_wrap_mp_setitem),
489 ('mp_ass_subscript', '__delitem__', W_wrap_mp_delitem),
490 ('mp_length', '__len__', W_wrap_lenfunc),
491 ('mp_subscript', '__getitem__', W_wrap_mp_item),
492 ('nb_absolute', '__abs__', W_wrap_unaryfunc),
493 ('nb_add', '__add__', W_wrap_binaryfunc),
494 ('nb_and', '__and__', W_wrap_binaryfunc),
495 ('nb_bool', '__bool__', W_wrap_inquirypred),
496 ('nb_divmod', '__divmod__', W_wrap_binaryfunc),
497 ('nb_float', '__float__', W_wrap_unaryfunc),
498 ('nb_floor_divide', '__floordiv__', W_wrap_binaryfunc),
499 ('nb_index', '__index__', W_wrap_unaryfunc),
500 ('nb_inplace_add', '__iadd__', W_wrap_binaryfunc),
501 ('nb_inplace_and', '__iand__', W_wrap_binaryfunc),
502 ('nb_inplace_floor_divide', '__ifloordiv__', W_wrap_binaryfunc),
503 ('nb_inplace_lshift', '__ilshift__', W_wrap_binaryfunc),
504 ('nb_inplace_multiply', '__imul__', W_wrap_binaryfunc),
505 ('nb_inplace_or', '__ior__', W_wrap_binaryfunc),
506 # CPython is buggy here: it uses wrap_binaryfunc for nb_inplace_power, but
507 # it means you end up calling the cfunc with the wrong signature! We
508 # correctly user W_wrap_ternaryfunc instead
509 ('nb_inplace_power', '__ipow__', W_wrap_ternaryfunc),
510 ('nb_inplace_remainder', '__imod__', W_wrap_binaryfunc),
511 ('nb_inplace_rshift', '__irshift__', W_wrap_binaryfunc),
512 ('nb_inplace_subtract', '__isub__', W_wrap_binaryfunc),
513 ('nb_inplace_true_divide', '__itruediv__', W_wrap_binaryfunc),
514 ('nb_inplace_xor', '__ixor__', W_wrap_binaryfunc),
515 ('nb_int', '__int__', W_wrap_unaryfunc),
516 ('nb_invert', '__invert__', W_wrap_unaryfunc),
517 ('nb_lshift', '__lshift__', W_wrap_binaryfunc),
518 ('nb_multiply', '__mul__', W_wrap_binaryfunc),
519 ('nb_negative', '__neg__', W_wrap_unaryfunc),
520 ('nb_or', '__or__', W_wrap_binaryfunc),
521 ('nb_positive', '__pos__', W_wrap_unaryfunc),
522 ('nb_power', '__pow__', W_wrap_ternaryfunc),
523 ('nb_remainder', '__mod__', W_wrap_binaryfunc),
524 ('nb_rshift', '__rshift__', W_wrap_binaryfunc),
525 ('nb_subtract', '__sub__', W_wrap_binaryfunc),
526 ('nb_true_divide', '__truediv__', W_wrap_binaryfunc),
527 ('nb_xor', '__xor__', W_wrap_binaryfunc),
528 ('sq_ass_item', '__setitem__', W_wrap_sq_setitem),
529 ('sq_ass_item', '__delitem__', W_wrap_sq_delitem),
530 ('sq_concat', '__add__', W_wrap_binaryfunc),
531 ('sq_contains', '__contains__', W_wrap_objobjproc),
532 ('sq_inplace_concat', '__iadd__', W_wrap_binaryfunc),
533 ('sq_inplace_repeat', '__imul__', W_wrap_indexargfunc),
534 ('sq_item', '__getitem__', W_wrap_sq_item),
535 ('sq_length', '__len__', W_wrap_lenfunc),
536 ('sq_repeat', '__mul__', W_wrap_indexargfunc),
537 # ('tp_base', '__xxx__', AGS.W_SlotWrapper_...),
538 # ('tp_bases', '__xxx__', AGS.W_SlotWrapper_...),
539 ('tp_call', '__call__', W_wrap_call),
540 # ('tp_clear', '__xxx__', AGS.W_SlotWrapper_...),
541 # ('tp_del', '__xxx__', AGS.W_SlotWrapper_...),
542 ('tp_descr_get', '__get__', W_wrap_ternaryfunc),
543 # ('tp_descr_set', '__xxx__', AGS.W_SlotWrapper_...),
544 # ('tp_doc', '__xxx__', AGS.W_SlotWrapper_...),
545 # ('tp_getattr', '__xxx__', AGS.W_SlotWrapper_...),
546 # ('tp_getattro', '__xxx__', AGS.W_SlotWrapper_...),
547 ('tp_hash', '__hash__', W_wrap_hashfunc),
548 ('tp_init', '__init__', W_wrap_init),
549 # ('tp_is_gc', '__xxx__', AGS.W_SlotWrapper_...),
550 # ('tp_iter', '__iter__', W_wrap_unaryfunc),
551 # ('tp_iternext', '__xxx__', AGS.W_SlotWrapper_...),
552 # tp_new SPECIAL-CASED
553 ('tp_repr', '__repr__', W_wrap_unaryfunc),
554 # tp_richcompare SPECIAL-CASED
555 # ('tp_setattr', '__xxx__', AGS.W_SlotWrapper_...),
556 # ('tp_setattro', '__xxx__', AGS.W_SlotWrapper_...),
557 ('tp_str', '__str__', W_wrap_unaryfunc),
558 # tp_traverse SPECIAL-CASED
559 ('nb_matrix_multiply', '__matmul__', W_wrap_binaryfunc),
560 ('nb_inplace_matrix_multiply', '__imatmul__', W_wrap_binaryfunc),
561 # ('am_await', '__await__', W_wrap_unaryfunc),
562 # ('am_aiter', '__aiter__', W_wrap_unaryfunc),
563 # ('am_anext', '__anext__', W_wrap_unaryfunc),
564 ('tp_finalize', '__del__', W_wrap_unaryfunc),
565
566 # extra HPy-specific slots
567 # ('tp_destroy', '__xxx__', AGS.W_SlotWrapper_...),
568 ])
569
570
571 @jit.dont_look_inside
572 @specialize.arg(0)
573 def fill_slot(handles, w_type, hpyslot):
574 space = handles.space
575 slot_num = rffi.cast(lltype.Signed, hpyslot.c_slot)
576 has_tp_call = False
577 # special cases
578 if slot_num == HPySlot_Slot.HPy_tp_new:
579 # this is the moral equivalent of CPython's add_tp_new_wrapper
580 cls = get_tp_new_wrapper_cls(handles)
581 w_func = cls(hpyslot.c_impl, w_type)
582 w_type.setdictvalue(space, '__new__', w_func)
583 return has_tp_call
584 elif slot_num == HPySlot_Slot.HPy_tp_destroy:
585 w_type.tp_destroy = llapi.cts.cast('HPyFunc_destroyfunc', hpyslot.c_impl)
586 return has_tp_call
587 elif slot_num == HPySlot_Slot.HPy_tp_traverse:
588 w_type.tp_traverse = llapi.cts.cast('HPyFunc_traverseproc', hpyslot.c_impl)
589 return has_tp_call
590 elif slot_num == HPySlot_Slot.HPy_tp_richcompare:
591 for methname, opval in CMP_SLOTS:
592 cls = get_cmp_wrapper_cls(handles, methname, opval)
593 w_slot = cls(slot_num, methname, hpyslot.c_impl, w_type)
594 w_type.setdictvalue(space, methname, w_slot)
595 return has_tp_call
596 elif slot_num == HPySlot_Slot.HPy_tp_finalize:
597 # This is not a normal __slot__ since we want __del__ to be called as a
598 # finalizer, not when the object __del__ is called.
599 cls = get_slot_cls(handles, W_wrap_voidfunc)
600 w_slot = cls(slot_num, "__del__", hpyslot.c_impl, w_type)
601 w_type.tp_finalize = w_slot
602 return has_tp_call
603 elif slot_num == HPySlot_Slot.HPy_bf_releasebuffer:
604 # Handled befor this function
605 return has_tp_call
606 elif slot_num == HPySlot_Slot.HPy_bf_getbuffer:
607 cls = get_slot_cls(handles, W_wrap_getbuffer)
608 w_slot = cls(slot_num,"__buffer__", hpyslot.c_impl, w_type)
609 w_type.setdictvalue(space, "__buffer__", w_slot)
610 elif slot_num == HPySlot_Slot.HPy_tp_call:
611 has_tp_call = True
612 # generic cases
613 found = False
614 for slotname, methname, mixin in SLOTS:
615 assert methname != '__xxx__' # sanity check
616 n = getattr(HPySlot_Slot, 'HPy_' + slotname)
617 if slot_num == n:
618 found = True
619 cls = get_slot_cls(handles, mixin)
620 w_slot = cls(slot_num, methname, hpyslot.c_impl, w_type)
621 w_type.setdictvalue(space, methname, w_slot)
622
623 if not found:
624 raise oefmt(space.w_NotImplementedError, "Unimplemented slot: %s", str(slot_num))
625 return has_tp_call