[pypy-commit] pypy cpyext-fast-typecheck: add a test for sq_ass_slice and port the corresponding wrapper to the new style
antocuni
pypy.commits at gmail.com
Thu Mar 22 18:41:59 EDT 2018
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: cpyext-fast-typecheck
Changeset: r94101:564712927aef
Date: 2018-03-22 23:17 +0100
http://bitbucket.org/pypy/pypy/changeset/564712927aef/
Log: add a test for sq_ass_slice and port the corresponding wrapper to
the new style
diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -269,16 +269,17 @@
w_kwargs = w_kwargs_from_args(space, __args__)
return generic_cpy_call(space, func_target, w_self, py_args, w_kwargs)
-def wrap_ssizessizeobjargproc(space, w_self, w_args, func):
- func_target = rffi.cast(ssizessizeobjargproc, func)
- check_num_args(space, w_args, 3)
- args_w = space.fixedview(w_args)
- i = space.int_w(space.index(args_w[0]))
- j = space.int_w(space.index(args_w[1]))
- w_y = args_w[2]
- res = generic_cpy_call(space, func_target, w_self, i, j, w_y)
- if rffi.cast(lltype.Signed, res) == -1:
- space.fromcache(State).check_and_raise_exception(always=True)
+class wrap_ssizessizeobjargproc(W_PyCWrapperObject):
+ def call(self, space, w_self, __args__):
+ self.check_args(__args__, 3)
+ func = self.get_func_to_call()
+ func_target = rffi.cast(ssizessizeobjargproc, func)
+ i = space.int_w(space.index(__args__.arguments_w[0]))
+ j = space.int_w(space.index(__args__.arguments_w[1]))
+ w_y = __args__.arguments_w[2]
+ res = generic_cpy_call(space, func_target, w_self, i, j, w_y)
+ if rffi.cast(lltype.Signed, res) == -1:
+ space.fromcache(State).check_and_raise_exception(always=True)
class wrap_lenfunc(W_PyCWrapperObject):
def call(self, space, w_self, __args__):
@@ -290,12 +291,14 @@
space.fromcache(State).check_and_raise_exception(always=True)
return space.newint(res)
-def wrap_sq_item(space, w_self, w_args, func):
- func_target = rffi.cast(ssizeargfunc, func)
- check_num_args(space, w_args, 1)
- args_w = space.fixedview(w_args)
- index = space.int_w(space.index(args_w[0]))
- return generic_cpy_call(space, func_target, w_self, index)
+class wrap_sq_item(W_PyCWrapperObject):
+ 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 wrap_sq_setitem(space, w_self, w_args, func):
func_target = rffi.cast(ssizeobjargproc, func)
diff --git a/pypy/module/cpyext/test/test_typeobject.py b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -889,6 +889,43 @@
res = "foo" in obj
assert res is True
+ def test_sq_ass_slice(self):
+ module = self.import_extension('foo', [
+ ("new_obj", "METH_NOARGS",
+ '''
+ PyObject *obj;
+ obj = PyObject_New(PyObject, &Foo_Type);
+ return obj;
+ '''
+ )], prologue='''
+ static int
+ sq_ass_slice(PyObject *self, Py_ssize_t a, Py_ssize_t b, PyObject *o)
+ {
+ int expected = (a == 10 && b == 20 &&
+ PyInt_Check(o) && PyInt_AsLong(o) == 42);
+ if (!expected) {
+ PyErr_SetString(PyExc_ValueError, "test failed");
+ return -1;
+ }
+ return 0;
+ }
+ PySequenceMethods tp_as_sequence;
+ static PyTypeObject Foo_Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "foo.foo",
+ };
+ ''', more_init='''
+ Foo_Type.tp_flags = Py_TPFLAGS_DEFAULT;
+ Foo_Type.tp_as_sequence = &tp_as_sequence;
+ tp_as_sequence.sq_ass_slice = sq_ass_slice;
+ if (PyType_Ready(&Foo_Type) < 0) INITERROR;
+ ''')
+ obj = module.new_obj()
+ obj[10:20] = 42
+ raises(ValueError, "obj[10:20] = 43")
+ raises(ValueError, "obj[11:20] = 42")
+ raises(ValueError, "obj[10:21] = 42")
+
def test_tp_iter(self):
module = self.import_extension('foo', [
("tp_iter", "METH_VARARGS",
More information about the pypy-commit
mailing list