[pypy-svn] r74020 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Fri Apr 23 15:23:44 CEST 2010


Author: afa
Date: Fri Apr 23 15:23:42 2010
New Revision: 74020

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/listobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_listobject.py
Log:
PyList_SetSlice


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/listobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/listobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/listobject.py	Fri Apr 23 15:23:42 2010
@@ -102,3 +102,17 @@
     space.call_method(w_list, "reverse")
     return 0
 
+ at cpython_api([PyObject, Py_ssize_t, Py_ssize_t, PyObject], rffi.INT_real, error=-1)
+def PyList_SetSlice(space, w_list, low, high, w_sequence):
+    """Set the slice of list between low and high to the contents of
+    itemlist.  Analogous to list[low:high] = itemlist. The itemlist may
+    be NULL, indicating the assignment of an empty list (slice deletion).
+    Return 0 on success, -1 on failure.  Negative indices, as when
+    slicing from Python, are not supported."""
+    w_start = space.wrap(low)
+    w_stop = space.wrap(high)
+    if w_sequence:
+        space.setslice(w_list, w_start, w_stop, w_sequence)
+    else:
+        space.delslice(w_list, w_start, w_stop)
+    return 0

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_listobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_listobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_listobject.py	Fri Apr 23 15:23:42 2010
@@ -74,6 +74,16 @@
              Py_RETURN_NONE;
              """
              ),
+             ("setslice", "METH_VARARGS",
+             """
+             PyObject *l = PyTuple_GetItem(args, 0);
+             PyObject *seq = PyTuple_GetItem(args, 1);
+             if (seq == Py_None) seq = NULL;
+             if (PyList_SetSlice(l, 1, 4, seq) < 0)
+                 return NULL;
+             Py_RETURN_NONE;
+             """
+             ),
             ])
         l = module.newlist()
         assert l == [3, -5, 1000]
@@ -95,3 +105,11 @@
         assert len(l) == 1
         assert l[0] == 14
 
+        l = range(6)
+        module.setslice(l, ['a'])
+        assert l == [0, 'a', 4, 5]
+
+        l = range(6)
+        module.setslice(l, None)
+        assert l == [0, 4, 5]
+



More information about the Pypy-commit mailing list