| 1 |
from __future__ import with_statement |
| 2 |
|
| 3 |
import re |
| 4 |
|
| 5 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 6 |
from rpython.rlib.rarithmetic import widen |
| 7 |
from pypy.module.cpyext.api import ( |
| 8 |
slot_function, generic_cpy_call, PyObject, Py_ssize_t, |
| 9 |
Py_buffer, Py_bufferP, PyTypeObjectPtr, cts, CANNOT_FAIL) |
| 10 |
from pypy.module.cpyext.typeobjectdefs import ( |
| 11 |
unaryfunc, ternaryfunc, binaryfunc, releasebufferproc, |
| 12 |
getattrfunc, getattrofunc, setattrofunc, lenfunc, ssizeargfunc, inquiry, |
| 13 |
ssizessizeargfunc, ssizeobjargproc, iternextfunc, initproc, richcmpfunc, |
| 14 |
cmpfunc, hashfunc, descrgetfunc, descrsetfunc, objobjproc, objobjargproc, |
| 15 |
getbufferproc, ssizessizeobjargproc, destructor) |
| 16 |
from pypy.module.cpyext.pyobject import make_ref, from_ref, as_pyobj, decref |
| 17 |
from pypy.module.cpyext.pyerrors import PyErr_Occurred |
| 18 |
from pypy.module.cpyext.memoryobject import fill_Py_buffer |
| 19 |
from pypy.module.cpyext.state import State |
| 20 |
from pypy.module.cpyext import userslot |
| 21 |
from pypy.module.cpyext.buffer import CBuffer, CPyBuffer, fq |
| 22 |
from pypy.module.cpyext.methodobject import (W_PyCWrapperObject, tuple_from_args_w, |
| 23 |
w_kwargs_from_args) |
| 24 |
from pypy.interpreter.error import OperationError, oefmt |
| 25 |
from pypy.interpreter.argument import Arguments |
| 26 |
from rpython.rlib.unroll import unrolling_iterable |
| 27 |
from rpython.rlib.objectmodel import specialize, not_rpython |
| 28 |
from rpython.tool.sourcetools import func_renamer |
| 29 |
from rpython.flowspace.model import Constant |
| 30 |
from rpython.flowspace.specialcase import register_flow_sc |
| 31 |
from pypy.module.sys.version import CPYTHON_VERSION |
| 32 |
|
| 33 |
PY2 = CPYTHON_VERSION[0] == 2 |
| 34 |
|
| 35 |
|
| 36 |
Py_LT = 0 |
| 37 |
Py_LE = 1 |
| 38 |
Py_EQ = 2 |
| 39 |
Py_NE = 3 |
| 40 |
Py_GT = 4 |
| 41 |
Py_GE = 5 |
| 42 |
|
| 43 |
@not_rpython |
| 44 |
def llslot(space, func): |
| 45 |
return func.api_func.get_llhelper(space) |
| 46 |
|
| 47 |
@register_flow_sc(llslot) |
| 48 |
def sc_llslot(ctx, v_space, v_func): |
| 49 |
assert isinstance(v_func, Constant) |
| 50 |
get_llhelper = v_func.value.api_func.get_llhelper |
| 51 |
return ctx.appcall(get_llhelper, v_space) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
class wrap_init(W_PyCWrapperObject): |
| 60 |
def call(self, space, w_self, __args__): |
| 61 |
func = self.get_func_to_call() |
| 62 |
func_init = rffi.cast(initproc, func) |
| 63 |
py_args = tuple_from_args_w(space, __args__.arguments_w) |
| 64 |
w_kwargs = w_kwargs_from_args(space, __args__) |
| 65 |
try: |
| 66 |
res = generic_cpy_call(space, func_init, w_self, py_args, w_kwargs) |
| 67 |
finally: |
| 68 |
decref(space, py_args) |
| 69 |
if rffi.cast(lltype.Signed, res) == -1: |
| 70 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 71 |
return None |
| 72 |
|
| 73 |
class wrap_unaryfunc(W_PyCWrapperObject): |
| 74 |
def call(self, space, w_self, __args__): |
| 75 |
self.check_args(__args__, 0) |
| 76 |
func = self.get_func_to_call() |
| 77 |
func_unary = rffi.cast(unaryfunc, func) |
| 78 |
return generic_cpy_call(space, func_unary, w_self) |
| 79 |
|
| 80 |
class wrap_binaryfunc(W_PyCWrapperObject): |
| 81 |
def call(self, space, w_self, __args__): |
| 82 |
self.check_args(__args__, 1) |
| 83 |
func = self.get_func_to_call() |
| 84 |
func_binary = rffi.cast(binaryfunc, func) |
| 85 |
w_x = __args__.arguments_w[0] |
| 86 |
return generic_cpy_call(space, func_binary, w_self, w_x) |
| 87 |
|
| 88 |
def _get_ob_type(space, w_obj): |
| 89 |
|
| 90 |
ob_type = as_pyobj(space, space.type(w_obj)) |
| 91 |
return rffi.cast(PyTypeObjectPtr, ob_type) |
| 92 |
|
| 93 |
class wrap_binaryfunc_l(W_PyCWrapperObject): |
| 94 |
def call(self, space, w_self, __args__): |
| 95 |
self.check_args(__args__, 1) |
| 96 |
func = self.get_func_to_call() |
| 97 |
func_binary = rffi.cast(binaryfunc, func) |
| 98 |
w_value = __args__.arguments_w[0] |
| 99 |
return generic_cpy_call(space, func_binary, w_self, w_value) |
| 100 |
|
| 101 |
class wrap_binaryfunc_r(W_PyCWrapperObject): |
| 102 |
def call(self, space, w_self, __args__): |
| 103 |
self.check_args(__args__, 1) |
| 104 |
func = self.get_func_to_call() |
| 105 |
func_binary = rffi.cast(binaryfunc, func) |
| 106 |
w_value = __args__.arguments_w[0] |
| 107 |
return generic_cpy_call(space, func_binary, w_value, w_self) |
| 108 |
|
| 109 |
class wrap_ternaryfunc(W_PyCWrapperObject): |
| 110 |
def call(self, space, w_self, __args__): |
| 111 |
|
| 112 |
self.check_argsv(__args__, 1, 2) |
| 113 |
func = self.get_func_to_call() |
| 114 |
func_ternary = rffi.cast(ternaryfunc, func) |
| 115 |
w_arg0 = __args__.arguments_w[0] |
| 116 |
if len(__args__.arguments_w) == 2: |
| 117 |
w_arg1 = __args__.arguments_w[1] |
| 118 |
else: |
| 119 |
w_arg1 = space.w_None |
| 120 |
return generic_cpy_call(space, func_ternary, w_self, w_arg0, w_arg1) |
| 121 |
|
| 122 |
class wrap_ternaryfunc_r(W_PyCWrapperObject): |
| 123 |
def call(self, space, w_self, __args__): |
| 124 |
|
| 125 |
self.check_argsv(__args__, 1, 2) |
| 126 |
func = self.get_func_to_call() |
| 127 |
func_ternary = rffi.cast(ternaryfunc, func) |
| 128 |
w_arg0 = __args__.arguments_w[0] |
| 129 |
if len(__args__.arguments_w) == 2: |
| 130 |
w_arg1 = __args__.arguments_w[1] |
| 131 |
else: |
| 132 |
w_arg1 = space.w_None |
| 133 |
return generic_cpy_call(space, func_ternary, w_arg0, w_self, w_arg1) |
| 134 |
|
| 135 |
class wrap_inquirypred(W_PyCWrapperObject): |
| 136 |
def call(self, space, w_self, __args__): |
| 137 |
self.check_args(__args__, 0) |
| 138 |
func = self.get_func_to_call() |
| 139 |
func_inquiry = rffi.cast(inquiry, func) |
| 140 |
res = generic_cpy_call(space, func_inquiry, w_self) |
| 141 |
res = rffi.cast(lltype.Signed, res) |
| 142 |
if res == -1: |
| 143 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 144 |
return space.newbool(bool(res)) |
| 145 |
|
| 146 |
class wrap_getattr(W_PyCWrapperObject): |
| 147 |
def call(self, space, w_self, __args__): |
| 148 |
self.check_args(__args__, 1) |
| 149 |
func = self.get_func_to_call() |
| 150 |
func_target = rffi.cast(getattrfunc, func) |
| 151 |
w_name = __args__.arguments_w[0] |
| 152 |
name_ptr = rffi.str2charp(space.text_w(w_name)) |
| 153 |
try: |
| 154 |
return generic_cpy_call(space, func_target, w_self, name_ptr) |
| 155 |
finally: |
| 156 |
rffi.free_charp(name_ptr) |
| 157 |
|
| 158 |
class wrap_getattro(W_PyCWrapperObject): |
| 159 |
def call(self, space, w_self, __args__): |
| 160 |
self.check_args(__args__, 1) |
| 161 |
func = self.get_func_to_call() |
| 162 |
func_target = rffi.cast(getattrofunc, func) |
| 163 |
w_name = __args__.arguments_w[0] |
| 164 |
return generic_cpy_call(space, func_target, w_self, w_name) |
| 165 |
|
| 166 |
class wrap_setattr(W_PyCWrapperObject): |
| 167 |
def call(self, space, w_self, __args__): |
| 168 |
self.check_args(__args__, 2) |
| 169 |
func = self.get_func_to_call() |
| 170 |
func_target = rffi.cast(setattrofunc, func) |
| 171 |
w_name = __args__.arguments_w[0] |
| 172 |
w_value = __args__.arguments_w[1] |
| 173 |
|
| 174 |
res = generic_cpy_call(space, func_target, w_self, w_name, w_value) |
| 175 |
if rffi.cast(lltype.Signed, res) == -1: |
| 176 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 177 |
|
| 178 |
class wrap_delattr(W_PyCWrapperObject): |
| 179 |
def call(self, space, w_self, __args__): |
| 180 |
self.check_args(__args__, 1) |
| 181 |
func = self.get_func_to_call() |
| 182 |
func_target = rffi.cast(setattrofunc, func) |
| 183 |
w_name = __args__.arguments_w[0] |
| 184 |
|
| 185 |
res = generic_cpy_call(space, func_target, w_self, w_name, None) |
| 186 |
if rffi.cast(lltype.Signed, res) == -1: |
| 187 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 188 |
|
| 189 |
class wrap_descr_get(W_PyCWrapperObject): |
| 190 |
def call(self, space, w_self, __args__): |
| 191 |
func = self.get_func_to_call() |
| 192 |
func_target = rffi.cast(descrgetfunc, func) |
| 193 |
length = len(__args__.arguments_w) |
| 194 |
if length == 1: |
| 195 |
w_obj = __args__.arguments_w[0] |
| 196 |
w_type = None |
| 197 |
elif length == 2: |
| 198 |
w_obj = __args__.arguments_w[0] |
| 199 |
w_type = __args__.arguments_w[1] |
| 200 |
else: |
| 201 |
raise oefmt(space.w_TypeError, |
| 202 |
"expected 1 or 2 arguments, got %d", len(__args__.arguments_w)) |
| 203 |
if w_obj is space.w_None: |
| 204 |
w_obj = None |
| 205 |
if w_type is space.w_None: |
| 206 |
w_type = None |
| 207 |
if w_obj is None and w_type is None: |
| 208 |
raise oefmt(space.w_TypeError, "__get__(None, None) is invalid") |
| 209 |
return generic_cpy_call(space, func_target, w_self, w_obj, w_type) |
| 210 |
|
| 211 |
class wrap_descr_set(W_PyCWrapperObject): |
| 212 |
def call(self, space, w_self, __args__): |
| 213 |
self.check_args(__args__, 2) |
| 214 |
func = self.get_func_to_call() |
| 215 |
func_target = rffi.cast(descrsetfunc, func) |
| 216 |
w_obj = __args__.arguments_w[0] |
| 217 |
w_value = __args__.arguments_w[1] |
| 218 |
res = generic_cpy_call(space, func_target, w_self, w_obj, w_value) |
| 219 |
if rffi.cast(lltype.Signed, res) == -1: |
| 220 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 221 |
|
| 222 |
class wrap_descr_delete(W_PyCWrapperObject): |
| 223 |
def call(self, space, w_self, __args__): |
| 224 |
self.check_args(__args__, 1) |
| 225 |
func = self.get_func_to_call() |
| 226 |
func_target = rffi.cast(descrsetfunc, func) |
| 227 |
w_obj = __args__.arguments_w[0] |
| 228 |
res = generic_cpy_call(space, func_target, w_self, w_obj, None) |
| 229 |
if rffi.cast(lltype.Signed, res) == -1: |
| 230 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 231 |
|
| 232 |
class wrap_call(W_PyCWrapperObject): |
| 233 |
def call(self, space, w_self, __args__): |
| 234 |
func = self.get_func_to_call() |
| 235 |
func_target = rffi.cast(ternaryfunc, func) |
| 236 |
py_args = tuple_from_args_w(space, __args__.arguments_w) |
| 237 |
w_kwargs = w_kwargs_from_args(space, __args__) |
| 238 |
try: |
| 239 |
ret = generic_cpy_call(space, func_target, w_self, py_args, w_kwargs) |
| 240 |
finally: |
| 241 |
decref(space, py_args) |
| 242 |
return ret |
| 243 |
|
| 244 |
class wrap_ssizessizeobjargproc(W_PyCWrapperObject): |
| 245 |
def call(self, space, w_self, __args__): |
| 246 |
self.check_args(__args__, 3) |
| 247 |
func = self.get_func_to_call() |
| 248 |
func_target = rffi.cast(ssizessizeobjargproc, func) |
| 249 |
i = space.int_w(space.index(__args__.arguments_w[0])) |
| 250 |
j = space.int_w(space.index(__args__.arguments_w[1])) |
| 251 |
w_y = __args__.arguments_w[2] |
| 252 |
res = generic_cpy_call(space, func_target, w_self, i, j, w_y) |
| 253 |
if rffi.cast(lltype.Signed, res) == -1: |
| 254 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 255 |
|
| 256 |
class wrap_lenfunc(W_PyCWrapperObject): |
| 257 |
def call(self, space, w_self, __args__): |
| 258 |
self.check_args(__args__, 0) |
| 259 |
func = self.get_func_to_call() |
| 260 |
func_len = rffi.cast(lenfunc, func) |
| 261 |
res = generic_cpy_call(space, func_len, w_self) |
| 262 |
if widen(res) == -1: |
| 263 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 264 |
return space.newint(res) |
| 265 |
|
| 266 |
class wrap_ssizeargproc(W_PyCWrapperObject): |
| 267 |
def call(self, space, w_self, __args__): |
| 268 |
self.check_args(__args__, 1) |
| 269 |
func = self.get_func_to_call() |
| 270 |
func_target = rffi.cast(ssizeargfunc, func) |
| 271 |
w_index = __args__.arguments_w[0] |
| 272 |
index = space.int_w(space.index(w_index)) |
| 273 |
return generic_cpy_call(space, func_target, w_self, index) |
| 274 |
|
| 275 |
class wrap_sq_setitem(W_PyCWrapperObject): |
| 276 |
def call(self, space, w_self, __args__): |
| 277 |
self.check_args(__args__, 2) |
| 278 |
func = self.get_func_to_call() |
| 279 |
func_target = rffi.cast(ssizeobjargproc, func) |
| 280 |
w_index = __args__.arguments_w[0] |
| 281 |
w_value = __args__.arguments_w[1] |
| 282 |
index = space.int_w(space.index(w_index)) |
| 283 |
res = generic_cpy_call(space, func_target, w_self, index, w_value) |
| 284 |
if rffi.cast(lltype.Signed, res) == -1: |
| 285 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 286 |
|
| 287 |
class wrap_sq_delitem(W_PyCWrapperObject): |
| 288 |
def call(self, space, w_self, __args__): |
| 289 |
self.check_args(__args__, 1) |
| 290 |
func = self.get_func_to_call() |
| 291 |
func_target = rffi.cast(ssizeobjargproc, func) |
| 292 |
w_index = __args__.arguments_w[0] |
| 293 |
index = space.int_w(space.index(w_index)) |
| 294 |
null = rffi.cast(PyObject, 0) |
| 295 |
res = generic_cpy_call(space, func_target, w_self, index, null) |
| 296 |
if rffi.cast(lltype.Signed, res) == -1: |
| 297 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 298 |
|
| 299 |
|
| 300 |
class wrap_objobjproc(W_PyCWrapperObject): |
| 301 |
def call(self, space, w_self, __args__): |
| 302 |
self.check_args(__args__, 1) |
| 303 |
func = self.get_func_to_call() |
| 304 |
func_target = rffi.cast(objobjproc, func) |
| 305 |
w_value = __args__.arguments_w[0] |
| 306 |
res = generic_cpy_call(space, func_target, w_self, w_value) |
| 307 |
res = rffi.cast(lltype.Signed, res) |
| 308 |
if res == -1: |
| 309 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 310 |
return space.newbool(bool(res)) |
| 311 |
|
| 312 |
class wrap_objobjargproc(W_PyCWrapperObject): |
| 313 |
def call(self, space, w_self, __args__): |
| 314 |
self.check_args(__args__, 2) |
| 315 |
func = self.get_func_to_call() |
| 316 |
func_target = rffi.cast(objobjargproc, func) |
| 317 |
w_key = __args__.arguments_w[0] |
| 318 |
w_value = __args__.arguments_w[1] |
| 319 |
res = generic_cpy_call(space, func_target, w_self, w_key, w_value) |
| 320 |
if rffi.cast(lltype.Signed, res) == -1: |
| 321 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 322 |
return space.w_None |
| 323 |
|
| 324 |
class wrap_delitem(W_PyCWrapperObject): |
| 325 |
def call(self, space, w_self, __args__): |
| 326 |
self.check_args(__args__, 1) |
| 327 |
func = self.get_func_to_call() |
| 328 |
func_target = rffi.cast(objobjargproc, func) |
| 329 |
w_key = __args__.arguments_w[0] |
| 330 |
null = rffi.cast(PyObject, 0) |
| 331 |
res = generic_cpy_call(space, func_target, w_self, w_key, null) |
| 332 |
if rffi.cast(lltype.Signed, res) == -1: |
| 333 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 334 |
return space.w_None |
| 335 |
|
| 336 |
class wrap_ssizessizeargfunc(W_PyCWrapperObject): |
| 337 |
def call(self, space, w_self, __args__): |
| 338 |
self.check_args(__args__, 2) |
| 339 |
func = self.get_func_to_call() |
| 340 |
func_target = rffi.cast(ssizessizeargfunc, func) |
| 341 |
start = space.int_w(__args__.arguments_w[0]) |
| 342 |
end = space.int_w(__args__.arguments_w[1]) |
| 343 |
return generic_cpy_call(space, func_target, w_self, start, end) |
| 344 |
|
| 345 |
class wrap_next(W_PyCWrapperObject): |
| 346 |
def call(self, space, w_self, __args__): |
| 347 |
from pypy.module.cpyext.api import generic_cpy_call_expect_null |
| 348 |
self.check_args(__args__, 0) |
| 349 |
func = self.get_func_to_call() |
| 350 |
func_target = rffi.cast(iternextfunc, func) |
| 351 |
w_res = generic_cpy_call_expect_null(space, func_target, w_self) |
| 352 |
if not w_res and not PyErr_Occurred(space): |
| 353 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 354 |
return w_res |
| 355 |
|
| 356 |
class wrap_hashfunc(W_PyCWrapperObject): |
| 357 |
def call(self, space, w_self, __args__): |
| 358 |
self.check_args(__args__, 0) |
| 359 |
func = self.get_func_to_call() |
| 360 |
func_target = rffi.cast(hashfunc, func) |
| 361 |
res = generic_cpy_call(space, func_target, w_self) |
| 362 |
if res == -1: |
| 363 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 364 |
return space.newint(res) |
| 365 |
|
| 366 |
|
| 367 |
class wrap_getbuffer(W_PyCWrapperObject): |
| 368 |
def call(self, space, w_self, __args__): |
| 369 |
func = self.get_func_to_call() |
| 370 |
func_target = rffi.cast(getbufferproc, func) |
| 371 |
py_type = _get_ob_type(space, w_self) |
| 372 |
rbp = rffi.cast(releasebufferproc, 0) |
| 373 |
if py_type.c_tp_as_buffer: |
| 374 |
rbp = py_type.c_tp_as_buffer.c_bf_releasebuffer |
| 375 |
with lltype.scoped_alloc(Py_buffer) as pybuf: |
| 376 |
_flags = 0 |
| 377 |
if len(__args__.arguments_w) > 0: |
| 378 |
_flags = space.int_w(__args__.arguments_w[0]) |
| 379 |
flags = rffi.cast(rffi.INT_real,_flags) |
| 380 |
size = generic_cpy_call(space, func_target, w_self, pybuf, flags) |
| 381 |
if widen(size) < 0: |
| 382 |
space.fromcache(State).check_and_raise_exception(always=True) |
| 383 |
ptr = pybuf.c_buf |
| 384 |
size = pybuf.c_len |
| 385 |
ndim = widen(pybuf.c_ndim) |
| 386 |
shape = None |
| 387 |
if pybuf.c_shape: |
| 388 |
shape = [pybuf.c_shape[i] for i in range(ndim)] |
| 389 |
strides = None |
| 390 |
if pybuf.c_strides: |
| 391 |
strides = [pybuf.c_strides[i] for i in range(ndim)] |
| 392 |
if pybuf.c_format: |
| 393 |
format = rffi.charp2str(pybuf.c_format) |
| 394 |
else: |
| 395 |
format = 'B' |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
buf = CPyBuffer(space, ptr, size, w_self, format=format, |
| 401 |
ndim=ndim, shape=shape, strides=strides, |
| 402 |
itemsize=pybuf.c_itemsize, |
| 403 |
readonly=widen(pybuf.c_readonly), |
| 404 |
needs_decref=True, |
| 405 |
releasebufferproc = rbp) |
| 406 |
fq.register_finalizer(buf) |
| 407 |
return buf.wrap(space) |
| 408 |
|
| 409 |
def get_richcmp_func(OP_CONST): |
| 410 |
class wrap_richcmp(W_PyCWrapperObject): |
| 411 |
def call(self, space, w_self, __args__): |
| 412 |
self.check_args(__args__, 1) |
| 413 |
func = self.get_func_to_call() |
| 414 |
func_target = rffi.cast(richcmpfunc, func) |
| 415 |
w_other = __args__.arguments_w[0] |
| 416 |
return generic_cpy_call(space, func_target, |
| 417 |
w_self, w_other, rffi.cast(rffi.INT_real, OP_CONST)) |
| 418 |
return wrap_richcmp |
| 419 |
|
| 420 |
richcmp_eq = get_richcmp_func(Py_EQ) |
| 421 |
richcmp_ne = get_richcmp_func(Py_NE) |
| 422 |
richcmp_lt = get_richcmp_func(Py_LT) |
| 423 |
richcmp_le = get_richcmp_func(Py_LE) |
| 424 |
richcmp_gt = get_richcmp_func(Py_GT) |
| 425 |
richcmp_ge = get_richcmp_func(Py_GE) |
| 426 |
|
| 427 |
class wrap_cmpfunc(W_PyCWrapperObject): |
| 428 |
def call(self, space, w_self, __args__): |
| 429 |
self.check_args(__args__, 1) |
| 430 |
func = self.get_func_to_call() |
| 431 |
func_target = rffi.cast(cmpfunc, func) |
| 432 |
w_other = __args__.arguments_w[0] |
| 433 |
|
| 434 |
if not space.issubtype_w(space.type(w_self), space.type(w_other)): |
| 435 |
raise oefmt(space.w_TypeError, |
| 436 |
"%T.__cmp__(x,y) requires y to be a '%T', not a '%T'", |
| 437 |
w_self, w_self, w_other) |
| 438 |
|
| 439 |
return space.newint(generic_cpy_call(space, func_target, w_self, w_other)) |
| 440 |
|
| 441 |
class wrap_del(W_PyCWrapperObject): |
| 442 |
def call(self, space, w_self, __args__): |
| 443 |
from pypy.module.cpyext.api import generic_cpy_call_expect_null |
| 444 |
self.check_args(__args__, 0) |
| 445 |
func = self.get_func_to_call() |
| 446 |
func_target = rffi.cast(destructor, func) |
| 447 |
generic_cpy_call(space, func_target, w_self) |
| 448 |
return space.w_None |
| 449 |
|
| 450 |
|
| 451 |
SLOT_FACTORIES = {} |
| 452 |
def slot_factory(tp_name): |
| 453 |
def decorate(func): |
| 454 |
SLOT_FACTORIES[tp_name] = func |
| 455 |
return func |
| 456 |
return decorate |
| 457 |
|
| 458 |
|
| 459 |
SLOTS = {} |
| 460 |
@specialize.memo() |
| 461 |
def get_slot_tp_function(space, typedef, name, method_name): |
| 462 |
"""Return a description of the slot C function to use for the built-in |
| 463 |
type for 'typedef'. The 'name' is the slot name. This is a memo |
| 464 |
function that, after translation, returns one of a built-in finite set. |
| 465 |
""" |
| 466 |
key = (typedef, name) |
| 467 |
try: |
| 468 |
return SLOTS[key] |
| 469 |
except KeyError: |
| 470 |
slot_func = SLOT_FACTORIES[name](space, typedef, name, method_name) |
| 471 |
api_func = slot_func.api_func if slot_func else None |
| 472 |
SLOTS[key] = api_func |
| 473 |
return api_func |
| 474 |
|
| 475 |
|
| 476 |
def make_unary_slot(space, typedef, name, attr): |
| 477 |
w_type = space.gettypeobject(typedef) |
| 478 |
slot_fn = w_type.lookup(attr) |
| 479 |
if slot_fn is None: |
| 480 |
return |
| 481 |
|
| 482 |
@slot_function([PyObject], PyObject) |
| 483 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 484 |
def slot_func(space, w_self): |
| 485 |
return space.call_function(slot_fn, w_self) |
| 486 |
return slot_func |
| 487 |
|
| 488 |
UNARY_SLOTS = [ |
| 489 |
'tp_as_async.c_am_await', |
| 490 |
'tp_as_async.c_am_anext', |
| 491 |
'tp_as_async.c_am_aiter', |
| 492 |
'tp_as_number.c_nb_int', |
| 493 |
'tp_as_number.c_nb_long', |
| 494 |
'tp_as_number.c_nb_float', |
| 495 |
'tp_as_number.c_nb_negative', |
| 496 |
'tp_as_number.c_nb_positive', |
| 497 |
'tp_as_number.c_nb_absolute', |
| 498 |
'tp_as_number.c_nb_invert', |
| 499 |
'tp_as_number.c_nb_index', |
| 500 |
'tp_as_number.c_nb_hex', |
| 501 |
'tp_as_number.c_nb_oct', |
| 502 |
'tp_str', |
| 503 |
'tp_repr', |
| 504 |
'tp_iter'] |
| 505 |
for name in UNARY_SLOTS: |
| 506 |
slot_factory(name)(make_unary_slot) |
| 507 |
|
| 508 |
def make_unary_slot_int(space, typedef, name, attr): |
| 509 |
w_type = space.gettypeobject(typedef) |
| 510 |
slot_fn = w_type.lookup(attr) |
| 511 |
if slot_fn is None: |
| 512 |
return |
| 513 |
@slot_function([PyObject], lltype.Signed, error=-1) |
| 514 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 515 |
def slot_func(space, w_obj): |
| 516 |
return space.int_w(space.call_function(slot_fn, w_obj)) |
| 517 |
return slot_func |
| 518 |
|
| 519 |
UNARY_SLOTS_INT = [ |
| 520 |
'tp_hash', |
| 521 |
'tp_as_sequence.c_sq_length', |
| 522 |
'tp_as_mapping.c_mp_length',] |
| 523 |
for name in UNARY_SLOTS_INT: |
| 524 |
slot_factory(name)(make_unary_slot_int) |
| 525 |
|
| 526 |
|
| 527 |
def make_binary_slot(space, typedef, name, attr): |
| 528 |
w_type = space.gettypeobject(typedef) |
| 529 |
slot_fn = w_type.lookup(attr) |
| 530 |
if slot_fn is None: |
| 531 |
return |
| 532 |
|
| 533 |
@slot_function([PyObject, PyObject], PyObject) |
| 534 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 535 |
def slot_func(space, w_self, w_arg): |
| 536 |
return space.call_function(slot_fn, w_self, w_arg) |
| 537 |
return slot_func |
| 538 |
|
| 539 |
BINARY_SLOTS = [ |
| 540 |
'tp_as_number.c_nb_add', |
| 541 |
'tp_as_number.c_nb_subtract', |
| 542 |
'tp_as_number.c_nb_multiply', |
| 543 |
'tp_as_number.c_nb_divide', |
| 544 |
'tp_as_number.c_nb_remainder', |
| 545 |
'tp_as_number.c_nb_divmod', |
| 546 |
'tp_as_number.c_nb_lshift', |
| 547 |
'tp_as_number.c_nb_rshift', |
| 548 |
'tp_as_number.c_nb_and', |
| 549 |
'tp_as_number.c_nb_xor', |
| 550 |
'tp_as_number.c_nb_or', |
| 551 |
'tp_as_sequence.c_sq_concat', |
| 552 |
'tp_as_sequence.c_sq_inplace_concat', |
| 553 |
'tp_as_mapping.c_mp_subscript',] |
| 554 |
for name in BINARY_SLOTS: |
| 555 |
slot_factory(name)(make_binary_slot) |
| 556 |
|
| 557 |
|
| 558 |
def make_binary_slot_int(space, typedef, name, attr): |
| 559 |
w_type = space.gettypeobject(typedef) |
| 560 |
slot_fn = w_type.lookup(attr) |
| 561 |
if slot_fn is None: |
| 562 |
return |
| 563 |
|
| 564 |
@slot_function([PyObject, Py_ssize_t], PyObject) |
| 565 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 566 |
def slot_func(space, w_self, arg): |
| 567 |
return space.call_function(slot_fn, w_self, space.newint(arg)) |
| 568 |
return slot_func |
| 569 |
|
| 570 |
BINARY_SLOTS_INT = [ |
| 571 |
'tp_as_sequence.c_sq_item', |
| 572 |
'tp_as_sequence.c_sq_repeat', |
| 573 |
'tp_as_sequence.c_sq_inplace_repeat',] |
| 574 |
for name in BINARY_SLOTS_INT: |
| 575 |
slot_factory(name)(make_binary_slot_int) |
| 576 |
|
| 577 |
@slot_factory('tp_as_number.c_nb_power') |
| 578 |
def make_nb_power(space, typedef, name, attr): |
| 579 |
w_type = space.gettypeobject(typedef) |
| 580 |
slot_fn = w_type.lookup(attr) |
| 581 |
if slot_fn is None: |
| 582 |
return |
| 583 |
|
| 584 |
@slot_function([PyObject, PyObject, PyObject], PyObject) |
| 585 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 586 |
def slot_func(space, w_self, w_arg1, w_arg2): |
| 587 |
return space.call_function(slot_fn, w_self, w_arg1, w_arg2) |
| 588 |
return slot_func |
| 589 |
|
| 590 |
@slot_factory('tp_as_mapping.c_mp_ass_subscript') |
| 591 |
def make_sq_set_item(space, typedef, name, attr): |
| 592 |
w_type = space.gettypeobject(typedef) |
| 593 |
slot_ass = w_type.lookup(attr) |
| 594 |
if slot_ass is None: |
| 595 |
return |
| 596 |
slot_del = w_type.lookup('__delitem__') |
| 597 |
if slot_del is None: |
| 598 |
return |
| 599 |
|
| 600 |
@slot_function([PyObject, PyObject, PyObject], rffi.INT_real, error=-1) |
| 601 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 602 |
def slot_func(space, w_self, w_arg1, arg2): |
| 603 |
if arg2: |
| 604 |
w_arg2 = from_ref(space, rffi.cast(PyObject, arg2)) |
| 605 |
space.call_function(slot_ass, w_self, w_arg1, w_arg2) |
| 606 |
else: |
| 607 |
space.call_function(slot_del, w_self, w_arg1) |
| 608 |
return 0 |
| 609 |
return slot_func |
| 610 |
|
| 611 |
|
| 612 |
@slot_factory('tp_as_sequence.c_sq_ass_item') |
| 613 |
def make_sq_ass_item(space, typedef, name, attr): |
| 614 |
w_type = space.gettypeobject(typedef) |
| 615 |
slot_ass = w_type.lookup(attr) |
| 616 |
if slot_ass is None: |
| 617 |
return |
| 618 |
slot_del = w_type.lookup('__delitem__') |
| 619 |
if slot_del is None: |
| 620 |
return |
| 621 |
|
| 622 |
@slot_function([PyObject, lltype.Signed, PyObject], rffi.INT_real, error=-1) |
| 623 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 624 |
def slot_func(space, w_self, arg1, arg2): |
| 625 |
if arg2: |
| 626 |
w_arg2 = from_ref(space, rffi.cast(PyObject, arg2)) |
| 627 |
space.call_function(slot_ass, w_self, space.newint(arg1), w_arg2) |
| 628 |
else: |
| 629 |
space.call_function(slot_del, w_self, space.newint(arg1)) |
| 630 |
return 0 |
| 631 |
return slot_func |
| 632 |
|
| 633 |
def make_tp_setattro(space, typedef, name, attr): |
| 634 |
w_type = space.gettypeobject(typedef) |
| 635 |
setattr_fn = w_type.lookup('__setattr__') |
| 636 |
delattr_fn = w_type.lookup('__delattr__') |
| 637 |
if setattr_fn is None or delattr_fn is None: |
| 638 |
return |
| 639 |
|
| 640 |
@slot_function([PyObject, PyObject, PyObject], rffi.INT_real, |
| 641 |
error=-1) |
| 642 |
@func_renamer("cpyext_tp_setattro_%s" % (typedef.name,)) |
| 643 |
def slot_tp_setattro(space, w_self, w_name, w_value): |
| 644 |
if w_value is not None: |
| 645 |
space.call_function(setattr_fn, w_self, w_name, w_value) |
| 646 |
else: |
| 647 |
space.call_function(delattr_fn, w_self, w_name) |
| 648 |
return 0 |
| 649 |
return slot_tp_setattro |
| 650 |
|
| 651 |
@slot_factory('tp_getattro') |
| 652 |
def make_tp_getattro(space, typedef, name, attr): |
| 653 |
w_type = space.gettypeobject(typedef) |
| 654 |
getattr_fn = w_type.lookup('__getattribute__') |
| 655 |
if getattr_fn is None: |
| 656 |
return |
| 657 |
|
| 658 |
@slot_function([PyObject, PyObject], PyObject) |
| 659 |
@func_renamer("cpyext_tp_getattro_%s" % (typedef.name,)) |
| 660 |
def slot_tp_getattro(space, w_self, w_name): |
| 661 |
return space.call_function(getattr_fn, w_self, w_name) |
| 662 |
return slot_tp_getattro |
| 663 |
|
| 664 |
@slot_factory('tp_call') |
| 665 |
def make_tp_call(space, typedef, name, attr): |
| 666 |
w_type = space.gettypeobject(typedef) |
| 667 |
call_fn = w_type.lookup('__call__') |
| 668 |
if call_fn is None: |
| 669 |
return |
| 670 |
|
| 671 |
@slot_function([PyObject, PyObject, PyObject], PyObject) |
| 672 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 673 |
def slot_tp_call(space, w_self, w_args, w_kwds): |
| 674 |
args = Arguments(space, [w_self], |
| 675 |
w_stararg=w_args, w_starstararg=w_kwds) |
| 676 |
return space.call_args(call_fn, args) |
| 677 |
return slot_tp_call |
| 678 |
|
| 679 |
|
| 680 |
TP_ITERNEXT_RAISES_STOPITERATION = ( |
| 681 |
"coroutine_wrapper", |
| 682 |
"generator", |
| 683 |
) |
| 684 |
|
| 685 |
@slot_factory('tp_iternext') |
| 686 |
def make_tp_iternext(space, typedef, name, attr): |
| 687 |
w_type = space.gettypeobject(typedef) |
| 688 |
iternext_fn = w_type.lookup('__next__') |
| 689 |
if iternext_fn is None: |
| 690 |
return |
| 691 |
|
| 692 |
raises_stopiter = w_type.name in TP_ITERNEXT_RAISES_STOPITERATION |
| 693 |
|
| 694 |
@slot_function([PyObject], PyObject) |
| 695 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 696 |
def slot_tp_iternext(space, w_self): |
| 697 |
try: |
| 698 |
return space.call_function(iternext_fn, w_self) |
| 699 |
except OperationError as e: |
| 700 |
if raises_stopiter or not e.match(space, space.w_StopIteration): |
| 701 |
raise |
| 702 |
return None |
| 703 |
return slot_tp_iternext |
| 704 |
|
| 705 |
@slot_factory('tp_init') |
| 706 |
def make_tp_init(space, typedef, name, attr): |
| 707 |
w_type = space.gettypeobject(typedef) |
| 708 |
init_fn = w_type.lookup('__init__') |
| 709 |
if init_fn is None: |
| 710 |
return |
| 711 |
|
| 712 |
@slot_function([PyObject, PyObject, PyObject], rffi.INT_real, error=-1) |
| 713 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 714 |
def slot_tp_init(space, w_self, w_args, w_kwds): |
| 715 |
args = Arguments(space, [w_self], |
| 716 |
w_stararg=w_args, w_starstararg=w_kwds) |
| 717 |
space.call_args(init_fn, args) |
| 718 |
return 0 |
| 719 |
return slot_tp_init |
| 720 |
|
| 721 |
@slot_factory('tp_new') |
| 722 |
def make_tp_new(space, typedef, name, attr): |
| 723 |
w_type = space.gettypeobject(typedef) |
| 724 |
new_fn = w_type.lookup('__new__') |
| 725 |
if new_fn is None: |
| 726 |
return |
| 727 |
|
| 728 |
@slot_function([PyTypeObjectPtr, PyObject, PyObject], PyObject) |
| 729 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 730 |
def slot_tp_new(space, w_self, w_args, w_kwds): |
| 731 |
args = Arguments(space, [w_self], |
| 732 |
w_stararg=w_args, w_starstararg=w_kwds) |
| 733 |
return space.call_args(space.get(new_fn, w_self), args) |
| 734 |
return slot_tp_new |
| 735 |
|
| 736 |
@slot_function([PyObject, lltype.Ptr(Py_buffer), rffi.INT_real], |
| 737 |
rffi.INT_real, error=-1) |
| 738 |
def bytes_getbuffer(space, w_str, view, flags): |
| 739 |
from pypy.module.cpyext.bytesobject import PyBytes_AsString |
| 740 |
from pypy.module.cpyext.buffer import PyBuffer_FillInfo |
| 741 |
c_buf = rffi.cast(rffi.VOIDP, PyBytes_AsString(space, w_str)) |
| 742 |
return PyBuffer_FillInfo(space, view, w_str, c_buf, |
| 743 |
space.len_w(w_str), 1, flags) |
| 744 |
|
| 745 |
@slot_factory('tp_as_buffer.c_bf_getbuffer') |
| 746 |
def make_bf_getbuffer(space, typedef, name, attr): |
| 747 |
w_type = space.gettypeobject(typedef) |
| 748 |
if space.is_w(w_type, space.w_bytes): |
| 749 |
return bytes_getbuffer |
| 750 |
elif typedef.buffer: |
| 751 |
return slot_from_buffer_w(space, typedef) |
| 752 |
else: |
| 753 |
return None |
| 754 |
|
| 755 |
@slot_factory('tp_as_buffer.c_bf_releasebuffer') |
| 756 |
def make_bf_releasebuffer(space, typedef, name, attr): |
| 757 |
w_type = space.gettypeobject(typedef) |
| 758 |
release_fn = w_type.lookup('__release_buffer__') |
| 759 |
if release_fn is None: |
| 760 |
return None |
| 761 |
name = 'bf_releasebuffer' |
| 762 |
@slot_function([PyObject, Py_bufferP], lltype.Void, error=CANNOT_FAIL) |
| 763 |
@func_renamer("cpyext_%s_%s" % (name, typedef.name)) |
| 764 |
def slot_bf_releasebuffer(space, w_self, view): |
| 765 |
return |
| 766 |
|
| 767 |
|
| 768 |
return slot_bf_releasebuffer |
| 769 |
|
| 770 |
@slot_factory('tp_descr_get') |
| 771 |
def make_tp_descr_get(space, typedef, name, attr): |
| 772 |
w_type = space.gettypeobject(typedef) |
| 773 |
get_fn = w_type.lookup('__get__') |
| 774 |
if get_fn is None: |
| 775 |
return |
| 776 |
|
| 777 |
@slot_function([PyObject, PyObject, PyObject], PyObject) |
| 778 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 779 |
def slot_tp_descr_get(space, w_self, w_obj, w_value): |
| 780 |
if w_obj is None: |
| 781 |
w_obj = space.w_None |
| 782 |
return space.call_function(get_fn, w_self, w_obj, w_value) |
| 783 |
return slot_tp_descr_get |
| 784 |
|
| 785 |
@slot_factory('tp_descr_set') |
| 786 |
def make_tp_descr_set(space, typedef, name, attr): |
| 787 |
w_type = space.gettypeobject(typedef) |
| 788 |
set_fn = w_type.lookup('__set__') |
| 789 |
delete_fn = w_type.lookup('__delete__') |
| 790 |
if set_fn is None and delete_fn is None: |
| 791 |
return |
| 792 |
|
| 793 |
@slot_function([PyObject, PyObject, PyObject], rffi.INT_real, error=-1) |
| 794 |
@func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name)) |
| 795 |
def slot_tp_descr_set(space, w_self, w_obj, w_value): |
| 796 |
if w_value is not None: |
| 797 |
if set_fn is None: |
| 798 |
raise oefmt(space.w_TypeError, |
| 799 |
"%s object has no __set__", typedef.name) |
| 800 |
space.call_function(set_fn, w_self, w_obj, w_value) |
| 801 |
else: |
| 802 |
if delete_fn is None: |
| 803 |
raise oefmt(space.w_TypeError, |
| 804 |
"%s object has no __delete__", typedef.name) |
| 805 |
space.call_function(delete_fn, w_self, w_obj) |
| 806 |
return 0 |
| 807 |
return slot_tp_descr_set |
| 808 |
|
| 809 |
def slot_from_buffer_w(space, typedef): |
| 810 |
name = 'bf_getbuffer' |
| 811 |
@slot_function([PyObject, Py_bufferP, rffi.INT_real], |
| 812 |
rffi.INT_real, error=-1) |
| 813 |
@func_renamer("cpyext_%s_%s" % (name, typedef.name)) |
| 814 |
def buff_w(space, w_self, c_view, flags): |
| 815 |
w_obj = w_self |
| 816 |
if c_view: |
| 817 |
|
| 818 |
flags = widen(flags) |
| 819 |
buf = space.buffer_w(w_obj, flags) |
| 820 |
try: |
| 821 |
c_view.c_buf = rffi.cast(rffi.VOIDP, buf.get_raw_address()) |
| 822 |
c_view.c_obj = make_ref(space, w_obj) |
| 823 |
if space.isinstance_w(w_obj, space.w_bytes): |
| 824 |
rffi.setintfield(c_view, 'c_readonly', 1) |
| 825 |
else: |
| 826 |
rffi.setintfield(c_view, 'c_readonly', int(buf.readonly)) |
| 827 |
except ValueError: |
| 828 |
s = buf.as_str() |
| 829 |
w_s = space.newbytes(s) |
| 830 |
c_view.c_obj = make_ref(space, w_s) |
| 831 |
c_view.c_buf = rffi.cast(rffi.VOIDP, rffi.str2charp( |
| 832 |
s, track_allocation=False)) |
| 833 |
rffi.setintfield(c_view, 'c_readonly', 1) |
| 834 |
ret = fill_Py_buffer(space, buf, c_view) |
| 835 |
return ret |
| 836 |
return 0 |
| 837 |
return buff_w |
| 838 |
|
| 839 |
def make_missing_slot(space, typedef, name, attr): |
| 840 |
return None |
| 841 |
|
| 842 |
missing_builtin_slots = [ |
| 843 |
'tp_vectorcall_offset', 'tp_compare', 'tp_getattr', 'tp_setattr', 'tp_setattro', |
| 844 |
'tp_finalize', |
| 845 |
'tp_richcompare', 'tp_del', |
| 846 |
'tp_as_number.c_nb_bool', 'tp_as_number.c_nb_coerce', |
| 847 |
'tp_as_number.c_nb_inplace_add', 'tp_as_number.c_nb_inplace_subtract', |
| 848 |
'tp_as_number.c_nb_inplace_multiply', 'tp_as_number.c_nb_inplace_divide', |
| 849 |
'tp_as_number.c_nb_inplace_remainder', 'tp_as_number.c_nb_inplace_power', |
| 850 |
'tp_as_number.c_nb_inplace_lshift', 'tp_as_number.c_nb_inplace_rshift', |
| 851 |
'tp_as_number.c_nb_inplace_and', 'tp_as_number.c_nb_inplace_xor', |
| 852 |
'tp_as_number.c_nb_inplace_or', |
| 853 |
'tp_as_number.c_nb_floor_divide', 'tp_as_number.c_nb_true_divide', |
| 854 |
'tp_as_number.c_nb_inplace_floor_divide', 'tp_as_number.c_nb_inplace_true_divide', |
| 855 |
'tp_as_number.c_nb_matrix_multiply', |
| 856 |
'tp_as_number.c_nb_inplace_matrix_multiply', |
| 857 |
'tp_as_sequence.c_sq_slice', 'tp_as_sequence.c_sq_ass_slice', |
| 858 |
'tp_as_sequence.c_sq_contains', |
| 859 |
] |
| 860 |
if PY2: |
| 861 |
missing_builtin_slots += [ |
| 862 |
'tp_as_buffer.c_bf_getreadbuffer', |
| 863 |
'tp_as_buffer.c_bf_getwritebuffer', |
| 864 |
] |
| 865 |
for name in missing_builtin_slots: |
| 866 |
slot_factory(name)(make_missing_slot) |
| 867 |
|
| 868 |
|
| 869 |
PyWrapperFlag_KEYWORDS = 1 |
| 870 |
|
| 871 |
class TypeSlot: |
| 872 |
def __init__(self, method_name, slot_name, function, wrapper, doc): |
| 873 |
self.method_name = method_name |
| 874 |
self.slot_name = slot_name |
| 875 |
self.slot_names = tuple(("c_" + slot_name).split(".")) |
| 876 |
self.slot_func = function |
| 877 |
self.wrapper_class = wrapper |
| 878 |
self.doc = doc |
| 879 |
|
| 880 |
|
| 881 |
def FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS): |
| 882 |
if WRAPPER is None: |
| 883 |
wrapper = None |
| 884 |
else: |
| 885 |
wrapper = globals()[WRAPPER] |
| 886 |
|
| 887 |
|
| 888 |
if NAME == "__getattr__": |
| 889 |
if SLOT == "tp_getattro": |
| 890 |
wrapper = wrap_getattro |
| 891 |
elif SLOT == "tp_getattr": |
| 892 |
wrapper = wrap_getattr |
| 893 |
else: |
| 894 |
assert False |
| 895 |
|
| 896 |
function = getattr(userslot, FUNCTION or '!missing', None) |
| 897 |
assert FLAGS == 0 or FLAGS == PyWrapperFlag_KEYWORDS |
| 898 |
return TypeSlot(NAME, SLOT, function, wrapper, DOC) |
| 899 |
|
| 900 |
def TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC): |
| 901 |
return FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, 0) |
| 902 |
|
| 903 |
ETSLOT = TPSLOT |
| 904 |
|
| 905 |
def AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC): |
| 906 |
return ETSLOT(NAME, "tp_as_async.c_" + SLOT, FUNCTION, WRAPPER, DOC) |
| 907 |
def SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC): |
| 908 |
return ETSLOT(NAME, "tp_as_sequence.c_" + SLOT, FUNCTION, WRAPPER, DOC) |
| 909 |
def MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC): |
| 910 |
return ETSLOT(NAME, "tp_as_mapping.c_" + SLOT, FUNCTION, WRAPPER, DOC) |
| 911 |
def NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC): |
| 912 |
return ETSLOT(NAME, "tp_as_number.c_" + SLOT, FUNCTION, WRAPPER, DOC) |
| 913 |
def UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC): |
| 914 |
return ETSLOT(NAME, "tp_as_number.c_" + SLOT, FUNCTION, WRAPPER, |
| 915 |
"x." + NAME + "() <==> " + DOC) |
| 916 |
def IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC): |
| 917 |
return ETSLOT(NAME, "tp_as_number.c_" + SLOT, FUNCTION, WRAPPER, |
| 918 |
"x." + NAME + "(y) <==> x" + DOC + "y") |
| 919 |
def BINSLOT(NAME, SLOT, FUNCTION, DOC): |
| 920 |
return ETSLOT(NAME, "tp_as_number.c_" + SLOT, FUNCTION, "wrap_binaryfunc_l", \ |
| 921 |
"x." + NAME + "(y) <==> x" + DOC + "y") |
| 922 |
def RBINSLOT(NAME, SLOT, FUNCTION, DOC): |
| 923 |
return ETSLOT(NAME, "tp_as_number.c_" + SLOT, FUNCTION, "wrap_binaryfunc_r", \ |
| 924 |
"x." + NAME + "(y) <==> y" + DOC + "x") |
| 925 |
def BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC): |
| 926 |
return ETSLOT(NAME, "tp_as_number.c_" + SLOT, FUNCTION, "wrap_binaryfunc_l", \ |
| 927 |
"x." + NAME + "(y) <==> " + DOC) |
| 928 |
def RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC): |
| 929 |
return ETSLOT(NAME, "tp_as_number.c_" + SLOT, FUNCTION, "wrap_binaryfunc_r", \ |
| 930 |
"x." + NAME + "(y) <==> " + DOC) |
| 931 |
|
| 932 |
""" |
| 933 |
/* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. |
| 934 |
The logic in abstract.c always falls back to nb_add/nb_multiply in |
| 935 |
this case. Defining both the nb_* and the sq_* slots to call the |
| 936 |
user-defined methods has unexpected side-effects, as shown by |
| 937 |
test_descr.notimplemented() */ |
| 938 |
""" |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
slotdefs_str = r""" |
| 944 |
static slotdef slotdefs[] = { |
| 945 |
TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 946 |
TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 947 |
TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 948 |
TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 949 |
TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 950 |
"__repr__($self, /)\n--\n\nReturn repr(self)."), |
| 951 |
TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 952 |
"__hash__($self, /)\n--\n\nReturn hash(self)."), |
| 953 |
FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 954 |
"__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function.", |
| 955 |
PyWrapperFlag_KEYWORDS), |
| 956 |
TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 957 |
"__str__($self, /)\n--\n\nReturn str(self)."), |
| 958 |
TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
| 959 |
wrap_binaryfunc, |
| 960 |
"__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)."), |
| 961 |
TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 962 |
TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 963 |
"__setattr__($self, name, value, /)\n--\n\nImplement setattr(self, name, value)."), |
| 964 |
TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 965 |
"__delattr__($self, name, /)\n--\n\nImplement delattr(self, name)."), |
| 966 |
TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 967 |
"__lt__($self, value, /)\n--\n\nReturn self<value."), |
| 968 |
TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 969 |
"__le__($self, value, /)\n--\n\nReturn self<=value."), |
| 970 |
TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 971 |
"__eq__($self, value, /)\n--\n\nReturn self==value."), |
| 972 |
TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 973 |
"__ne__($self, value, /)\n--\n\nReturn self!=value."), |
| 974 |
TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 975 |
"__gt__($self, value, /)\n--\n\nReturn self>value."), |
| 976 |
TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 977 |
"__ge__($self, value, /)\n--\n\nReturn self>=value."), |
| 978 |
TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 979 |
"__iter__($self, /)\n--\n\nImplement iter(self)."), |
| 980 |
TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next, |
| 981 |
"__next__($self, /)\n--\n\nImplement next(self)."), |
| 982 |
TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 983 |
"__get__($self, instance, owner, /)\n--\n\nReturn an attribute of instance, which is of type owner."), |
| 984 |
TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 985 |
"__set__($self, instance, value, /)\n--\n\nSet an attribute of instance to value."), |
| 986 |
TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, |
| 987 |
wrap_descr_delete, |
| 988 |
"__delete__($self, instance, /)\n--\n\nDelete an attribute of instance."), |
| 989 |
FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
| 990 |
"__init__($self, /, *args, **kwargs)\n--\n\n" |
| 991 |
"Initialize self. See help(type(self)) for accurate signature.", |
| 992 |
PyWrapperFlag_KEYWORDS), |
| 993 |
TPSLOT("__new__", tp_new, slot_tp_new, NULL, |
| 994 |
"__new__(type, /, *args, **kwargs)\n--\n\n" |
| 995 |
"Create and return new object. See help(type) for accurate signature."), |
| 996 |
TPSLOT("__del__", tp_finalize, slot_tp_finalize, wrap_del, ""), |
| 997 |
|
| 998 |
AMSLOT("__await__", am_await, slot_am_await, wrap_unaryfunc, |
| 999 |
"__await__($self, /)\n--\n\nReturn an iterator to be used in await expression."), |
| 1000 |
AMSLOT("__aiter__", am_aiter, slot_am_aiter, wrap_unaryfunc, |
| 1001 |
"__aiter__($self, /)\n--\n\nReturn an awaitable, that resolves in asynchronous iterator."), |
| 1002 |
AMSLOT("__anext__", am_anext, slot_am_anext, wrap_unaryfunc, |
| 1003 |
"__anext__($self, /)\n--\n\nReturn a value or raise StopAsyncIteration."), |
| 1004 |
|
| 1005 |
BINSLOT("__add__", nb_add, slot_nb_add, |
| 1006 |
"+"), |
| 1007 |
RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 1008 |
"+"), |
| 1009 |
BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 1010 |
"-"), |
| 1011 |
RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 1012 |
"-"), |
| 1013 |
BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 1014 |
"*"), |
| 1015 |
RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 1016 |
"*"), |
| 1017 |
BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 1018 |
"%"), |
| 1019 |
RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 1020 |
"%"), |
| 1021 |
BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, |
| 1022 |
"Return divmod(self, value)."), |
| 1023 |
RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 1024 |
"Return divmod(value, self)."), |
| 1025 |
NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 1026 |
"__pow__($self, value, mod=None, /)\n--\n\nReturn pow(self, value, mod)."), |
| 1027 |
NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 1028 |
"__rpow__($self, value, mod=None, /)\n--\n\nReturn pow(value, self, mod)."), |
| 1029 |
UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-self"), |
| 1030 |
UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+self"), |
| 1031 |
UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 1032 |
"abs(self)"), |
| 1033 |
UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred, |
| 1034 |
"self != 0"), |
| 1035 |
UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~self"), |
| 1036 |
BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 1037 |
RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 1038 |
BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 1039 |
RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 1040 |
BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 1041 |
RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 1042 |
BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 1043 |
RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 1044 |
BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 1045 |
RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 1046 |
UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 1047 |
"int(self)"), |
| 1048 |
UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 1049 |
"float(self)"), |
| 1050 |
IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 1051 |
wrap_binaryfunc, "+="), |
| 1052 |
IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 1053 |
wrap_binaryfunc, "-="), |
| 1054 |
IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 1055 |
wrap_binaryfunc, "*="), |
| 1056 |
IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 1057 |
wrap_binaryfunc, "%="), |
| 1058 |
IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
| 1059 |
wrap_ternaryfunc, "**="), |
| 1060 |
IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 1061 |
wrap_binaryfunc, "<<="), |
| 1062 |
IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 1063 |
wrap_binaryfunc, ">>="), |
| 1064 |
IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 1065 |
wrap_binaryfunc, "&="), |
| 1066 |
IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 1067 |
wrap_binaryfunc, "^="), |
| 1068 |
IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 1069 |
wrap_binaryfunc, "|="), |
| 1070 |
BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 1071 |
RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 1072 |
BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 1073 |
RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 1074 |
IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 1075 |
slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="), |
| 1076 |
IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 1077 |
slot_nb_inplace_true_divide, wrap_binaryfunc, "/="), |
| 1078 |
NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, |
| 1079 |
"__index__($self, /)\n--\n\n" |
| 1080 |
"Return self converted to an integer, if self is suitable " |
| 1081 |
"for use as an index into a list."), |
| 1082 |
BINSLOT("__matmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
| 1083 |
"@"), |
| 1084 |
RBINSLOT("__rmatmul__", nb_matrix_multiply, slot_nb_matrix_multiply, |
| 1085 |
"@"), |
| 1086 |
IBSLOT("__imatmul__", nb_inplace_matrix_multiply, slot_nb_inplace_matrix_multiply, |
| 1087 |
wrap_binaryfunc, "@="), |
| 1088 |
MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, |
| 1089 |
"__len__($self, /)\n--\n\nReturn len(self)."), |
| 1090 |
MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
| 1091 |
wrap_binaryfunc, |
| 1092 |
"__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
| 1093 |
MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 1094 |
wrap_objobjargproc, |
| 1095 |
"__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
| 1096 |
MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
| 1097 |
wrap_delitem, |
| 1098 |
"__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
| 1099 |
|
| 1100 |
SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, |
| 1101 |
"__len__($self, /)\n--\n\nReturn len(self)."), |
| 1102 |
|
| 1103 |
SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, |
| 1104 |
"__add__($self, value, /)\n--\n\nReturn self+value."), |
| 1105 |
SQSLOT("__mul__", sq_repeat, NULL, wrap_ssizeargproc, |
| 1106 |
"__mul__($self, value, /)\n--\n\nReturn self*value.n"), |
| 1107 |
SQSLOT("__rmul__", sq_repeat, NULL, wrap_ssizeargproc, |
| 1108 |
"__rmul__($self, value, /)\n--\n\nReturn self*value."), |
| 1109 |
SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_ssizeargproc, |
| 1110 |
"__getitem__($self, key, /)\n--\n\nReturn self[key]."), |
| 1111 |
SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 1112 |
"__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."), |
| 1113 |
SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 1114 |
"__delitem__($self, key, /)\n--\n\nDelete self[key]."), |
| 1115 |
SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 1116 |
"__contains__($self, key, /)\n--\n\nReturn key in self."), |
| 1117 |
SQSLOT("__iadd__", sq_inplace_concat, NULL, |
| 1118 |
wrap_binaryfunc, |
| 1119 |
"__iadd__($self, value, /)\n--\n\nImplement self+=value."), |
| 1120 |
SQSLOT("__imul__", sq_inplace_repeat, NULL, |
| 1121 |
wrap_ssizeargproc, |
| 1122 |
"__imul__($self, value, /)\n--\n\nImplement self*=value."), |
| 1123 |
|
| 1124 |
{NULL} |
| 1125 |
}; |
| 1126 |
""" |
| 1127 |
|
| 1128 |
|
| 1129 |
slotdef_replacements = ( |
| 1130 |
("\s+", " "), |
| 1131 |
("static [^{]*{", "("), |
| 1132 |
("};", ")"), |
| 1133 |
("{NULL}", ""), |
| 1134 |
|
| 1135 |
(r"(?P<start> +..SLOT\([^,]*, )(?P<fname>[^,]*), (?P<slotcname>[^,]*), (?P<wname>[^,]*)", r"\g<start>'\g<fname>', '\g<slotcname>', '\g<wname>'"), |
| 1136 |
(r"(?P<start> *R?[^ ]{3}SLOT(NOTINFIX)?\([^,]*, )(?P<fname>[^,]*), (?P<slotcname>[^,]*)", r"\g<start>'\g<fname>', '\g<slotcname>'"), |
| 1137 |
("'NULL'", "None"), |
| 1138 |
("\(wrapperfunc\)", ""), |
| 1139 |
("\),", "),\n"), |
| 1140 |
) |
| 1141 |
|
| 1142 |
for regex, repl in slotdef_replacements: |
| 1143 |
slotdefs_str = re.sub(regex, repl, slotdefs_str) |
| 1144 |
|
| 1145 |
slotdefs = eval(slotdefs_str) |
| 1146 |
|
| 1147 |
slotdefs += ( |
| 1148 |
TPSLOT("__buffer__", "tp_as_buffer.c_bf_getbuffer", "slot_bf_getbuffer", "wrap_getbuffer", ""), |
| 1149 |
TPSLOT("__release_buffer__", "tp_as_buffer.c_bf_releasebuffer", "slot_bf_releasebuffer", None, ""), |
| 1150 |
) |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
def slotdef_sort_key(slotdef): |
| 1157 |
if slotdef.slot_name.startswith('tp_as_number'): |
| 1158 |
return 1 |
| 1159 |
if slotdef.slot_name.startswith('tp_as_mapping'): |
| 1160 |
return 2 |
| 1161 |
if slotdef.slot_name.startswith('tp_as_sequence'): |
| 1162 |
return 3 |
| 1163 |
return 0 |
| 1164 |
slotdefs = sorted(slotdefs, key=slotdef_sort_key) |
| 1165 |
|
| 1166 |
slotdefs_for_tp_slots = unrolling_iterable( |
| 1167 |
[(x.method_name, x.slot_name, x.slot_names, |
| 1168 |
x.slot_func.api_func if x.slot_func else None) for x in slotdefs]) |
| 1169 |
|
| 1170 |
slotdefs_for_wrappers = unrolling_iterable( |
| 1171 |
[(x.method_name, x.slot_names, x.wrapper_class, x.doc) |
| 1172 |
for x in slotdefs]) |
| 1173 |
|
| 1174 |
if __name__ == "__main__": |
| 1175 |
print slotdefs_str |