[pypy-commit] pypy win64 test: Factored out code to check for int bounds, and changed some checks

berdario noreply at buildbot.pypy.org
Sat Jul 2 16:03:55 CEST 2011


Author: Dario Bertini <berdario at gmail.com>
Branch: win64 test
Changeset: r45268:b4558e71f05e
Date: 2011-07-02 16:10 +0200
http://bitbucket.org/pypy/pypy/changeset/b4558e71f05e/

Log:	Factored out code to check for int bounds, and changed some checks

diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -1,6 +1,5 @@
 import __builtin__
 import types
-import sys
 from pypy.interpreter import pyframe, function, special
 from pypy.interpreter.baseobjspace import ObjSpace, Wrappable
 from pypy.interpreter.error import OperationError, operationerrfmt
@@ -10,7 +9,7 @@
 from pypy.objspace.descroperation import DescrOperation, raiseattrerror
 from pypy.rlib.objectmodel import instantiate, r_dict, specialize
 from pypy.rlib.debug import make_sure_not_resized
-from pypy.rlib.rarithmetic import base_int, widen
+from pypy.rlib.rarithmetic import base_int, widen, is_valid_int
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.rlib.jit import hint
 from pypy.rlib.rbigint import rbigint
@@ -162,7 +161,7 @@
         if isinstance(x, OperationError):
             raise TypeError, ("attempt to wrap already wrapped exception: %s"%
                               (x,))
-        if isinstance(x, (int, long)) and -sys.maxint -1 <= x <= sys.maxint:
+        if isinstance(x, (int, long)) and is_valid_int(x):
             if isinstance(x, bool):
                 return self.newbool(x)
             else:
diff --git a/pypy/rlib/rarithmetic.py b/pypy/rlib/rarithmetic.py
--- a/pypy/rlib/rarithmetic.py
+++ b/pypy/rlib/rarithmetic.py
@@ -70,7 +70,7 @@
     if isinstance(n, objectmodel.Symbolic):
         return n        # assume Symbolics don't overflow
     assert not isinstance(n, float)
-    if -sys.maxint -1 < n < sys.maxint:
+    if is_valid_int(n):
         return int(n)
     n = long(n)
     n &= LONG_MASK
@@ -109,6 +109,9 @@
 
 del _bits, _test, _maxnumber
 
+def is_valid_int(r):
+    return -sys.maxint - 1 <= r <= sys.maxint
+
 def ovfcheck(r):
     "NOT_RPYTHON"
     # to be used as ovfcheck(x <op> y)
@@ -116,7 +119,7 @@
     assert not isinstance(r, r_uint), "unexpected ovf check on unsigned"
     assert not isinstance(r, r_longlong), "ovfcheck not supported on r_longlong"
     assert not isinstance(r, r_ulonglong), "ovfcheck not supported on r_ulonglong"
-    if r > sys.maxint or r < -sys.maxint - 1:
+    if not is_valid_int(r):
         raise OverflowError, "signed integer expression did overflow"
     return r
 
diff --git a/pypy/rlib/rbigint.py b/pypy/rlib/rbigint.py
--- a/pypy/rlib/rbigint.py
+++ b/pypy/rlib/rbigint.py
@@ -1,5 +1,5 @@
 from pypy.rlib.rarithmetic import LONG_BIT, intmask, r_uint, r_ulonglong
-from pypy.rlib.rarithmetic import ovfcheck, r_longlong, widen
+from pypy.rlib.rarithmetic import ovfcheck, r_longlong, widen, is_valid_int
 from pypy.rlib.rarithmetic import most_neg_value_of_same_type
 from pypy.rlib.rfloat import isfinite
 from pypy.rlib.debug import make_sure_not_resized, check_regular_int
@@ -51,14 +51,14 @@
 
 def _widen_digit(x):
     if not we_are_translated():
-        assert type(x) in (int, long), "widen_digit() takes an int, got a %r" % type(x)
+        assert type(x) in (int, long) and is_valid_int(x), "widen_digit() takes an int, got a %r" % type(x)
     if SHIFT <= 15:
         return int(x)
     return r_longlong(x)
 
 def _store_digit(x):
     if not we_are_translated():
-        assert type(x) in (int, long), "store_digit() takes an int, got a %r" % type(x)
+        assert type(x) in (int, long) and is_valid_int(x), "store_digit() takes an int, got a %r" % type(x)
     if SHIFT <= 15:
         return rffi.cast(rffi.SHORT, x)
     elif SHIFT <= 31:


More information about the pypy-commit mailing list