[pypy-svn] r72998 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test
fijal at codespeak.net
fijal at codespeak.net
Sat Mar 27 23:44:09 CET 2010
Author: fijal
Date: Sat Mar 27 23:44:07 2010
New Revision: 72998
Modified:
pypy/branch/cpython-extension/pypy/module/cpyext/sequence.py
pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
pypy/branch/cpython-extension/pypy/module/cpyext/test/test_sequence.py
Log:
(zooko, fijal) Add PySequence_Fast_GET_ITEM
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/sequence.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/sequence.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/sequence.py Sat Mar 27 23:44:07 2010
@@ -1,8 +1,9 @@
from pypy.interpreter.error import OperationError
from pypy.module.cpyext.api import cpython_api, PyObject, CANNOT_FAIL,\
- Py_ssize_t
+ Py_ssize_t, register_container
from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.objspace.std import listobject, tupleobject
@cpython_api([PyObject, rffi.CCHARP], PyObject)
def PySequence_Fast(space, w_obj, m):
@@ -17,3 +18,17 @@
return space.newtuple(space.unpackiterable(w_obj))
except OperationError:
raise OperationError(space.w_TypeError, space.wrap(rffi.charp2str(m)))
+
+ at cpython_api([PyObject, Py_ssize_t], PyObject, borrowed=True)
+def PySequence_Fast_GET_ITEM(space, w_obj, index):
+ """Return the ith element of o, assuming that o was returned by
+ PySequence_Fast(), o is not NULL, and that i is within bounds.
+ """
+ if isinstance(w_obj, listobject.W_ListObject):
+ w_res = w_obj.wrappeditems[index]
+ else:
+ assert isinstance(w_obj, tupleobject.W_TupleObject)
+ w_res = w_obj.wrappeditems[index]
+ register_container(space, w_obj)
+ return w_res
+
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 23:44:07 2010
@@ -5081,23 +5081,6 @@
equivalent to the Python expression tuple(o)."""
raise NotImplementedError
- at cpython_api([PyObject, rffi.CCHARP], PyObject)
-def PySequence_Fast(space, o, m):
- """Returns the sequence o as a tuple, unless it is already a tuple or list, in
- which case o is returned. Use PySequence_Fast_GET_ITEM() to access the
- members of the result. Returns NULL on failure. If the object is not a
- sequence, raises TypeError with m as the message text."""
- raise NotImplementedError
-
- at cpython_api([PyObject, Py_ssize_t], PyObject, borrowed=True)
-def PySequence_Fast_GET_ITEM(space, o, i):
- """Return the ith element of o, assuming that o was returned by
- PySequence_Fast(), o is not NULL, and that i is within bounds.
-
- This function used an int type for i. This might require
- changes in your code for properly supporting 64-bit systems."""
- raise NotImplementedError
-
@cpython_api([PyObject], {PyObject**})
def PySequence_Fast_ITEMS(space, o):
"""Return the underlying array of PyObject pointers. Assumes that o was returned
Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_sequence.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_sequence.py (original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_sequence.py Sat Mar 27 23:44:07 2010
@@ -11,6 +11,13 @@
return PySequence_Fast(PyTuple_GetItem(args, 0), "message");
'''
),
+ ("fast_getitem", "METH_VARARGS",
+ '''
+ PyObject *lst = PyTuple_GetItem(args, 0);
+ long index = PyInt_AsLong(PyTuple_GetItem(args, 1));
+ return PySequence_Fast_GET_ITEM(lst, index);
+ '''
+ ),
])
t = (1, 2, 3, 4)
assert module.newiter(t) is t
@@ -24,3 +31,4 @@
assert te.args == ("message",)
else:
raise Exception("DID NOT RAISE")
+ assert module.fast_getitem((1, 2, 3), 1) == 2
More information about the Pypy-commit
mailing list