[pypy-svn] r11626 - in pypy/dist: lib-python-2.3.4/test pypy/lib/test2 pypy/objspace/std

pedronis at codespeak.net pedronis at codespeak.net
Fri Apr 29 16:42:47 CEST 2005


Author: pedronis
Date: Fri Apr 29 16:42:47 2005
New Revision: 11626

Added:
   pypy/dist/pypy/lib/test2/test_coercion.py   (contents, props changed)
Modified:
   pypy/dist/lib-python-2.3.4/test/conftest.py
   pypy/dist/pypy/objspace/std/listobject.py
   pypy/dist/pypy/objspace/std/stdtypedef.py
   pypy/dist/pypy/objspace/std/stringobject.py
   pypy/dist/pypy/objspace/std/tupleobject.py
Log:
test_coercion passes after changes needed since str(TypeError) is different in PyPy because of new-styleness

marked as such in conftest, notice that it also needs to be run with oldstyle because it depends on __coerce__

delegation through descroperation did not work for str|list|tuple */*=, fixing that was only necessary.




Modified: pypy/dist/lib-python-2.3.4/test/conftest.py
==============================================================================
--- pypy/dist/lib-python-2.3.4/test/conftest.py	(original)
+++ pypy/dist/lib-python-2.3.4/test/conftest.py	Fri Apr 29 16:42:47 2005
@@ -353,7 +353,8 @@
         #rev 10840: Uncaught interp-level exception: Same place as test_cfgparser
 
     RegrTest('test_codeop.py', enabled=True),
-    RegrTest('test_coercion.py', enabled=False),
+    RegrTest('test_coercion.py', enabled=False, oldstyle=True),
+        # needed changes because our exceptions are new-style and so have a different str(.) behavior
     RegrTest('test_commands.py', enabled=True),
     RegrTest('test_compare.py', enabled=True, oldstyle=True),
     RegrTest('test_compile.py', enabled=True),

Added: pypy/dist/pypy/lib/test2/test_coercion.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/test2/test_coercion.py	Fri Apr 29 16:42:47 2005
@@ -0,0 +1,118 @@
+import copy
+import sys
+import warnings
+
+# Fake a number that implements numeric methods through __coerce__
+class CoerceNumber:
+    def __init__(self, arg):
+        self.arg = arg
+
+    def __repr__(self):
+        return '<CoerceNumber %s>' % repr(self.arg)
+
+    def __coerce__(self, other):
+        if isinstance(other, CoerceNumber):
+            return self.arg, other.arg
+        else:
+            return (self.arg, other)
+
+
+# Fake a number that implements numeric ops through methods.
+class MethodNumber:
+
+    def __init__(self,arg):
+        self.arg = arg
+
+    def __repr__(self):
+        return '<MethodNumber %s>' % repr(self.arg)
+
+    def __add__(self,other):
+        return self.arg + other
+
+    def __radd__(self,other):
+        return other + self.arg
+
+    def __sub__(self,other):
+        return self.arg - other
+
+    def __rsub__(self,other):
+        return other - self.arg
+
+    def __mul__(self,other):
+        return self.arg * other
+
+    def __rmul__(self,other):
+        return other * self.arg
+
+    def __div__(self,other):
+        return self.arg / other
+
+    def __rdiv__(self,other):
+        return other / self.arg
+
+    def __pow__(self,other):
+        return self.arg ** other
+
+    def __rpow__(self,other):
+        return other ** self.arg
+
+    def __mod__(self,other):
+        return self.arg % other
+
+    def __rmod__(self,other):
+        return other % self.arg
+
+    def __cmp__(self, other):
+        return cmp(self.arg, other)
+
+
+candidates = [ 2, 4.0, 2L, 2+0j, [1], (2,), None,
+               MethodNumber(1), CoerceNumber(2)]
+
+infix_binops = [ '+', '-', '*', '/', '**', '%' ]
+prefix_binops = [ 'divmod' ]
+
+def do_infix_binops():
+    for a in candidates:
+        for b in candidates:
+            for op in infix_binops:
+                print '%s %s %s' % (a, op, b),
+                try:
+                    x = eval('a %s b' % op)
+                except:
+                    error = sys.exc_info()[:2]
+                    print '... exceptions.%s' % error[0].__name__
+                else:
+                    print '=', x
+                try:
+                    z = copy.copy(a)
+                except copy.Error:
+                    z = a # assume it has no inplace ops
+                print '%s %s= %s' % (a, op, b),
+                try:
+                    exec('z %s= b' % op)
+                except:
+                    error = sys.exc_info()[:2]
+                    print '... exceptions.%s' % error[0].__name__
+                else:
+                    print '=>', z
+
+def do_prefix_binops():
+    for a in candidates:
+        for b in candidates:
+            for op in prefix_binops:
+                print '%s(%s, %s)' % (op, a, b),
+                try:
+                    x = eval('%s(a, b)' % op)
+                except:
+                    error = sys.exc_info()[:2]
+                    print '... exceptions.%s' % error[0].__name__
+                else:
+                    print '=', x
+
+warnings.filterwarnings("ignore",
+                        r'complex divmod\(\), // and % are deprecated',
+                        DeprecationWarning,
+                        r'test.test_coercion$')
+do_infix_binops()
+do_prefix_binops()

Modified: pypy/dist/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/listobject.py	(original)
+++ pypy/dist/pypy/objspace/std/listobject.py	Fri Apr 29 16:42:47 2005
@@ -130,7 +130,13 @@
     list_extend__List_ANY(space, w_list1, w_iterable2)
     return w_list1
 
-def mul_list_times(space, w_list, times):
+def mul_list_times(space, w_list, w_times):
+    try:
+        times = space.int_w(w_times)
+    except OperationError, e:
+        if e.match(space, space.w_TypeError):
+            raise FailedToImplement
+        raise
     w_res = W_ListObject(space, [])
     size = w_list.ob_size
     newlen = size * times  # XXX check overflow
@@ -146,13 +152,18 @@
     return w_res
 
 def mul__List_ANY(space, w_list, w_times):
-    return mul_list_times(space, w_list, space.int_w(w_times))
+    return mul_list_times(space, w_list, w_times)
 
 def mul__ANY_List(space, w_times, w_list):
-    return mul_list_times(space, w_list, space.int_w(w_times))
+    return mul_list_times(space, w_list, w_times)
 
 def inplace_mul__List_ANY(space, w_list, w_times):
-    times = space.int_w(w_times)
+    try:
+        times = space.int_w(w_times)
+    except OperationError, e:
+        if e.match(space, space.w_TypeError):
+            raise FailedToImplement
+        raise
     if times <= 0:
         w_list.clear()
         return w_list

Modified: pypy/dist/pypy/objspace/std/stdtypedef.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stdtypedef.py	(original)
+++ pypy/dist/pypy/objspace/std/stdtypedef.py	Fri Apr 29 16:42:47 2005
@@ -211,7 +211,8 @@
             dest.append(expr_arg)
     renaming = ', '.join(dest) +" = "+', '.join(src)
 
-    if allow_NotImplemented_results and len(multimethod.specialnames) > 1:
+    if allow_NotImplemented_results and (len(multimethod.specialnames) > 1 or
+                                         multimethod.name.startswith('inplace_')):
         # turn FailedToImplement into NotImplemented
         code = """def %s_perform_call(space, %s):
                       %s

Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Fri Apr 29 16:42:47 2005
@@ -915,7 +915,13 @@
     w_empty = space.newstring([])
     return str_join__String_ANY(space, w_empty, w_r)
 
-def mul_string_times(space, w_str, mul):
+def mul_string_times(space, w_str, w_times):
+    try:
+        mul = space.int_w(w_times)
+    except OperationError, e:
+        if e.match(space, space.w_TypeError):
+            raise FailedToImplement
+        raise    
     input = w_str._value
     if mul < 0:
         return space.wrap("")
@@ -934,10 +940,10 @@
     return space.wrap("".join(buffer))
 
 def mul__String_ANY(space, w_str, w_times):
-    return mul_string_times(space, w_str, space.int_w(w_times))
+    return mul_string_times(space, w_str, w_times)
 
 def mul__ANY_String(space, w_times, w_str):
-    return mul_string_times(space, w_str, space.int_w(w_times))
+    return mul_string_times(space, w_str, w_times)
 
 def add__String_String(space, w_left, w_right):
     right = w_right._value

Modified: pypy/dist/pypy/objspace/std/tupleobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/tupleobject.py	(original)
+++ pypy/dist/pypy/objspace/std/tupleobject.py	Fri Apr 29 16:42:47 2005
@@ -65,15 +65,21 @@
     items2 = w_tuple2.wrappeditems
     return W_TupleObject(space, items1 + items2)
 
-def mul_tuple_times(space, w_tuple, times):
+def mul_tuple_times(space, w_tuple, w_times):
+    try:
+        times = space.int_w(w_times)
+    except OperationError, e:
+        if e.match(space, space.w_TypeError):
+            raise FailedToImplement
+        raise    
     items = w_tuple.wrappeditems
     return W_TupleObject(space, items * times)    
 
 def mul__Tuple_ANY(space, w_tuple, w_times):
-    return mul_tuple_times(space, w_tuple, space.int_w(w_times))
+    return mul_tuple_times(space, w_tuple, w_times)
 
 def mul__ANY_Tuple(space, w_times, w_tuple):
-    return mul_tuple_times(space, w_tuple, space.int_w(w_times))
+    return mul_tuple_times(space, w_tuple, w_times)
 
 def eq__Tuple_Tuple(space, w_tuple1, w_tuple2):
     items1 = w_tuple1.wrappeditems



More information about the Pypy-commit mailing list