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

jandem at codespeak.net jandem at codespeak.net
Sat Apr 10 15:01:54 CEST 2010


Author: jandem
Date: Sat Apr 10 15:01:53 2010
New Revision: 73617

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/listobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_listobject.py
Log:
Add PyList_Size, with tests


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	Sat Apr 10 15:01:53 2010
@@ -70,3 +70,16 @@
     code for properly supporting 64-bit systems."""
     assert isinstance(w_list, W_ListObject)
     return len(w_list.wrappeditems)
+
+
+ at cpython_api([PyObject], Py_ssize_t, error=-1)
+def PyList_Size(space, ref):
+    """Return the length of the list object in list; this is equivalent to
+    len(list) on a list object.
+    
+    This function returned an int. This might require changes in
+    your code for properly supporting 64-bit systems."""
+    if not PyList_Check(space, ref):
+        raise OperationError(space.w_TypeError,
+                             space.wrap("expected list object"))
+    return PyList_GET_SIZE(space, ref)

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	Sat Apr 10 15:01:53 2010
@@ -25,6 +25,13 @@
         assert api.PyList_GET_SIZE(l) == 0
         api.PyList_Append(l, space.wrap(3))
         assert api.PyList_GET_SIZE(l) == 1
+    
+    def test_size(self, space, api):
+        l = space.newlist([space.w_None, space.w_None])
+        assert api.PyList_Size(l) == 2
+        assert api.PyList_Size(space.w_None) == -1
+        assert api.PyErr_Occurred() is space.w_TypeError
+        api.PyErr_Clear()
 
 class AppTestListObject(AppTestCpythonExtensionBase):
     def test_listobject(self):



More information about the Pypy-commit mailing list