[pypy-svn] r8564 - pypy/dist/pypy/appspace

arigo at codespeak.net arigo at codespeak.net
Tue Jan 25 15:29:03 CET 2005


Author: arigo
Date: Tue Jan 25 15:29:03 2005
New Revision: 8564

Modified:
   pypy/dist/pypy/appspace/operator.py
Log:
Fixed the operator module.


Modified: pypy/dist/pypy/appspace/operator.py
==============================================================================
--- pypy/dist/pypy/appspace/operator.py	(original)
+++ pypy/dist/pypy/appspace/operator.py	Tue Jan 25 15:29:03 2005
@@ -1,6 +1,8 @@
+import __builtin__
+
 def abs(obj,):
     'abs(a) -- Same as abs(a).'
-    return abs(obj)
+    return __builtin__.abs(obj)
 __abs__ = abs
 def add(obj1, obj2):
     'add(a, b) -- Same as a + b.'
@@ -12,14 +14,18 @@
 __and__ = and_
 def concat(obj1, obj2):
     'concat(a, b) -- Same as a + b, for a and b sequences.'
-    return obj1 + obj2  # XXX 
+    return obj1 + obj2  # XXX should we be stricter?
 def contains(obj1,obj2):
     'contains(a, b) -- Same as b in a (note reversed operands).'
     return obj2 in obj1 
 __contains__ = contains
 def countOf(a,b): 
     'countOf(a, b) -- Return the number of times b occurs in a.'
-    raise NotImplementedError 
+    count = 0
+    for x in a:
+        if x == b:
+            count += 1
+    return count
 def delitem(obj, key):
     'delitem(a, b) -- Same as del a[b].'
     del obj[key]
@@ -58,7 +64,12 @@
 __gt__ = gt
 def indexOf(a, b):
     'indexOf(a, b) -- Return the first index of b in a.'
-    raise NotImplementedError
+    index = 0
+    for x in a:
+        if x == b:
+            return index
+        index += 1
+    raise ValueError, 'sequence.index(x): x not in sequence'
 def inv(obj,):
     'inv(a) -- Same as ~a.'
     return ~obj 
@@ -70,15 +81,18 @@
 def isCallable(obj,):
     'isCallable(a) -- Same as callable(a).'
     return callable(obj) 
+
+# XXX the following is approximative
 def isMappingType(obj,):
     'isMappingType(a) -- Return True if a has a mapping type, False otherwise.'
-    return hasattr(obj, '__getitem__') # Xxx only close 
+    return hasattr(obj, '__getitem__') and hasattr(obj, 'keys')
 def isNumberType(obj,):
     'isNumberType(a) -- Return True if a has a numeric type, False otherwise.'
     return hasattr(obj, '__int__') or hasattr(obj, '__float__') 
 def isSequenceType(obj,):
     'isSequenceType(a) -- Return True if a has a sequence type, False otherwise.'
-    return hasattr(obj, '__getitem__') # Xxx only close 
+    return hasattr(obj, '__getitem__')
+
 def is_(a, b):
     'is_(a, b) -- Same as a is b.'
     return a is b 
@@ -111,7 +125,7 @@
 __ne__ = ne
 def neg(obj,):
     'neg(a) -- Same as -a.'
-    return -a 
+    return -obj
 __neg__ = neg
 def not_(obj,):
     'not_(a) -- Same as not a.'
@@ -130,14 +144,19 @@
 __pow__ = pow
 def repeat(obj, num):
     'repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.'
-    return obj * num 
+    if not isinstance(num, (int, long)):
+        raise TypeError, 'an integer is required'
+    return obj * num   # XXX should we be stricter?
 def rshift(a, b):
     'rshift(a, b) -- Same as a >> b.'
     return a >> b 
 __rshift__ = rshift
 def sequenceIncludes(a, b):
     'sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).'
-    raise NotImplementedError 
+    for x in a:
+        if x == b:
+            return True
+    return False
 def setitem(obj, key, value):
     'setitem(a, b, c) -- Same as a[b] = c.'
     obj[key] = value 
@@ -150,9 +169,12 @@
     'sub(a, b) -- Same as a - b.'
     return a - b 
 __sub__ = sub
+
+exec """from __future__ import division
 def truediv(a, b):
     'truediv(a, b) -- Same as a / b when __future__.division is in effect.'
     return a / b 
+"""
 __truediv__ = truediv
 def truth(a,):
     'truth(a) -- Return True if a is true, False otherwise.'



More information about the Pypy-commit mailing list