[pypy-svn] pypy default: Add rlib.debug.check_regular_int(), used in rbigint.fromint()

arigo commits-noreply at bitbucket.org
Wed Feb 2 12:04:20 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r41538:3db61138bdbd
Date: 2011-02-02 12:03 +0100
http://bitbucket.org/pypy/pypy/changeset/3db61138bdbd/

Log:	Add rlib.debug.check_regular_int(), used in rbigint.fromint() to
	check that we are really getting a regular integer.

diff --git a/pypy/rlib/debug.py b/pypy/rlib/debug.py
--- a/pypy/rlib/debug.py
+++ b/pypy/rlib/debug.py
@@ -233,6 +233,9 @@
 class UnexpectedRUInt(Exception):
     pass
 
+class ExpectedRegularInt(Exception):
+    pass
+
 def check_nonneg(x):
     """Give a translation-time error if 'x' is not known to be non-negative.
     To help debugging, this also gives a translation-time error if 'x' is
@@ -259,3 +262,23 @@
     def specialize_call(self, hop):
         hop.exception_cannot_occur()
         return hop.inputarg(hop.args_r[0], arg=0)
+
+def check_regular_int(x):
+    """Give a translation-time error if 'x' is not a plain int
+    (e.g. if it's a r_longlong or an r_uint).
+    """
+    assert type(x) is int
+    return x
+
+class Entry(ExtRegistryEntry):
+    _about_ = check_regular_int
+
+    def compute_result_annotation(self, s_arg):
+        from pypy.annotation.model import SomeInteger
+        if not SomeInteger().contains(s_arg):
+            raise ExpectedRegularInt(s_arg)
+        return s_arg
+
+    def specialize_call(self, hop):
+        hop.exception_cannot_occur()
+        return hop.inputarg(hop.args_r[0], arg=0)

diff --git a/pypy/rlib/rbigint.py b/pypy/rlib/rbigint.py
--- a/pypy/rlib/rbigint.py
+++ b/pypy/rlib/rbigint.py
@@ -1,7 +1,7 @@
 from pypy.rlib.rarithmetic import LONG_BIT, intmask, r_uint, r_ulonglong
 from pypy.rlib.rarithmetic import ovfcheck, r_longlong, widen, isinf, isnan
 from pypy.rlib.rarithmetic import most_neg_value_of_same_type
-from pypy.rlib.debug import make_sure_not_resized
+from pypy.rlib.debug import make_sure_not_resized, check_regular_int
 from pypy.rlib.objectmodel import we_are_translated
 
 import math, sys
@@ -71,6 +71,7 @@
         return len(self.digits)
 
     def fromint(intval):
+        check_regular_int(intval)
         if intval < 0:
             sign = -1
             ival = r_uint(-intval)


More information about the Pypy-commit mailing list