[pypy-commit] pypy py3k: completely remove support for coerce() and __coerce__. I hope I didn't break anything :-)

antocuni noreply at buildbot.pypy.org
Fri Feb 24 15:12:08 CET 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r52862:67d5616698f2
Date: 2012-02-24 15:11 +0100
http://bitbucket.org/pypy/pypy/changeset/67d5616698f2/

Log:	completely remove support for coerce() and __coerce__. I hope I
	didn't break anything :-)

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1569,7 +1569,6 @@
     ('gt',              '>',         2, ['__gt__', '__lt__']),
     ('ge',              '>=',        2, ['__ge__', '__le__']),
     ('cmp',             'cmp',       2, ['__cmp__']),   # rich cmps preferred
-    ('coerce',          'coerce',    2, ['__coerce__', '__coerce__']),
     ('contains',        'contains',  2, ['__contains__']),
     ('iter',            'iter',      1, ['__iter__']),
     ('next',            'next',      1, ['__next__']),
diff --git a/pypy/module/__builtin__/__init__.py b/pypy/module/__builtin__/__init__.py
--- a/pypy/module/__builtin__/__init__.py
+++ b/pypy/module/__builtin__/__init__.py
@@ -58,7 +58,6 @@
         'hex'           : 'operation.hex',
         'round'         : 'operation.round',
         'cmp'           : 'operation.cmp',
-        'coerce'        : 'operation.coerce',
         'divmod'        : 'operation.divmod',
         'format'        : 'operation.format',
         'issubclass'    : 'abstractinst.app_issubclass',
diff --git a/pypy/module/__builtin__/operation.py b/pypy/module/__builtin__/operation.py
--- a/pypy/module/__builtin__/operation.py
+++ b/pypy/module/__builtin__/operation.py
@@ -107,14 +107,6 @@
     """return 0 when x == y, -1 when x < y and 1 when x > y """
     return space.cmp(w_x, w_y)
 
-def coerce(space, w_x, w_y):
-    """coerce(x, y) -> (x1, y1)
-
-Return a tuple consisting of the two numeric arguments converted to
-a common type, using the same rules as used by arithmetic operations.
-If coercion is not possible, raise TypeError."""
-    return space.coerce(w_x, w_y)
-
 def divmod(space, w_x, w_y):
     """Return the tuple ((x-x%y)/y, x%y).  Invariant: div*y + mod == x."""
     return space.divmod(w_x, w_y)
diff --git a/pypy/module/__builtin__/test/test_builtin.py b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -440,17 +440,6 @@
         raises(RuntimeError, cmp, a, c)
         # okay, now break the cycles
         a.pop(); b.pop(); c.pop()
-        
-    def test_coerce(self):
-        assert coerce(1, 2)    == (1, 2)
-        assert coerce(1L, 2L)  == (1L, 2L)
-        assert coerce(1, 2L)   == (1L, 2L)
-        assert coerce(1L, 2)   == (1L, 2L)
-        assert coerce(1, 2.0)  == (1.0, 2.0)
-        assert coerce(1.0, 2L) == (1.0, 2.0)
-        assert coerce(1L, 2.0) == (1.0, 2.0)
-        raises(TypeError,coerce, 1    , 'a')
-        raises(TypeError,coerce, u'a' , 'a')
 
     def test_return_None(self):
         class X(object): pass
diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -513,8 +513,6 @@
         RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
         BINSLOT("__or__", nb_or, slot_nb_or, "|"),
         RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
