Author: Alexander Schremmer <alex AT alexanderweb DOT de>
Branch: math-improvements
Changeset: r95792:116eb65b5ec6
Date: 2019-02-04 15:38 +0100
http://bitbucket.org/pypy/pypy/changeset/116eb65b5ec6/

Log:    Change interface to ovf2long, found one buggy invocation, wrote test
        for it.

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
@@ -299,7 +299,7 @@
     return ix
 
 
-def _pow_ovf2long(space, iv, iw, iw_obj, w_modulus):
+def _pow_ovf2long(space, iv, w_iv, iw, w_iw, w_modulus):
     if space.is_none(w_modulus) and _recover_with_smalllong(space):
         from pypy.objspace.std.smalllongobject import _pow as _pow_small
         try:
@@ -308,32 +308,38 @@
             return _pow_small(space, r_longlong(iv), iw, r_longlong(0))
         except (OverflowError, ValueError):
             pass
-    from pypy.objspace.std.longobject import W_LongObject
-    w_iv = W_LongObject.fromint(space, iv)
+    from pypy.objspace.std.longobject import W_LongObject, W_AbstractLongObject
+    if w_iv is None or not isinstance(w_iv, W_AbstractLongObject):
+        w_iv = W_LongObject.fromint(space, iv)
+    if w_iw is None or not isinstance(w_iw, W_AbstractLongObject):
+        w_iw = W_LongObject.fromint(space, iw)
 
-    return w_iv.descr_pow(space, iw_obj, w_modulus)
+    return w_iv.descr_pow(space, w_iw, w_modulus)
 
 
 def _make_ovf2long(opname, ovf2small=None):
     op = getattr(operator, opname, None)
     assert op or ovf2small
 
-    def ovf2long(space, x, y, y_obj):
+    def ovf2long(space, x, w_x, y, w_y):
         """Handle overflowing to smalllong or long"""
         if _recover_with_smalllong(space):
             if ovf2small:
                 return ovf2small(space, x, y)
-            # Assume a generic operation without an explicit #iovf2small
+            # Assume a generic operation without an explicit ovf2small
             # handler
             from pypy.objspace.std.smalllongobject import W_SmallLongObject
             a = r_longlong(x)
             b = r_longlong(y)
             return W_SmallLongObject(op(a, b))
 
-        from pypy.objspace.std.longobject import W_LongObject
-        w_x = W_LongObject.fromint(space, x)
+        from pypy.objspace.std.longobject import W_LongObject, 
W_AbstractLongObject
+        if w_x is None or not isinstance(w_x, W_AbstractLongObject):
+            w_x = W_LongObject.fromint(space, x)
+        if w_y is None or not isinstance(w_y, W_AbstractLongObject):
+            w_y = W_LongObject.fromint(space, y)
 
-        return getattr(w_x, 'descr_' + opname)(space, y_obj)
+        return getattr(w_x, 'descr_' + opname)(space, w_y)
 
     return ovf2long
 
@@ -496,12 +502,12 @@
             # can't return NotImplemented (space.pow doesn't do full
             # ternary, i.e. w_modulus.__zpow__(self, w_exponent)), so
             # handle it ourselves
-            return _pow_ovf2long(space, x, y, w_exponent, w_modulus)
+            return _pow_ovf2long(space, x, self, y, w_exponent, w_modulus)
 
         try:
             result = _pow(space, x, y, z)
         except (OverflowError, ValueError):
-            return _pow_ovf2long(space, x, y, w_exponent, w_modulus)
+            return _pow_ovf2long(space, x, self, y, w_exponent, w_modulus)
         return space.newint(result)
 
     @unwrap_spec(w_modulus=WrappedDefault(None))
@@ -546,7 +552,7 @@
                 try:
                     z = ovfcheck(op(x, y))
                 except OverflowError:
-                    return ovf2long(space, x, y, w_other)
+                    return ovf2long(space, x, self, y, w_other)
             else:
                 z = op(x, y)
             return wrapint(space, z)
@@ -568,7 +574,7 @@
                 try:
                     z = ovfcheck(op(y, x))
                 except OverflowError:
-                    return ovf2long(space, y, x, w_other)
+                    return ovf2long(space, y, w_other, x, self)  # XXX write a 
test
             else:
                 z = op(y, x)
             return wrapint(space, z)
@@ -599,7 +605,7 @@
                 try:
                     return func(space, x, y)
                 except OverflowError:
-                    return ovf2long(space, x, y, w_other)
+                    return ovf2long(space, x, self, y, w_other)
             else:
                 return func(space, x, y)
 
@@ -614,7 +620,7 @@
                 try:
                     return func(space, y, x)
                 except OverflowError:
-                    return ovf2long(space, y, x, self)
+                    return ovf2long(space, y, w_other, x, self)
             else:
                 return func(space, y, x)
 
diff --git a/pypy/objspace/std/test/test_intobject.py 
b/pypy/objspace/std/test/test_intobject.py
--- a/pypy/objspace/std/test/test_intobject.py
+++ b/pypy/objspace/std/test/test_intobject.py
@@ -679,6 +679,11 @@
         x = int(321)
         assert x.__rlshift__(333) == 
1422567365923326114875084456308921708325401211889530744784729710809598337369906606315292749899759616L
 
+    def test_some_rops(self):
+        import sys
+        x = int(-sys.maxint)
+        assert x.__rsub__(2) == (2 + sys.maxint)
+
 class AppTestIntShortcut(AppTestInt):
     spaceconfig = {"objspace.std.intshortcut": True}
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to