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

afa at codespeak.net afa at codespeak.net
Mon Apr 26 14:34:20 CEST 2010


Author: afa
Date: Mon Apr 26 14:34:18 2010
New Revision: 74071

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


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	Mon Apr 26 14:34:18 2010
@@ -62,6 +62,14 @@
     w_list.append(w_item)
     return 0
 
+ at cpython_api([PyObject, Py_ssize_t, PyObject], rffi.INT_real, error=-1)
+def PyList_Insert(space, w_list, index, w_item):
+    """Insert the item item into list list in front of index index.  Return
+    0 if successful; return -1 and set an exception if unsuccessful.
+    Analogous to list.insert(index, item)."""
+    space.call_method(w_list, "insert", space.wrap(index), w_item)
+    return 0
+
 @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL)
 def PyList_GET_SIZE(space, w_list):
     """Macro form of PyList_Size() without error checking.

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	Mon Apr 26 14:34:18 2010
@@ -32,6 +32,16 @@
         assert api.PyList_Size(space.w_None) == -1
         assert api.PyErr_Occurred() is space.w_TypeError
         api.PyErr_Clear()
+
+    def test_insert(self, space, api):
+        w_l = space.newlist([space.w_None, space.w_None])
+        assert api.PyList_Insert(w_l, 0, space.wrap(1)) == 0
+        assert api.PyList_Size(w_l) == 3
+        assert api.PyList_Insert(w_l, 99, space.wrap(2)) == 0
+        assert space.unwrap(api.PyList_GetItem(w_l, 3)) == 2
+        # insert at index -1: next-to-last
+        assert api.PyList_Insert(w_l, -1, space.wrap(3)) == 0
+        assert space.unwrap(api.PyList_GetItem(w_l, 3)) == 3
     
     def test_sort(self, space, api):
         l = space.newlist([space.wrap(1), space.wrap(0), space.wrap(7000)])



More information about the Pypy-commit mailing list