[pypy-commit] pypy cpyext-fast-typecheck: test for sq_ass_item and fix

antocuni pypy.commits at gmail.com
Thu Mar 22 18:42:01 EDT 2018


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: cpyext-fast-typecheck
Changeset: r94102:150b6a41b9f2
Date: 2018-03-22 23:24 +0100
http://bitbucket.org/pypy/pypy/changeset/150b6a41b9f2/

Log:	test for sq_ass_item and fix

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
@@ -300,14 +300,17 @@
         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)
-    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)
+class wrap_sq_setitem(W_PyCWrapperObject):
+    def call(self, space, w_self, __args__):
+        self.check_args(__args__, 2)
+        func = self.get_func_to_call()
+        func_target = rffi.cast(ssizeobjargproc, func)
+        w_index = __args__.arguments_w[0]
+        w_value = __args__.arguments_w[1]
+        index = space.int_w(space.index(w_index))
+        res = generic_cpy_call(space, func_target, w_self, index, w_value)
+        if rffi.cast(lltype.Signed, res) == -1:
+            space.fromcache(State).check_and_raise_exception(always=True)
 
 def wrap_sq_delitem(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
@@ -926,6 +926,41 @@
         raises(ValueError, "obj[11:20] = 42")
         raises(ValueError, "obj[10:21] = 42")
 
+    def test_sq_ass_item(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_item(PyObject *self, Py_ssize_t i, PyObject *o)
+            {
+                int expected = (i == 10 && 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_item = sq_ass_item;
+                if (PyType_Ready(&Foo_Type) < 0) INITERROR;
+            ''')
+        obj = module.new_obj()
+        obj[10] = 42
+        raises(ValueError, "obj[10] = 43")
+        raises(ValueError, "obj[11] = 42")
+
     def test_tp_iter(self):
         module = self.import_extension('foo', [
            ("tp_iter", "METH_VARARGS",


More information about the pypy-commit mailing list