Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r62189:60088715348f
Date: 2013-03-07 14:14 -0800
http://bitbucket.org/pypy/pypy/changeset/60088715348f/

Log:    use the new space.sequence_index for operator.indexOf for
        consistency's sake (though this may be slower until sequence_index
        gets a jitdriver)

diff --git a/pypy/module/operator/__init__.py b/pypy/module/operator/__init__.py
--- a/pypy/module/operator/__init__.py
+++ b/pypy/module/operator/__init__.py
@@ -17,7 +17,7 @@
 
     appleveldefs = {} 
     
-    app_names = ['countOf', 'indexOf',
+    app_names = ['countOf',
                  'attrgetter', 'itemgetter', 'methodcaller',
              ]
 
@@ -34,7 +34,8 @@
                     'sub', 'truediv', 'truth', 'xor',
                     'iadd', 'iand', 'iconcat', 'ifloordiv',
                     'ilshift', 'imod', 'imul', 'ior', 'ipow',
-                    'irshift', 'isub', 'itruediv', 'ixor', '_length_hint']
+                    'irshift', 'isub', 'itruediv', 'ixor', '_length_hint',
+                    'indexOf']
 
     interpleveldefs = {}
 
diff --git a/pypy/module/operator/app_operator.py 
b/pypy/module/operator/app_operator.py
--- a/pypy/module/operator/app_operator.py
+++ b/pypy/module/operator/app_operator.py
@@ -14,16 +14,6 @@
             count += 1
     return count
 
-def indexOf(a, b):
-    'indexOf(a, b) -- Return the first index of b in a.'
-    index = 0
-    for x in a:
-        if x == b:
-            return index
-        index += 1
-    raise ValueError('sequence.index(x): x not in sequence')
-
-
 def attrgetter(attr, *attrs):
     if attrs:
         getters = [single_attr_getter(a) for a in (attr,) + attrs]
diff --git a/pypy/module/operator/interp_operator.py 
b/pypy/module/operator/interp_operator.py
--- a/pypy/module/operator/interp_operator.py
+++ b/pypy/module/operator/interp_operator.py
@@ -54,7 +54,9 @@
     'gt(a, b) -- Same as a>b.'
     return space.gt(w_a, w_b)
 
-# indexOf
+def indexOf(space, w_a, w_b):
+    'indexOf(a, b) -- Return the first index of b in a.'
+    return space.sequence_index(w_a, w_b)
 
 def inv(space, w_obj,):
     'inv(a) -- Same as ~a.'
diff --git a/pypy/module/operator/test/test_operator.py 
b/pypy/module/operator/test/test_operator.py
--- a/pypy/module/operator/test/test_operator.py
+++ b/pypy/module/operator/test/test_operator.py
@@ -173,3 +173,10 @@
         assert methodcaller("method", 4)(x) == (4, 3)
         assert methodcaller("method", 4, 5)(x) == (4, 5)
         assert methodcaller("method", 4, arg2=42)(x) == (4, 42)
+
+    def test_indexOf(self):
+        import operator
+        raises(TypeError, operator.indexOf)
+        raises(TypeError, operator.indexOf, None, None)
+        assert operator.indexOf([4, 3, 2, 1], 3) == 1
+        raises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to