Debian Codegraph @

pypy/module/cpyext/userslot.py

raw symbols SCIP
1 """
2 These are the default implementation for type slots that we put
3 in user-defined app-level Python classes, if the class implements
4 the corresponding '__xxx__' special method. It should mostly just
5 call back the general version of the space operation.
6
7 This is only approximately correct. One problem is that some
8 details are likely subtly wrong. Another problem is that we don't
9 track changes to an app-level Python class (addition or removal of
10 '__xxx__' special methods) after initalization of the PyTypeObject.
11 """
12
13 from rpython.rtyper.lltypesystem import rffi, lltype
14 from rpython.rlib.rarithmetic import widen
15 from pypy.interpreter.error import oefmt
16 from pypy.interpreter.argument import Arguments
17 from pypy.module.cpyext.api import (
18 slot_function, PyObject, Py_ssize_t, Py_bufferP, PyTypeObjectPtr,
19 CANNOT_FAIL,
20 )
21 from pypy.module.cpyext.memoryobject import fill_Py_buffer
22 from pypy.module.cpyext.pyobject import make_ref
23
24 @slot_function([PyObject], Py_ssize_t, error=-1)
25 def slot_sq_length(space, w_obj):
26 return space.int_w(space.len(w_obj))
27
28 @slot_function([PyObject], lltype.Signed, error=-1)
29 def slot_tp_hash(space, w_obj):
30 return space.hash_w(w_obj)
31
32 @slot_function([PyObject, Py_ssize_t], PyObject)
33 def slot_sq_item(space, w_obj, index):
34 return space.getitem(w_obj, space.newint(index))
35
36 @slot_function([PyTypeObjectPtr, PyObject, PyObject], PyObject)
37 def slot_tp_new(space, w_type, w_args, w_kwds):
38 # XXX problem - we need to find the actual __new__ function to call.
39 # but we have no 'self' argument. Theoretically, self will be
40 # w_type, but if w_type is a subclass of self, and w_type has a
41 # __new__ function that calls super().__new__, and that call ends
42 # up here, we will get infinite recursion. Prevent the recursion
43 # in the simple case (from cython) where w_type is a cpytype, but
44 # we know (since we are in this function) that self is not a cpytype
45 from pypy.module.cpyext.typeobject import W_PyCTypeObject
46 w_type0 = w_type
47 mro_w = space.listview(space.getattr(w_type0, space.newtext('__mro__')))
48 for w_m in mro_w[1:]:
49 if not w_type0.is_cpytype():
50 break
51 w_type0 = w_m
52 w_impl = space.getattr(w_type0, space.newtext('__new__'))
53 args = Arguments(space, [w_type],
54 w_stararg=w_args, w_starstararg=w_kwds)
55 return space.call_args(w_impl, args)
56
57 @slot_function([PyObject, PyObject, PyObject], rffi.INT_real, error=-1)
58 def slot_tp_init(space, w_type, w_args, w_kwds):
59 w_impl = space.lookup(w_type, '__init__')
60 args = Arguments(space, [w_type],
61 w_stararg=w_args, w_starstararg=w_kwds)
62 space.call_args(w_impl, args)
63 return 0
64
65 @slot_function([PyObject, PyObject, PyObject], PyObject)
66 def slot_tp_call(space, w_self, w_args, w_kwds):
67 args = Arguments(space, [], w_stararg=w_args, w_starstararg=w_kwds)
68 return space.call_args(w_self, args)
69
70 # unary functions
71
72 @slot_function([PyObject], PyObject)
73 def slot_tp_str(space, w_obj):
74 return space.str(w_obj)
75
76 @slot_function([PyObject], PyObject)
77 def slot_tp_repr(space, w_obj):
78 return space.repr(w_obj)
79
80 @slot_function([PyObject], PyObject)
81 def slot_nb_int(space, w_obj):
82 return space.call_function(space.w_int, w_obj)
83
84 @slot_function([PyObject], PyObject)
85 def slot_nb_float(space, w_obj):
86 return space.float(w_obj)
87
88 @slot_function([PyObject], PyObject)
89 def slot_nb_index(space, w_obj):
90 return space.index(w_obj)
91
92 #binary functions
93
94 @slot_function([PyObject, PyObject], PyObject)
95 def slot_nb_add(space, w_obj1, w_obj2):
96 return space.add(w_obj1, w_obj2)
97
98 @slot_function([PyObject, PyObject], PyObject)
99 def slot_nb_subtract(space, w_obj1, w_obj2):
100 return space.sub(w_obj1, w_obj2)
101
102 @slot_function([PyObject, PyObject], PyObject)
103 def slot_nb_multiply(space, w_obj1, w_obj2):
104 return space.mul(w_obj1, w_obj2)
105
106 @slot_function([PyObject, PyObject], PyObject)
107 def slot_nb_divide(space, w_obj1, w_obj2):
108 return space.div(w_obj1, w_obj2)
109
110 @slot_function([PyObject, PyObject], PyObject)
111 def slot_nb_inplace_add(space, w_obj1, w_obj2):
112 return space.add(w_obj1, w_obj2)
113
114 @slot_function([PyObject, PyObject], PyObject)
115 def slot_nb_inplace_subtract(space, w_obj1, w_obj2):
116 return space.sub(w_obj1, w_obj2)
117
118 @slot_function([PyObject, PyObject], PyObject)
119 def slot_nb_inplace_multiply(space, w_obj1, w_obj2):
120 return space.mul(w_obj1, w_obj2)
121
122 @slot_function([PyObject, PyObject], PyObject)
123 def slot_nb_inplace_divide(space, w_obj1, w_obj2):
124 return space.div(w_obj1, w_obj2)
125
126 @slot_function([PyObject, PyObject], PyObject)
127 def slot_sq_concat(space, w_obj1, w_obj2):
128 return space.add(w_obj1, w_obj2)
129
130 @slot_function([PyObject, PyObject], PyObject)
131 def slot_sq_inplace_concat(space, w_obj1, w_obj2):
132 return space.add(w_obj1, w_obj2)
133
134 @slot_function([PyObject, PyObject], PyObject)
135 def slot_mp_subscript(space, w_obj1, w_obj2):
136 return space.getitem(w_obj1, w_obj2)
137
138 @slot_function([PyObject, PyObject], PyObject)
139 def slot_tp_getattr_hook(space, w_obj1, w_obj2):
140 return space.getattr(w_obj1, w_obj2)
141
142 @slot_function([PyObject, PyObject, PyObject], PyObject)
143 def slot_tp_descr_get(space, w_self, w_obj, w_type):
144 if w_obj is None:
145 w_obj = space.w_None
146 return space.get(w_self, w_obj, w_type)
147
148 @slot_function([PyObject, PyObject, PyObject], rffi.INT_real, error=-1)
149 def slot_tp_descr_set(space, w_self, w_obj, w_value):
150 if w_value is not None:
151 space.set(w_self, w_obj, w_value)
152 else:
153 space.delete(w_self, w_obj)
154 return 0
155
156 @slot_function([PyObject], PyObject)
157 def slot_tp_iter(space, w_self):
158 return space.iter(w_self)
159
160 @slot_function([PyObject], PyObject)
161 def slot_tp_iternext(space, w_self):
162 return space.next(w_self)
163
164 @slot_function([PyObject], PyObject)
165 def slot_am_await(space, w_self):
166 w_await = space.lookup(w_self, "__await__")
167 if w_await is None:
168 raise oefmt(space.w_TypeError,
169 "object %T does not have __await__ method", w_self)
170 return space.get_and_call_function(w_await, w_self)
171
172 @slot_function([PyObject], PyObject)
173 def slot_am_aiter(space, w_self):
174 w_aiter = space.lookup(w_self, "__aiter__")
175 if w_aiter is None:
176 raise oefmt(space.w_TypeError,
177 "object %T does not have __aiter__ method", w_self)
178 return space.get_and_call_function(w_aiter, w_self)
179
180 @slot_function([PyObject], PyObject)
181 def slot_am_anext(space, w_self):
182 w_anext = space.lookup(w_self, "__anext__")
183 if w_anext is None:
184 raise oefmt(space.w_TypeError,
185 "object %T does not have __anext__ method", w_self)
186 return space.get_and_call_function(w_anext, w_self)
187
188 @slot_function([PyObject, Py_bufferP, rffi.INT_real], rffi.INT_real, error=-1)
189 def slot_bf_getbuffer(space, w_self, c_view, flags):
190 if c_view:
191 c_view.c_obj = rffi.cast(PyObject, 0)
192 w_buffer = space.lookup(w_self, "__buffer__")
193 if not w_buffer:
194 raise oefmt(space.w_BufferError,
195 "object %T does have the buffer interface (via the __buffer__ method)",
196 w_self)
197 flags = widen(flags)
198 args = Arguments(space, [w_self, space.newint(flags)])
199 w_obj = space.call_args(w_buffer, args)
200 # Like slot_from_buffer_w, fill c_view from w_obj
201 buf = space.buffer_w(w_obj, flags)
202 try:
203 c_view.c_buf = rffi.cast(rffi.VOIDP, buf.get_raw_address())
204 c_view.c_obj = make_ref(space, w_obj)
205 if space.isinstance_w(w_obj, space.w_bytes):
206 rffi.setintfield(c_view, 'c_readonly', 1)
207 except ValueError:
208 s = buf.as_str()
209 w_s = space.newbytes(s)
210 c_view.c_obj = make_ref(space, w_s)
211 c_view.c_buf = rffi.cast(rffi.VOIDP, rffi.str2charp(
212 s, track_allocation=False))
213 rffi.setintfield(c_view, 'c_readonly', 1)
214 ret = fill_Py_buffer(space, buf, c_view)
215 return ret
216 return 0
217
218 @slot_function([PyObject, Py_bufferP], lltype.Void, error=CANNOT_FAIL)
219 def slot_bf_releasebuffer(space, w_self, c_view):
220 return