[pypy-svn] r10464 - in pypy/dist/pypy/tool: . test
pedronis at codespeak.net
pedronis at codespeak.net
Fri Apr 8 20:46:23 CEST 2005
Author: pedronis
Date: Fri Apr 8 20:46:22 2005
New Revision: 10464
Modified:
pypy/dist/pypy/tool/rarithmetic.py
pypy/dist/pypy/tool/test/test_rarithmetic.py
Log:
ovfcheck with tests.
Modified: pypy/dist/pypy/tool/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/tool/rarithmetic.py (original)
+++ pypy/dist/pypy/tool/rarithmetic.py Fri Apr 8 20:46:22 2005
@@ -1,18 +1,23 @@
"""
-This file defines restricted integers.
+This file defines restricted arithmetic:
+
+classes and operations to express integer arithmetic,
+such that before and after translation semantics are
+consistent
+
+r_uint an unsigned integer which has not overflow
+ checking. It is always positive and always
+ truncated to the internal machine word size.
+intmask mask a possibly long value when running on CPython
+ back to a signed int value
+ovfcheck check on CPython whether the result of a signed
+ integer operation did overflow
+
+These are meant to be erased by translation, r_uint
+in the process should mark unsigned values, ovfcheck should
+mark where overflow checking is required.
-Purpose:
-Have an integer implementation that emulates
-restricted Python for CPython.
-
-r_int an integer type which has overflow checking.
- It doesn not automatically extend to long
-r_uint an unsigned integer which has not overflow
- checking. It is always positive and always
- truncated to the internal machine word size.
-We try to keep the number of such internal types
-to a minimum.
"""
class r_int(int):
@@ -159,6 +164,15 @@
del _bits, _itest, _Ltest
+def ovfcheck(r):
+ # to be used as ovfcheck(x <op> y)
+ # raise OverflowError if the operation did overflow
+ assert not isinstance(r, r_uint), "unexpected ovf check on unsigned"
+ if isinstance(r, long):
+ raise OverflowError, "signed integer expression did overflow"
+ return r
+
+
class r_uint(long):
""" fake unsigned integer implementation """
Modified: pypy/dist/pypy/tool/test/test_rarithmetic.py
==============================================================================
--- pypy/dist/pypy/tool/test/test_rarithmetic.py (original)
+++ pypy/dist/pypy/tool/test/test_rarithmetic.py Fri Apr 8 20:46:22 2005
@@ -12,7 +12,7 @@
i *= 2
l *= 2
machbits += 1
-print machbits
+#print machbits
objspacename = 'std'
@@ -150,5 +150,65 @@
assert intmask(sys.maxint+1) == minint
assert intmask(minint-1) == sys.maxint
-
+def test_ovfcheck():
+ one = 1
+ x = sys.maxint
+ minusx = -sys.maxint
+ n = -sys.maxint-1
+ y = sys.maxint-1
+ # sanity
+ raises(AssertionError, ovfcheck, r_uint(0))
+
+
+ # not overflowing
+ try:
+ ovfcheck(y+one)
+ except OverflowError:
+ assert False
+ else:
+ pass
+ try:
+ ovfcheck(minusx-one)
+ except OverflowError:
+ assert False
+ else:
+ pass
+ try:
+ ovfcheck(x-x)
+ except OverflowError:
+ assert False
+ else:
+ pass
+ try:
+ ovfcheck(n-n)
+ except OverflowError:
+ assert False
+ else:
+ pass
+
+ # overflowing
+ try:
+ ovfcheck(x+one)
+ except OverflowError:
+ pass
+ else:
+ assert False
+ try:
+ ovfcheck(x+x)
+ except OverflowError:
+ pass
+ else:
+ assert False
+ try:
+ ovfcheck(n-one)
+ except OverflowError:
+ pass
+ else:
+ assert False
+ try:
+ ovfcheck(n-y)
+ except OverflowError:
+ pass
+ else:
+ assert False
More information about the Pypy-commit
mailing list