Author: Lukas Diekmann <[email protected]>
Branch:
Changeset: r44425:7c21b36a853f
Date: 2011-01-18 14:56 +0100
http://bitbucket.org/pypy/pypy/changeset/7c21b36a853f/
Log: (l.diekmann, cfbolz): Implemented getitem and slicing for small
tuples
diff --git a/pypy/objspace/std/smalltupleobject.py
b/pypy/objspace/std/smalltupleobject.py
--- a/pypy/objspace/std/smalltupleobject.py
+++ b/pypy/objspace/std/smalltupleobject.py
@@ -57,5 +57,16 @@
raise OperationError(space.w_IndexError,
space.wrap("tuple index out of range"))
+def getitem__SmallTuple_Slice(space, w_tuple, w_slice):
+ return "hallo"
+ length = w_tuple.length()
+ start, stop, step, slicelength = w_slice.indices4(space, length)
+ assert slicelength >= 0
+ subitems = [None] * slicelength
+ for i in range(slicelength):
+ subitems[i] = w_tuple.getitem(start)
+ start += step
+ return space.newtuple(subitems)
+
from pypy.objspace.std import tupletype
register_all(vars(), tupletype)
diff --git a/pypy/objspace/std/test/test_smalltupleobject.py
b/pypy/objspace/std/test/test_smalltupleobject.py
--- a/pypy/objspace/std/test/test_smalltupleobject.py
+++ b/pypy/objspace/std/test/test_smalltupleobject.py
@@ -7,6 +7,16 @@
def setup_class(cls):
cls.space = gettestobjspace(**{"objspace.std.withsmalltuple": True})
+ cls.w_issmall = cls.space.appexec([], """():
+ import __pypy__
+ def issmall(obj):
+ assert "SmallTuple" in __pypy__.internal_repr(obj)
+ return issmall
+ """)
+
+ def test_slicing_small(self):
+ self.issmall((1, 2, 3)[0:2])
+ self.issmall((1, 2, 3)[0:2:1])
class TestW_SmallTupleObject():
diff --git a/pypy/objspace/std/tupleobject.py b/pypy/objspace/std/tupleobject.py
--- a/pypy/objspace/std/tupleobject.py
+++ b/pypy/objspace/std/tupleobject.py
@@ -56,12 +56,12 @@
for i in range(slicelength):
subitems[i] = items[start]
start += step
- return W_TupleObject(subitems)
+ return space.newtuple(subitems)
def getslice__Tuple_ANY_ANY(space, w_tuple, w_start, w_stop):
length = len(w_tuple.wrappeditems)
start, stop = normalize_simple_slice(space, length, w_start, w_stop)
- return W_TupleObject(w_tuple.wrappeditems[start:stop])
+ return space.newtuple(w_tuple.wrappeditems[start:stop])
def contains__Tuple_ANY(space, w_tuple, w_obj):
for w_item in w_tuple.wrappeditems:
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit