[pypy-svn] r75627 - in pypy/branch/fast-forward/pypy/objspace/std: . test
benjamin at codespeak.net
benjamin at codespeak.net
Mon Jun 28 02:06:00 CEST 2010
Author: benjamin
Date: Mon Jun 28 02:05:58 2010
New Revision: 75627
Modified:
pypy/branch/fast-forward/pypy/objspace/std/test/test_tupleobject.py
pypy/branch/fast-forward/pypy/objspace/std/tupleobject.py
pypy/branch/fast-forward/pypy/objspace/std/tupletype.py
Log:
add index and count methods to tuple
Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_tupleobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_tupleobject.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_tupleobject.py Mon Jun 28 02:05:58 2010
@@ -328,6 +328,21 @@
assert repr((1,)) == '(1,)'
assert repr(()) == '()'
assert repr((1,2,3)) == '(1, 2, 3)'
-
+
def test_getslice(self):
assert ('a', 'b', 'c').__getslice__(-17, 2) == ('a', 'b')
+
+ def test_count(self):
+ assert ().count(4) == 0
+ assert (1, 2, 3, 4).count(3) == 1
+ assert (1, 2, 3, 4).count(5) == 0
+ assert (1, 1, 1).count(1) == 3
+
+ def test_index(self):
+ raises(ValueError, ().index, 4)
+ (1, 2).index(1) == 0
+ (3, 4, 5).index(4) == 1
+ raises(ValueError, (1, 2, 3, 4).index, 5)
+ assert (4, 2, 3, 4).index(4, 1) == 3
+ assert (4, 4, 4).index(4, 1, 2) == 1
+ raises(ValueError, (1, 2, 3, 4).index, 4, 0, 2)
Modified: pypy/branch/fast-forward/pypy/objspace/std/tupleobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/tupleobject.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/tupleobject.py Mon Jun 28 02:05:58 2010
@@ -5,6 +5,7 @@
from pypy.objspace.std.multimethod import FailedToImplement
from pypy.rlib.rarithmetic import intmask
from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
+from pypy.objspace.std import slicetype
from pypy.interpreter import gateway
from pypy.rlib.debug import make_sure_not_resized
@@ -160,4 +161,31 @@
def getnewargs__Tuple(space, w_tuple):
return space.newtuple([W_TupleObject(w_tuple.wrappeditems)])
-register_all(vars())
+def tuple_count__Tuple_ANY(space, w_tuple, w_obj):
+ count = 0
+ for w_item in w_tuple.wrappeditems:
+ if space.eq_w(w_item, w_obj):
+ count += 1
+ return space.wrap(count)
+
+def tuple_index__Tuple_ANY_ANY_ANY(space, w_tuple, w_obj, w_start, w_stop):
+ start = slicetype._Eval_SliceIndex(space, w_start)
+ stop = slicetype._Eval_SliceIndex(space, w_stop)
+ length = len(w_tuple.wrappeditems)
+ if start < 0:
+ start += length
+ if start < 0:
+ start = 0
+ if stop < 0:
+ stop += length
+ if stop < 0:
+ stop = 0
+ for i in range(start, min(stop, length)):
+ w_item = w_tuple.wrappeditems[i]
+ if space.eq_w(w_item, w_obj):
+ return space.wrap(i)
+ raise OperationError(space.w_ValueError,
+ space.wrap("tuple.index(x): x not in tuple"))
+
+from pypy.objspace.std import tupletype
+register_all(vars(), tupletype)
Modified: pypy/branch/fast-forward/pypy/objspace/std/tupletype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/tupletype.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/tupletype.py Mon Jun 28 02:05:58 2010
@@ -1,5 +1,16 @@
+import sys
from pypy.interpreter import gateway
-from pypy.objspace.std.stdtypedef import StdTypeDef
+from pypy.objspace.std.register_all import register_all
+from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
+
+
+tuple_count = SMM("count", 2,
+ doc="count(obj) -> number of times obj appears in the tuple")
+
+tuple_index = SMM("index", 4, defaults=(0, sys.maxint),
+ doc="index(obj, [start, [stop]]) -> first index that obj "
+ "appears in the tuple")
+
def descr__new__(space, w_tupletype, w_sequence=gateway.NoneNotWrapped):
from pypy.objspace.std.tupleobject import W_TupleObject
@@ -23,3 +34,4 @@
If the argument is a tuple, the return value is the same object.''',
__new__ = gateway.interp2app(descr__new__),
)
+tuple_typedef.registermethods(globals())
More information about the Pypy-commit
mailing list