def test_descr_slots(self, space, api): w_descr = space.appexec([], """(): class Descr(object): def __get__(self, obj, type): return 42 + (obj is None) def __set__(self, obj, value): obj.append('set') def __delete__(self, obj): obj.append('del') return Descr() """) w_descrtype = space.type(w_descr) py_descr = make_ref(space, w_descr) py_descrtype = rffi.cast(PyTypeObjectPtr, make_ref(space, w_descrtype)) w_obj = space.newlist([]) py_obj = make_ref(space, w_obj) w_res = generic_cpy_call(space, py_descrtype.c_tp_descr_get, py_descr, py_obj, py_obj) assert space.int_w(w_res) == 42 assert generic_cpy_call( space, py_descrtype.c_tp_descr_set, py_descr, py_obj, make_ref(space, space.w_None)) == 0 assert generic_cpy_call( space, py_descrtype.c_tp_descr_set, py_descr, py_obj, None) == 0 assert space.eq_w(w_obj, space.wrap(['set', 'del'])) # # unbound __get__(self, NULL, type) w_res = generic_cpy_call(space, py_descrtype.c_tp_descr_get, py_descr, None, space.w_int) assert space.int_w(w_res) == 43
def PyObject_AsCharBuffer(space, obj, bufferp, sizep): """Returns a pointer to a read-only memory location usable as character-based input. The obj argument must support the single-segment character buffer interface. On success, returns 0, sets buffer to the memory location and size to the buffer length. Returns -1 and sets a TypeError on error. """ pto = obj.c_ob_type pb = pto.c_tp_as_buffer if not (pb and pb.c_bf_getbuffer): raise OperationError(space.w_TypeError, space.wrap( "expected an object with the buffer interface")) with lltype.scoped_alloc(Py_buffer) as view: ret = generic_cpy_call( space, pb.c_bf_getbuffer, obj, view, rffi.cast(rffi.INT_real, PyBUF_SIMPLE)) if rffi.cast(lltype.Signed, ret) == -1: return -1 bufferp[0] = rffi.cast(rffi.CCHARP, view.c_buf) sizep[0] = view.c_len if pb.c_bf_releasebuffer: generic_cpy_call(space, pb.c_bf_releasebuffer, obj, view) Py_DecRef(space, view.c_obj) return 0
def _Py_Dealloc(space, obj): from pypy.module.cpyext.api import generic_cpy_call pto = obj.c_ob_type #print >>sys.stderr, "Calling dealloc slot", pto.c_tp_dealloc, "of", obj, \ # "'s type which is", rffi.charp2str(pto.c_tp_name) rawrefcount.mark_deallocating(w_marker_deallocating, obj) generic_cpy_call(space, pto.c_tp_dealloc, obj)
def subtype_dealloc(space, obj): pto = obj.c_ob_type base = pto this_func_ptr = llslot(space, subtype_dealloc) w_obj = from_ref(space, rffi.cast(PyObject, base)) # This wrapper is created on a specific type, call it w_A. # We wish to call the dealloc function from one of the base classes of w_A, # the first of which is not this function itself. # w_obj is an instance of w_A or one of its subclasses. So climb up the # inheritance chain until base.c_tp_dealloc is exactly this_func, and then # continue on up until they differ. #print 'subtype_dealloc, start from', rffi.charp2str(base.c_tp_name) while base.c_tp_dealloc != this_func_ptr: base = base.c_tp_base assert base #print ' ne move to', rffi.charp2str(base.c_tp_name) w_obj = from_ref(space, rffi.cast(PyObject, base)) while base.c_tp_dealloc == this_func_ptr: base = base.c_tp_base assert base #print ' eq move to', rffi.charp2str(base.c_tp_name) w_obj = from_ref(space, rffi.cast(PyObject, base)) #print ' end with', rffi.charp2str(base.c_tp_name) dealloc = base.c_tp_dealloc # XXX call tp_del if necessary generic_cpy_call(space, dealloc, obj)
def _Py_Dealloc(space, obj): from pypy.module.cpyext.api import generic_cpy_call pto = obj.c_ob_type #print >>sys.stderr, "Calling dealloc slot", pto.c_tp_dealloc, "of", obj, \ # "'s type which is", rffi.charp2str(pto.c_tp_name) rawrefcount.mark_deallocating(w_marker_deallocating, obj) generic_cpy_call(space, pto.c_tp_dealloc, obj)
def releasebuffer(self): if self.pyobj: if self.needs_decref: if self.releasebufferproc: func_target = rffi.cast(releasebufferproc, self.releasebufferproc) size = rffi.sizeof(cts.gettype('Py_buffer')) pybuf = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw', zero=True) pybuf = cts.cast('Py_buffer*', pybuf) pybuf.c_buf = self.ptr pybuf.c_len = self.size pybuf.c_ndim = cts.cast('int', self.ndim) pybuf.c_shape = cts.cast('Py_ssize_t*', pybuf.c__shape) pybuf.c_strides = cts.cast('Py_ssize_t*', pybuf.c__strides) for i in range(self.ndim): pybuf.c_shape[i] = self.shape[i] pybuf.c_strides[i] = self.strides[i] fmt = rffi.str2charp(self.format if self.format else "B") try: pybuf.c_format = fmt generic_cpy_call(self.space, func_target, self.pyobj, pybuf) finally: lltype.free(fmt, flavor='raw') lltype.free(pybuf, flavor='raw') decref(self.space, self.pyobj) self.pyobj = lltype.nullptr(PyObject.TO) self.w_obj = None else: #do not call twice return
def call(self, space, w_self, w_args, w_kw): # Call the C function if w_self is None: w_self = self.w_self flags = rffi.cast(lltype.Signed, self.ml.c_ml_flags) flags &= ~(METH_CLASS | METH_STATIC | METH_COEXIST) if space.is_true(w_kw) and not flags & METH_KEYWORDS: raise OperationError(space.w_TypeError, space.wrap(self.name + "() takes no keyword arguments")) func = rffi.cast(PyCFunction, self.ml.c_ml_meth) length = space.int_w(space.len(w_args)) if flags & METH_KEYWORDS: func = rffi.cast(PyCFunctionKwArgs, self.ml.c_ml_meth) return generic_cpy_call(space, func, w_self, w_args, w_kw) elif flags & METH_NOARGS: if length == 0: return generic_cpy_call(space, func, w_self, None) raise OperationError(space.w_TypeError, space.wrap(self.name + "() takes no arguments")) elif flags & METH_O: if length != 1: raise oefmt(space.w_TypeError, "%s() takes exactly one argument (%d given)", self.name, length) w_arg = space.getitem(w_args, space.wrap(0)) return generic_cpy_call(space, func, w_self, w_arg) elif flags & METH_VARARGS: return generic_cpy_call(space, func, w_self, w_args) else: # METH_OLDARGS, the really old style size = length if size == 1: w_arg = space.getitem(w_args, space.wrap(0)) elif size == 0: w_arg = None else: w_arg = w_args return generic_cpy_call(space, func, w_self, w_arg)
def releasebuffer(self): if self.pyobj: if self.needs_decref: if self.releasebufferproc: func_target = rffi.cast(releasebufferproc, self.releasebufferproc) with lltype.scoped_alloc(Py_buffer) as pybuf: pybuf.c_buf = self.ptr pybuf.c_len = self.size pybuf.c_ndim = cts.cast('int', self.ndim) pybuf.c_shape = cts.cast('Py_ssize_t*', pybuf.c__shape) pybuf.c_strides = cts.cast('Py_ssize_t*', pybuf.c__strides) for i in range(self.ndim): pybuf.c_shape[i] = self.shape[i] pybuf.c_strides[i] = self.strides[i] if self.format: pybuf.c_format = rffi.str2charp(self.format) else: pybuf.c_format = rffi.str2charp("B") generic_cpy_call(self.space, func_target, self.pyobj, pybuf) decref(self.space, self.pyobj) self.pyobj = lltype.nullptr(PyObject.TO) else: #do not call twice return
def PyObject_AsCharBuffer(space, obj, bufferp, sizep): """Returns a pointer to a read-only memory location usable as character-based input. The obj argument must support the single-segment character buffer interface. On success, returns 0, sets buffer to the memory location and size to the buffer length. Returns -1 and sets a TypeError on error. """ pto = obj.c_ob_type pb = pto.c_tp_as_buffer if not (pb and pb.c_bf_getbuffer): raise oefmt(space.w_TypeError, "expected an object with the buffer interface") with lltype.scoped_alloc(Py_buffer) as view: ret = generic_cpy_call(space, pb.c_bf_getbuffer, obj, view, rffi.cast(rffi.INT_real, PyBUF_SIMPLE)) if rffi.cast(lltype.Signed, ret) == -1: return -1 bufferp[0] = rffi.cast(rffi.CCHARP, view.c_buf) sizep[0] = view.c_len if pb.c_bf_releasebuffer: generic_cpy_call(space, pb.c_bf_releasebuffer, obj, view) decref(space, view.c_obj) return 0
def _dealloc(space, obj): # This frees an object after its refcount dropped to zero, so we # assert that it is really zero here. assert obj.c_ob_refcnt == 0 pto = obj.c_ob_type obj_voidp = rffi.cast(rffi.VOIDP, obj) generic_cpy_call(space, pto.c_tp_free, obj_voidp) if pto.c_tp_flags & Py_TPFLAGS_HEAPTYPE: decref(space, rffi.cast(PyObject, pto))
def subtype_dealloc(space, obj): pto = obj.c_ob_type base = pto this_func_ptr = llhelper(subtype_dealloc.api_func.functype, subtype_dealloc.api_func.get_wrapper(space)) while base.c_tp_dealloc == this_func_ptr: base = base.c_tp_base assert base dealloc = base.c_tp_dealloc # XXX call tp_del if necessary generic_cpy_call(space, dealloc, obj)
def subtype_dealloc(space, obj): pto = obj.c_ob_type base = pto this_func_ptr = llslot(space, subtype_dealloc) while base.c_tp_dealloc == this_func_ptr: base = base.c_tp_base assert base dealloc = base.c_tp_dealloc # XXX call tp_del if necessary generic_cpy_call(space, dealloc, obj)
def subtype_dealloc(space, obj): pto = obj.c_ob_type base = pto this_func_ptr = llhelper(subtype_dealloc.api_func.functype, subtype_dealloc.api_func.get_wrapper(space)) while base.c_tp_dealloc == this_func_ptr: base = base.c_tp_base assert base dealloc = base.c_tp_dealloc # XXX call tp_del if necessary generic_cpy_call(space, dealloc, obj)
def decref(space, pyobj): from pypy.module.cpyext.api import generic_cpy_call assert is_pyobj(pyobj) pyobj = rffi.cast(PyObject, pyobj) if pyobj: assert pyobj.c_ob_refcnt > 0 assert (pyobj.c_ob_pypy_link == 0 or pyobj.c_ob_refcnt > rawrefcount.REFCNT_FROM_PYPY) pyobj.c_ob_refcnt -= 1 if pyobj.c_ob_refcnt == 0: state = space.fromcache(State) generic_cpy_call(space, state.C._Py_Dealloc, pyobj)
def PySequence_ITEM(space, w_obj, i): """Return the ith element of o or NULL on failure. Macro form of PySequence_GetItem() but without checking that PySequence_Check(o)() is true and without adjustment for negative indices. This function used an int type for i. This might require changes in your code for properly supporting 64-bit systems.""" # XXX we should call Py*_GET_ITEM() instead of Py*_GetItem() # from here, but we cannot because we are also called from # PySequence_GetItem() py_obj = as_pyobj(space, w_obj) if isinstance(w_obj, tupleobject.W_TupleObject): from pypy.module.cpyext.tupleobject import PyTuple_GetItem py_res = PyTuple_GetItem(space, py_obj, i) incref(space, py_res) keepalive_until_here(w_obj) return py_res if isinstance(w_obj, W_ListObject): from pypy.module.cpyext.listobject import PyList_GetItem py_res = PyList_GetItem(space, py_obj, i) incref(space, py_res) keepalive_until_here(w_obj) return py_res as_sequence = py_obj.c_ob_type.c_tp_as_sequence if not as_sequence or not as_sequence.c_sq_item: raise oefmt(space.w_TypeError, "'%T' object does not support indexing", w_obj) ret = generic_cpy_call(space, as_sequence.c_sq_item, w_obj, i) return make_ref(space, ret)
def wrap_hashfunc(space, w_self, w_args, func): func_target = rffi.cast(hashfunc, func) check_num_args(space, w_args, 0) res = generic_cpy_call(space, func_target, w_self) if res == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.newint(res)
def wrap_lenfunc(space, w_self, w_args, func): func_len = rffi.cast(lenfunc, func) check_num_args(space, w_args, 0) res = generic_cpy_call(space, func_len, w_self) if widen(res) == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.newint(res)
def setter(self, space, w_self, w_value): assert isinstance(self, W_GetSetPropertyEx) check_descr(space, w_self, self.w_type) res = generic_cpy_call(space, self.getset.c_set, w_self, w_value, self.getset.c_closure) if rffi.cast(lltype.Signed, res) < 0: state = space.fromcache(State) state.check_and_raise_exception()
def call(self, space, w_self, __args__): self.check_args(__args__, 1) func = self.get_func_to_call() func_target = rffi.cast(richcmpfunc, func) w_other = __args__.arguments_w[0] return generic_cpy_call(space, func_target, w_self, w_other, rffi.cast(rffi.INT_real, OP_CONST))
def call(self, space, w_self, __args__): self.check_args(__args__, 1) func = self.get_func_to_call() func_target = rffi.cast(ssizeargfunc, func) w_index = __args__.arguments_w[0] index = space.int_w(space.index(w_index)) return generic_cpy_call(space, func_target, w_self, index)
def call(self, space, w_self, __args__): self.check_args(__args__, 2) func = self.get_func_to_call() func_target = rffi.cast(ssizessizeargfunc, func) start = space.int_w(__args__.arguments_w[0]) end = space.int_w(__args__.arguments_w[1]) return generic_cpy_call(space, func_target, w_self, start, end)
def wrap_descr_delete(space, w_self, w_args, func): func_target = rffi.cast(descrsetfunc, func) check_num_args(space, w_args, 1) w_obj, = space.fixedview(w_args) res = generic_cpy_call(space, func_target, w_self, w_obj, None) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True)
def wrap_hashfunc(space, w_self, w_args, func): func_target = rffi.cast(hashfunc, func) check_num_args(space, w_args, 0) res = generic_cpy_call(space, func_target, w_self) if res == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.wrap(res)
def wrap_ssizessizeargfunc(space, w_self, w_args, func): func_target = rffi.cast(ssizessizeargfunc, func) check_num_args(space, w_args, 2) args_w = space.fixedview(w_args) start = space.int_w(args_w[0]) end = space.int_w(args_w[1]) return generic_cpy_call(space, func_target, w_self, start, end)
def wrap_ssizessizeargfunc(space, w_self, w_args, func): func_target = rffi.cast(ssizessizeargfunc, func) check_num_args(space, w_args, 2) args_w = space.fixedview(w_args) start = space.int_w(args_w[0]) end = space.int_w(args_w[1]) return generic_cpy_call(space, func_target, w_self, start, end)
def wrap_getbuffer(space, w_self, w_args, func): func_target = rffi.cast(getbufferproc, func) with lltype.scoped_alloc(Py_buffer) as pybuf: _flags = 0 if space.len_w(w_args) > 0: _flags = space.int_w(space.listview(w_args)[0]) flags = rffi.cast(rffi.INT_real,_flags) size = generic_cpy_call(space, func_target, w_self, pybuf, flags) if widen(size) < 0: space.fromcache(State).check_and_raise_exception(always=True) ptr = pybuf.c_buf size = pybuf.c_len ndim = widen(pybuf.c_ndim) shape = [pybuf.c_shape[i] for i in range(ndim)] if pybuf.c_strides: strides = [pybuf.c_strides[i] for i in range(ndim)] else: strides = [1] if pybuf.c_format: format = rffi.charp2str(pybuf.c_format) else: format = 'B' return space.newbuffer(CPyBuffer(ptr, size, w_self, format=format, ndim=ndim, shape=shape, strides=strides, itemsize=pybuf.c_itemsize, readonly=widen(pybuf.c_readonly)))
def wrap_descr_delete(space, w_self, w_args, func): func_target = rffi.cast(descrsetfunc, func) check_num_args(space, w_args, 1) w_obj, = space.fixedview(w_args) res = generic_cpy_call(space, func_target, w_self, w_obj, None) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True)
def call(self, space, w_self, __args__): func = self.get_func_to_call() func_target = rffi.cast(ternaryfunc, func) py_args = tuple_from_args_w(space, __args__.arguments_w) w_kwargs = w_kwargs_from_args(space, __args__) ret = generic_cpy_call(space, func_target, w_self, py_args, w_kwargs) decref(space, py_args) return ret
def call(self, space, w_self, __args__): self.check_args(__args__, 0) func = self.get_func_to_call() func_target = rffi.cast(hashfunc, func) res = generic_cpy_call(space, func_target, w_self) if res == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.newint(res)
def wrap_setattr(space, w_self, w_args, func): func_target = rffi.cast(setattrofunc, func) check_num_args(space, w_args, 2) w_name, w_value = space.fixedview(w_args) # XXX "Carlo Verre hack"? res = generic_cpy_call(space, func_target, w_self, w_name, w_value) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True)
def wrap_getreadbuffer(space, w_self, w_args, func): func_target = rffi.cast(readbufferproc, func) with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as ptr: index = rffi.cast(Py_ssize_t, 0) size = generic_cpy_call(space, func_target, w_self, index, ptr) if size < 0: space.fromcache(State).check_and_raise_exception(always=True) return space.wrap(CPyBuffer(ptr[0], size, w_self))
def wrap_delitem(space, w_self, w_args, func): func_target = rffi.cast(objobjargproc, func) check_num_args(space, w_args, 1) w_key, = space.fixedview(w_args) res = generic_cpy_call(space, func_target, w_self, w_key, None) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.w_None
def call(self, space, w_self, __args__): self.check_args(__args__, 1) func = self.get_func_to_call() func_target = rffi.cast(descrsetfunc, func) w_obj = __args__.arguments_w[0] res = generic_cpy_call(space, func_target, w_self, w_obj, None) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True)
def call_keywords(self, space, w_self, __args__): func = rffi.cast(PyCFunctionKwArgs, self.ml.c_ml_meth) py_args = tuple_from_args_w(space, __args__.arguments_w) w_kwargs = w_kwargs_from_args(space, __args__) try: return generic_cpy_call(space, func, w_self, py_args, w_kwargs) finally: decref(space, py_args)
def call_varargs(self, space, w_self, __args__): state = space.fromcache(State) func = self.ml.c_ml_meth py_args = tuple_from_args_w(space, __args__.arguments_w) try: return generic_cpy_call(space, func, w_self, py_args) finally: decref(space, py_args)
def test_string_buffer(self, space, api): py_str = new_empty_str(space, 10) c_buf = py_str.c_ob_type.c_tp_as_buffer assert c_buf py_obj = rffi.cast(PyObject, py_str) assert generic_cpy_call(space, c_buf.c_bf_getsegcount, py_obj, lltype.nullptr(Py_ssize_tP.TO)) == 1 ref = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw') assert generic_cpy_call(space, c_buf.c_bf_getsegcount, py_obj, ref) == 1 assert ref[0] == 10 lltype.free(ref, flavor='raw') ref = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw') assert generic_cpy_call(space, c_buf.c_bf_getreadbuffer, py_obj, 0, ref) == 10 lltype.free(ref, flavor='raw') Py_DecRef(space, py_obj)
def wrap_getbuffer(space, w_self, w_args, func): func_target = rffi.cast(getbufferproc, func) with lltype.scoped_alloc(Py_buffer) as view: flags = rffi.cast(rffi.INT_real, 0) ret = generic_cpy_call(space, func_target, w_self, view, flags) if rffi.cast(lltype.Signed, ret) == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.newbuffer(CPyBuffer(view.c_buf, view.c_len, w_self))
def test_string_buffer(self, space): py_str = new_empty_str(space, 10) c_buf = py_str.c_ob_type.c_tp_as_buffer assert c_buf py_obj = rffi.cast(PyObject, py_str) assert generic_cpy_call(space, c_buf.c_bf_getsegcount, py_obj, lltype.nullptr(Py_ssize_tP.TO)) == 1 ref = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw') assert generic_cpy_call(space, c_buf.c_bf_getsegcount, py_obj, ref) == 1 assert ref[0] == 10 lltype.free(ref, flavor='raw') ref = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw') assert generic_cpy_call(space, c_buf.c_bf_getreadbuffer, py_obj, 0, ref) == 10 lltype.free(ref, flavor='raw') decref(space, py_obj)
def deleter(self, space, w_self): assert isinstance(self, W_GetSetPropertyEx) check_descr(space, w_self, self.w_type) res = generic_cpy_call(space, self.getset.c_set, w_self, None, self.getset.c_closure) if rffi.cast(lltype.Signed, res) < 0: state = space.fromcache(State) state.check_and_raise_exception()
def wrap_sq_setitem(space, w_self, w_args, func): func_target = rffi.cast(ssizeobjargproc, func) check_num_args(space, w_args, 2) args_w = space.fixedview(w_args) index = space.int_w(space.index(args_w[0])) res = generic_cpy_call(space, func_target, w_self, index, args_w[1]) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True)
def wrap_getwritebuffer(space, w_self, w_args, func): func_target = rffi.cast(readbufferproc, func) with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as ptr: index = rffi.cast(Py_ssize_t, 0) size = generic_cpy_call(space, func_target, w_self, index, ptr) if size < 0: space.fromcache(State).check_and_raise_exception(always=True) return space.newbuffer(CPyBuffer(ptr[0], size, w_self, readonly=False))
def wrap_delitem(space, w_self, w_args, func): func_target = rffi.cast(objobjargproc, func) check_num_args(space, w_args, 1) w_key, = space.fixedview(w_args) res = generic_cpy_call(space, func_target, w_self, w_key, None) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.w_None
def wrap_delattr(space, w_self, w_args, func): func_target = rffi.cast(setattrofunc, func) check_num_args(space, w_args, 1) w_name, = space.fixedview(w_args) # XXX "Carlo Verre hack"? res = generic_cpy_call(space, func_target, w_self, w_name, None) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True)
def wrap_sq_setitem(space, w_self, w_args, func): func_target = rffi.cast(ssizeobjargproc, func) check_num_args(space, w_args, 2) args_w = space.fixedview(w_args) index = space.int_w(space.index(args_w[0])) res = generic_cpy_call(space, func_target, w_self, index, args_w[1]) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True)
def wrap_getattr(space, w_self, w_args, func): func_target = rffi.cast(getattrfunc, func) check_num_args(space, w_args, 1) args_w = space.fixedview(w_args) name_ptr = rffi.str2charp(space.str_w(args_w[0])) try: return generic_cpy_call(space, func_target, w_self, name_ptr) finally: rffi.free_charp(name_ptr)
def wrap_inquirypred(space, w_self, w_args, func): func_inquiry = rffi.cast(inquiry, func) check_num_args(space, w_args, 0) args_w = space.fixedview(w_args) res = generic_cpy_call(space, func_inquiry, w_self) res = rffi.cast(lltype.Signed, res) if res == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.wrap(bool(res))
def wrap_objobjproc(space, w_self, w_args, func): func_target = rffi.cast(objobjproc, func) check_num_args(space, w_args, 1) w_value, = space.fixedview(w_args) res = generic_cpy_call(space, func_target, w_self, w_value) res = rffi.cast(lltype.Signed, res) if res == -1: space.fromcache(State).check_and_raise_exception(always=True) return space.wrap(bool(res))
def wrap_ternaryfunc(space, w_self, w_args, func): # The third argument is optional func_ternary = rffi.cast(ternaryfunc, func) check_num_argsv(space, w_args, 1, 2) args_w = space.fixedview(w_args) arg3 = space.w_None if len(args_w) > 1: arg3 = args_w[1] return generic_cpy_call(space, func_ternary, w_self, args_w[0], arg3)
def wrap_getattr(space, w_self, w_args, func): func_target = rffi.cast(getattrfunc, func) check_num_args(space, w_args, 1) args_w = space.fixedview(w_args) name_ptr = rffi.str2charp(space.str_w(args_w[0])) try: return generic_cpy_call(space, func_target, w_self, name_ptr) finally: rffi.free_charp(name_ptr)
def PyObject_AsCharBuffer(space, obj, bufferp, sizep): """Returns a pointer to a read-only memory location usable as character-based input. The obj argument must support the single-segment character buffer interface. On success, returns 0, sets buffer to the memory location and size to the buffer length. Returns -1 and sets a TypeError on error. """ pto = obj.c_ob_type pb = pto.c_tp_as_buffer if not (pb and pb.c_bf_getreadbuffer and pb.c_bf_getsegcount): raise OperationError(space.w_TypeError, space.wrap("expected a character buffer object")) if generic_cpy_call(space, pb.c_bf_getsegcount, obj, lltype.nullptr(Py_ssize_tP.TO)) != 1: raise OperationError(space.w_TypeError, space.wrap("expected a single-segment buffer object")) size = generic_cpy_call(space, pb.c_bf_getcharbuffer, obj, 0, bufferp) if size < 0: return -1 sizep[0] = size return 0
def wrap_binaryfunc_l(space, w_self, w_args, func): func_binary = rffi.cast(binaryfunc, func) check_num_args(space, w_args, 1) args_w = space.fixedview(w_args) ref = make_ref(space, w_self) if (not ref.c_ob_type.c_tp_flags & Py_TPFLAGS_CHECKTYPES and not space.issubtype_w(space.type(args_w[0]), space.type(w_self))): return space.w_NotImplemented Py_DecRef(space, ref) return generic_cpy_call(space, func_binary, w_self, args_w[0])
def wrap_cmpfunc(space, w_self, w_args, func): func_target = rffi.cast(cmpfunc, func) check_num_args(space, w_args, 1) w_other, = space.fixedview(w_args) if not space.issubtype_w(space.type(w_self), space.type(w_other)): raise oefmt(space.w_TypeError, "%T.__cmp__(x,y) requires y to be a '%T', not a '%T'", w_self, w_self, w_other) return space.wrap(generic_cpy_call(space, func_target, w_self, w_other))
def wrap_cmpfunc(space, w_self, w_args, func): func_target = rffi.cast(cmpfunc, func) check_num_args(space, w_args, 1) w_other, = space.fixedview(w_args) if not space.is_true(space.issubtype(space.type(w_self), space.type(w_other))): raise OperationError(space.w_TypeError, space.wrap( "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'" % (space.type(w_self).getname(space), space.type(w_self).getname(space), space.type(w_other).getname(space)))) return space.wrap(generic_cpy_call(space, func_target, w_self, w_other))
def wrap_ternaryfunc_r(space, w_self, w_args, func): # The third argument is optional func_ternary = rffi.cast(ternaryfunc, func) check_num_argsv(space, w_args, 1, 2) args_w = space.fixedview(w_args) ref = make_ref(space, w_self) if (not ref.c_ob_type.c_tp_flags & Py_TPFLAGS_CHECKTYPES and not space.issubtype_w(space.type(args_w[0]), space.type(w_self))): return space.w_NotImplemented Py_DecRef(space, ref) arg3 = space.w_None if len(args_w) > 1: arg3 = args_w[1] return generic_cpy_call(space, func_ternary, args_w[0], w_self, arg3)
def wrap_descr_get(space, w_self, w_args, func): func_target = rffi.cast(descrgetfunc, func) args_w = space.fixedview(w_args) if len(args_w) == 1: w_obj, = args_w w_type = None elif len(args_w) == 2: w_obj, w_type = args_w else: raise oefmt(space.w_TypeError, "expected 1 or 2 arguments, got %d", len(args_w)) if w_obj is space.w_None: w_obj = None if w_type is space.w_None: w_type = None if w_obj is None and w_type is None: raise oefmt(space.w_TypeError, "__get__(None, None) is invalid") return generic_cpy_call(space, func_target, w_self, w_obj, w_type)
def tp_new_wrapper(space, self, w_args, w_kwds): tp_new = rffi.cast(PyTypeObjectPtr, self).c_tp_new # Check that the user doesn't do something silly and unsafe like # object.__new__(dict). To do this, we check that the most # derived base that's not a heap type is this type. # XXX do it args_w = space.fixedview(w_args) w_subtype = args_w[0] w_args = space.newtuple(args_w[1:]) subtype = rffi.cast(PyTypeObjectPtr, make_ref(space, w_subtype)) try: w_obj = generic_cpy_call(space, tp_new, subtype, w_args, w_kwds) finally: Py_DecRef(space, w_subtype) return w_obj
def wrap_binaryfunc(space, w_self, w_args, func): func_binary = rffi.cast(binaryfunc, func) check_num_args(space, w_args, 1) args_w = space.fixedview(w_args) return generic_cpy_call(space, func_binary, w_self, args_w[0])
def wrap_unaryfunc(space, w_self, w_args, func): func_unary = rffi.cast(unaryfunc, func) check_num_args(space, w_args, 0) return generic_cpy_call(space, func_unary, w_self)
def wrap_init(space, w_self, w_args, func, w_kwargs): func_init = rffi.cast(initproc, func) res = generic_cpy_call(space, func_init, w_self, w_args, w_kwargs) if rffi.cast(lltype.Signed, res) == -1: space.fromcache(State).check_and_raise_exception(always=True) return None