[pypy-svn] r72982 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test
fijal at codespeak.net
fijal at codespeak.net
Sat Mar 27 22:19:58 CET 2010
Author: fijal
Date: Sat Mar 27 22:19:56 2010
New Revision: 72982
Added:
pypy/branch/cpython-extension/pypy/module/cpyext/listobject.py (contents, props changed)
pypy/branch/cpython-extension/pypy/module/cpyext/test/test_listobject.py (contents, props changed)
Modified:
pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py
pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
Log:
Implement a couple of list operations
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/__init__.py Sat Mar 27 22:19:56 2010
@@ -41,5 +41,6 @@
import pypy.module.cpyext.tupleobject
import pypy.module.cpyext.dictobject
import pypy.module.cpyext.intobject
+import pypy.module.cpyext.listobject
# now that all rffi_platform.Struct types are registered, configure them
api.configure_types()
Added: pypy/branch/cpython-extension/pypy/module/cpyext/listobject.py
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/listobject.py Sat Mar 27 22:19:56 2010
@@ -0,0 +1,38 @@
+
+from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.module.cpyext.api import cpython_api, PyObject, CANNOT_FAIL,\
+ Py_ssize_t
+from pypy.module.cpyext.api import general_check
+from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
+from pypy.module.cpyext.macros import Py_XDECREF
+from pypy.objspace.std.listobject import W_ListObject
+from pypy.interpreter.error import OperationError
+
+ at cpython_api([Py_ssize_t], PyObject)
+def PyList_New(space, len):
+ """Return a new list of length len on success, or NULL on failure.
+
+ If length is greater than zero, the returned list object's items are
+ set to NULL. Thus you cannot use abstract API functions such as
+ PySequence_SetItem() or expose the object to Python code before
+ setting all items to a real object with PyList_SetItem().
+ """
+ return space.newlist([None] * len)
+
+ at cpython_api([PyObject, Py_ssize_t, PyObject], rffi.INT_real, error=-1)
+def PyList_SetItem(space, w_list, index, w_item):
+ """Set the item at index index in list to item. Return 0 on success
+ or -1 on failure.
+
+ This function "steals" a reference to item and discards a reference to
+ an item already in the list at the affected position.
+ """
+ Py_XDECREF(space, w_item)
+ if not isinstance(w_list, W_ListObject):
+ PyErr_BadInternalCall()
+ wrappeditems = w_list.wrappeditems
+ if index < 0 or index >= len(wrappeditems):
+ raise OperationError(space.w_IndexError, space.wrap(
+ "list assignment index out of range"))
+ wrappeditems[index] = w_item
+ return 0
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py Sat Mar 27 22:19:56 2010
@@ -3452,19 +3452,6 @@
"""
raise NotImplementedError
- at cpython_api([Py_ssize_t], PyObject)
-def PyList_New(space, len):
- """Return a new list of length len on success, or NULL on failure.
-
- If length is greater than zero, the returned list object's items are
- set to NULL. Thus you cannot use abstract API functions such as
- PySequence_SetItem() or expose the object to Python code before
- setting all items to a real object with PyList_SetItem().
-
- This function used an int for size. This might require
- changes in your code for properly supporting 64-bit systems."""
- raise NotImplementedError
-
@cpython_api([PyObject], Py_ssize_t)
def PyList_Size(space, list):
"""
Added: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_listobject.py
==============================================================================
--- (empty file)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_listobject.py Sat Mar 27 22:19:56 2010
@@ -0,0 +1,44 @@
+
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+
+class AppTestListObject(AppTestCpythonExtensionBase):
+ def test_listobject(self):
+ import sys
+ module = self.import_extension('foo', [
+ ("newlist", "METH_NOARGS",
+ """
+ PyObject *lst = PyList_New(3);
+ PyList_SetItem(lst, 0, PyInt_FromLong(3));
+ PyList_SetItem(lst, 2, PyInt_FromLong(1000));
+ PyList_SetItem(lst, 1, PyInt_FromLong(-5));
+ return lst;
+ """
+ ),
+ ("setlistitem", "METH_VARARGS",
+ """
+ PyObject *l = PyTuple_GetItem(args, 0);
+ int index = PyInt_AsLong(PyTuple_GetItem(args, 1));
+ Py_INCREF(Py_None);
+ int res = PyList_SetItem(l, index, Py_None);
+ if (res == -1) {
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+ """
+ )
+ ])
+ l = module.newlist()
+ assert l == [3, -5, 1000]
+ module.setlistitem(l, 0)
+ assert l[0] is None
+
+ class L(list):
+ def __setitem__(self):
+ self.append("XYZ")
+
+ l = L([1])
+ module.setlistitem(l, 0)
+ assert len(l) == 1
+
+ raises(TypeError, module.setlistitem, (1, 2, 3), 0)
More information about the Pypy-commit
mailing list