-        NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
-               "x.__coerce__(y) <==> coerce(x, y)"),
         UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
                "int(x)"),
         UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -1571,24 +1571,6 @@
     """
     raise NotImplementedError
 
- at cpython_api([PyObjectP, PyObjectP], rffi.INT_real, error=-1)
-def PyNumber_Coerce(space, p1, p2):
-    """This function takes the addresses of two variables of type PyObject*.  If
-    the objects pointed to by *p1 and *p2 have the same type, increment their
-    reference count and return 0 (success). If the objects can be converted to a
-    common numeric type, replace *p1 and *p2 by their converted value (with
-    'new' reference counts), and return 0. If no conversion is possible, or if
-    some other error occurs, return -1 (failure) and don't increment the
-    reference counts.  The call PyNumber_Coerce(&o1, &o2) is equivalent to the
-    Python statement o1, o2 = coerce(o1, o2)."""
-    raise NotImplementedError
-
- at cpython_api([PyObjectP, PyObjectP], rffi.INT_real, error=-1)
-def PyNumber_CoerceEx(space, p1, p2):
-    """This function is similar to PyNumber_Coerce(), except that it returns
-    1 when the conversion is not possible and when no error is raised.
-    Reference counts are still not increased in this case."""
-    raise NotImplementedError
 
 @cpython_api([PyObject, rffi.INT_real], PyObject)
 def PyNumber_ToBase(space, n, base):
diff --git a/pypy/module/cpyext/typeobjectdefs.py b/pypy/module/cpyext/typeobjectdefs.py
--- a/pypy/module/cpyext/typeobjectdefs.py
+++ b/pypy/module/cpyext/typeobjectdefs.py
@@ -33,7 +33,6 @@
 ternaryfunc = P(FT([PyO, PyO, PyO], PyO))
 inquiry = P(FT([PyO], rffi.INT_real))
 lenfunc = P(FT([PyO], Py_ssize_t))
-coercion = P(FT([PyOPtr, PyOPtr], rffi.INT_real))
 intargfunc = P(FT([PyO, rffi.INT_real], PyO))
 intintargfunc = P(FT([PyO, rffi.INT_real, rffi.INT], PyO))
 ssizeargfunc = P(FT([PyO, Py_ssize_t], PyO))
@@ -89,7 +88,6 @@
     ("nb_and", binaryfunc),
     ("nb_xor", binaryfunc),
     ("nb_or", binaryfunc),
-    ("nb_coerce", coercion),
     ("nb_int", unaryfunc),
     ("nb_long", unaryfunc),
     ("nb_float", unaryfunc),
diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -459,36 +459,6 @@
             return space.wrap(-1)
         return space.wrap(1)
 
-    def coerce(space, w_obj1, w_obj2):
-        w_typ1 = space.type(w_obj1)
-        w_typ2 = space.type(w_obj2)
-        w_left_src, w_left_impl = space.lookup_in_type_where(w_typ1, '__coerce__')
-        if space.is_w(w_typ1, w_typ2):
-            w_right_impl = None
-        else:
-            w_right_src, w_right_impl = space.lookup_in_type_where(w_typ2, '__coerce__')
-            if (w_left_src is not w_right_src
-                and space.is_true(space.issubtype(w_typ2, w_typ1))):
-                w_obj1, w_obj2 = w_obj2, w_obj1
-                w_left_impl, w_right_impl = w_right_impl, w_left_impl
-
-        w_res = _invoke_binop(space, w_left_impl, w_obj1, w_obj2)
-        if w_res is None or space.is_w(w_res, space.w_None):
-            w_res = _invoke_binop(space, w_right_impl, w_obj2, w_obj1)
-            if w_res is None  or space.is_w(w_res, space.w_None):
-                raise OperationError(space.w_TypeError,
-                                     space.wrap("coercion failed"))
-            if (not space.is_true(space.isinstance(w_res, space.w_tuple)) or
-                space.len_w(w_res) != 2):
-                raise OperationError(space.w_TypeError,
-                                     space.wrap("coercion should return None or 2-tuple"))
-            w_res = space.newtuple([space.getitem(w_res, space.wrap(1)), space.getitem(w_res, space.wrap(0))])
-        elif (not space.is_true(space.isinstance(w_res, space.w_tuple)) or
-            space.len_w(w_res) != 2):
-            raise OperationError(space.w_TypeError,
-                                 space.wrap("coercion should return None or 2-tuple"))
-        return w_res
-
     def issubtype(space, w_sub, w_type):
         return space._type_issubtype(w_sub, w_type)
 
diff --git a/pypy/objspace/flow/operation.py b/pypy/objspace/flow/operation.py
--- a/pypy/objspace/flow/operation.py
+++ b/pypy/objspace/flow/operation.py
@@ -217,7 +217,6 @@
     ('inplace_or',      inplace_or),
     ('inplace_xor',     inplace_xor),
     ('cmp',             cmp),
-    ('coerce',          coerce),
     ('iter',            iter),
     ('next',            next),
     ('get',             get),
@@ -302,10 +301,9 @@
 
 for _name in 'getattr', 'delattr':
     _add_exceptions(_name, AttributeError)
-for _name in 'iter', 'coerce':
-    _add_exceptions(_name, TypeError)
 del _name
 
+_add_exceptions('iter', TypeError)
 _add_exceptions("""div mod divmod truediv floordiv pow
                    inplace_div inplace_mod inplace_divmod inplace_truediv
                    inplace_floordiv inplace_pow""", ZeroDivisionError)
diff --git a/pypy/objspace/std/builtinshortcut.py b/pypy/objspace/std/builtinshortcut.py
--- a/pypy/objspace/std/builtinshortcut.py
+++ b/pypy/objspace/std/builtinshortcut.py
@@ -37,7 +37,7 @@
                  'delitem', 'trunc',              # rare stuff?
                  'abs', 'hex', 'oct',             # rare stuff?
                  'pos', 'divmod', 'cmp',          # rare stuff?
-                 'float', 'long', 'coerce',       # rare stuff?
+                 'float', 'long',                 # rare stuff?
                  'isinstance', 'issubtype',
                  ]
 
diff --git a/pypy/objspace/std/complexobject.py b/pypy/objspace/std/complexobject.py
--- a/pypy/objspace/std/complexobject.py
+++ b/pypy/objspace/std/complexobject.py
@@ -247,9 +247,6 @@
     return space.newbool((w_complex.realval != 0.0) or
                          (w_complex.imagval != 0.0))
 
-def coerce__Complex_Complex(space, w_complex1, w_complex2):
-    return space.newtuple([w_complex1, w_complex2])
-
 def float__Complex(space, w_complex):
     raise OperationError(space.w_TypeError, space.wrap("can't convert complex to float; use abs(z)"))
 
diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py
--- a/pypy/objspace/std/floatobject.py
+++ b/pypy/objspace/std/floatobject.py
@@ -339,10 +339,6 @@
     return x
 
 
-# coerce
-def coerce__Float_Float(space, w_float1, w_float2):
-    return space.newtuple([w_float1, w_float2])
-
 
 def add__Float_Float(space, w_float1, w_float2):
     x = w_float1.floatval
diff --git a/pypy/objspace/std/intobject.py b/pypy/objspace/std/intobject.py
--- a/pypy/objspace/std/intobject.py
+++ b/pypy/objspace/std/intobject.py
@@ -102,10 +102,6 @@
     # Make sure this is consistent with the hash of floats and longs.
     return get_integer(space, w_int1)
 
-# coerce
-def coerce__Int_Int(space, w_int1, w_int2):
-    return space.newtuple([w_int1, w_int2])
-
 
 def add__Int_Int(space, w_int1, w_int2):
     x = w_int1.intval
diff --git a/pypy/objspace/std/longobject.py b/pypy/objspace/std/longobject.py
--- a/pypy/objspace/std/longobject.py
+++ b/pypy/objspace/std/longobject.py
@@ -170,11 +170,6 @@
 def hash__Long(space, w_value):
     return space.wrap(w_value.num.hash())
 
-# coerce
-def coerce__Long_Long(space, w_long1, w_long2):
-    return space.newtuple([w_long1, w_long2])
-
-
 def add__Long_Long(space, w_long1, w_long2):
     return W_LongObject(w_long1.num.add(w_long2.num))
 
diff --git a/pypy/objspace/std/test/test_complexobject.py b/pypy/objspace/std/test/test_complexobject.py
--- a/pypy/objspace/std/test/test_complexobject.py
+++ b/pypy/objspace/std/test/test_complexobject.py
@@ -165,9 +165,6 @@
     def test_floordiv(self):
         raises(TypeError, "3+0j // 0+0j")
 
-    def test_coerce(self):
-        raises(OverflowError, complex.__coerce__, 1+1j, 1L<<10000)
-
     def test_richcompare(self):
         assert complex.__lt__(1+1j, None) is NotImplemented
         assert complex.__eq__(1+1j, 2+2j) is False
diff --git a/pypy/objspace/test/test_descroperation.py b/pypy/objspace/test/test_descroperation.py
--- a/pypy/objspace/test/test_descroperation.py
+++ b/pypy/objspace/test/test_descroperation.py
@@ -249,11 +249,8 @@
                     ('__and__',      'x & y',                   'x &= y'),
                     ('__or__',       'x | y',                   'x |= y'),
                     ('__xor__',      'x ^ y',                   'x ^= y'),
-                    ('__coerce__',   'coerce(x, y)',            None)]:
-                if name == '__coerce__':
-                    rname = name
-                else:
-                    rname = '__r' + name[2:]
+                    ]:
+                rname = '__r' + name[2:]
                 A = metaclass('A', (), {name: specialmethod})
                 B = metaclass('B', (), {rname: specialmethod})
                 a = A()


More information about the pypy-commit mailing list