Author: Timo Paulssen <[email protected]>
Branch:
Changeset: r44130:a5a39a7fdbc3
Date: 2011-05-12 10:47 +0200
http://bitbucket.org/pypy/pypy/changeset/a5a39a7fdbc3/
Log: implement PySequence_ITEM, base GetItem on it, with test case.
diff --git a/pypy/module/cpyext/sequence.py b/pypy/module/cpyext/sequence.py
--- a/pypy/module/cpyext/sequence.py
+++ b/pypy/module/cpyext/sequence.py
@@ -36,7 +36,6 @@
def PySequence_Length(space, w_obj):
return space.len_w(w_obj)
-
@cpython_api([PyObject, CONST_STRING], PyObject)
def PySequence_Fast(space, w_obj, m):
"""Returns the sequence o as a tuple, unless it is already a tuple or
list, in
@@ -96,10 +95,21 @@
return 0
@cpython_api([PyObject, Py_ssize_t], PyObject)
+def PySequence_ITEM(space, w_obj, i):
+ """Return the ith element of o or NULL on failure. Macro form of
+ PySequence_GetItem() but without checking that
+ PySequence_Check(o)() is true and without adjustment for negative
+ indices.
+
+ This function used an int type for i. This might require
+ changes in your code for properly supporting 64-bit systems."""
+ return space.getitem(w_obj, space.wrap(i))
+
+@cpython_api([PyObject, Py_ssize_t], PyObject)
def PySequence_GetItem(space, w_obj, i):
"""Return the ith element of o, or NULL on failure. This is the equivalent
of
the Python expression o[i]."""
- return space.getitem(w_obj, space.wrap(i))
+ return PySequence_ITEM(space, w_obj, i)
@cpython_api([PyObject], PyObject)
def PySequence_List(space, w_obj):
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -2032,17 +2032,6 @@
"""
raise NotImplementedError
-@cpython_api([PyObject, Py_ssize_t], PyObject)
-def PySequence_ITEM(space, o, i):
- """Return the ith element of o or NULL on failure. Macro form of
- PySequence_GetItem() but without checking that
- PySequence_Check(o)() is true and without adjustment for negative
- indices.
-
- 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], rffi.INT_real, error=CANNOT_FAIL)
def PySet_Check(space, p):
"""Return true if p is a set object or an instance of a subtype.
diff --git a/pypy/module/cpyext/test/test_sequence.py
b/pypy/module/cpyext/test/test_sequence.py
--- a/pypy/module/cpyext/test/test_sequence.py
+++ b/pypy/module/cpyext/test/test_sequence.py
@@ -106,6 +106,15 @@
self.raises(space, api, IndexError, api.PySequence_DelItem,
w_l, 3)
+ def test_getitem(self, space, api):
+ thelist = [8, 7, 6, 5, 4, 3, 2, 1]
+ w_l = space.wrap(thelist)
+
+ result = api.PySequence_GetItem(w_l, 4)
+ assert space.is_true(space.eq(result, space.wrap(4)))
+
+ self.raises(space, api, IndexError, api.PySequence_GetItem, w_l, 9000)
+
def test_index(self, space, api):
thelist = [9, 8, 7, 6, 5, 4, 3, 2, 1]
w_l = space.wrap(thelist)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit