Author: Philip Jenvey <[email protected]>
Branch: remove-intlong-smm
Changeset: r68586:88b052759cdc
Date: 2014-01-06 14:51 -0800
http://bitbucket.org/pypy/pypy/changeset/88b052759cdc/

Log:    more don't bother w/ OverflowErrors unless it's necessary

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
@@ -262,7 +262,7 @@
     descr_or, descr_ror = _make_generic_descr_binop('or', ovf=False)
     descr_xor, descr_rxor = _make_generic_descr_binop('xor', ovf=False)
 
-    def _make_descr_binop(func):
+    def _make_descr_binop(func, ovf=True):
         opname = func.__name__[1:]
         oper = BINARY_OPS.get(opname)
         if oper == '%':
@@ -273,10 +273,13 @@
         def descr_binop(self, space, w_other):
             if not isinstance(w_other, W_AbstractIntObject):
                 return space.w_NotImplemented
-            try:
+            if ovf:
+                try:
+                    return func(self, space, w_other)
+                except OverflowError:
+                    return _ovf2long(space, opname, self, w_other)
+            else:
                 return func(self, space, w_other)
-            except OverflowError:
-                return _ovf2long(space, opname, self, w_other)
         descr_binop.__doc__ = "x.__%s__(y) <==> %s" % (opname,
                                                        oper % ('x', 'y'))
 
@@ -284,10 +287,13 @@
         def descr_rbinop(self, space, w_other):
             if not isinstance(w_other, W_AbstractIntObject):
                 return space.w_NotImplemented
-            try:
+            if ovf:
+                try:
+                    return func(w_other, space, self)
+                except OverflowError:
+                    return _ovf2long(space, opname, w_other, self)
+            else:
                 return func(w_other, space, self)
-            except OverflowError:
-                return _ovf2long(space, opname, w_other, self)
         descr_rbinop.__doc__ = "x.__r%s__(y) <==> %s" % (opname,
                                                          oper % ('y', 'x'))
 
@@ -314,7 +320,7 @@
             raise operationerrfmt(space.w_ZeroDivisionError,
                                   "division by zero")
         return space.wrap(x / y)
-    descr_truediv, descr_rtruediv = _make_descr_binop(_truediv)
+    descr_truediv, descr_rtruediv = _make_descr_binop(_truediv, ovf=False)
 
     def _mod(self, space, w_other):
         x = space.int_w(self)
@@ -369,7 +375,7 @@
         else:
             a = a >> b
         return wrapint(space, a)
-    descr_rshift, descr_rrshift = _make_descr_binop(_rshift)
+    descr_rshift, descr_rrshift = _make_descr_binop(_rshift, ovf=False)
 
 
 class W_IntObject(W_AbstractIntObject):
diff --git a/pypy/objspace/std/smalllongobject.py 
b/pypy/objspace/std/smalllongobject.py
--- a/pypy/objspace/std/smalllongobject.py
+++ b/pypy/objspace/std/smalllongobject.py
@@ -176,7 +176,7 @@
     descr_gt = _make_descr_cmp('gt')
     descr_ge = _make_descr_cmp('ge')
 
-    def _make_descr_binop(func):
+    def _make_descr_binop(func, ovf=True):
         opname = func.__name__[1:]
         descr_name, descr_rname = 'descr_' + opname, 'descr_r' + opname
         long_op = getattr(W_LongObject, descr_name)
@@ -191,12 +191,15 @@
                 self = _small2long(space, self)
                 return long_op(self, space, w_other)
 
-            try:
+            if ovf:
+                try:
+                    return func(self, space, w_other)
+                except OverflowError:
+                    self = _small2long(space, self)
+                    w_other = _small2long(space, w_other)
+                    return long_op(self, space, w_other)
+            else:
                 return func(self, space, w_other)
-            except OverflowError:
-                self = _small2long(space, self)
-                w_other = _small2long(space, w_other)
-                return long_op(self, space, w_other)
 
         if opname in COMMUTATIVE_OPS:
             descr_rbinop = func_with_new_name(descr_binop, descr_rname)
@@ -212,12 +215,15 @@
                     self = _small2long(space, self)
                     return long_rop(self, space, w_other)
 
-                try:
+                if ovf:
+                    try:
+                        return func(w_other, space, self)
+                    except OverflowError:
+                        self = _small2long(space, self)
+                        w_other = _small2long(space, w_other)
+                        return long_rop(self, space, w_other)
+                else:
                     return func(w_other, space, self)
-                except OverflowError:
-                    self = _small2long(space, self)
-                    w_other = _small2long(space, w_other)
-                    return long_rop(self, space, w_other)
 
         return descr_binop, descr_rbinop
 
@@ -322,28 +328,28 @@
         else:
             a = a >> b
         return W_SmallLongObject(a)
-    descr_rshift, descr_rrshift = _make_descr_binop(_rshift)
+    descr_rshift, descr_rrshift = _make_descr_binop(_rshift, ovf=False)
 
     def _and(self, space, w_other):
         a = self.longlong
         b = w_other.longlong
         res = a & b
         return W_SmallLongObject(res)
-    descr_and, descr_rand = _make_descr_binop(_and)
+    descr_and, descr_rand = _make_descr_binop(_and, ovf=False)
 
     def _xor(self, space, w_other):
         a = self.longlong
         b = w_other.longlong
         res = a ^ b
         return W_SmallLongObject(res)
-    descr_xor, descr_rxor = _make_descr_binop(_xor)
+    descr_xor, descr_rxor = _make_descr_binop(_xor, ovf=False)
 
     def _or(self, space, w_other):
         a = self.longlong
         b = w_other.longlong
         res = a | b
         return W_SmallLongObject(res)
-    descr_or, descr_ror = _make_descr_binop(_or)
+    descr_or, descr_ror = _make_descr_binop(_or, ovf=False)
 
 
 # ____________________________________________________________
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to