[pypy-svn] r10564 - in pypy/dist/pypy/objspace/std: . test
pedronis at codespeak.net
pedronis at codespeak.net
Tue Apr 12 22:13:06 CEST 2005
Author: pedronis
Date: Tue Apr 12 22:13:05 2005
New Revision: 10564
Modified:
pypy/dist/pypy/objspace/std/longobject.py
pypy/dist/pypy/objspace/std/test/test_longobject.py
Log:
some more tests and some fixes for longobject conversions
Modified: pypy/dist/pypy/objspace/std/longobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/longobject.py (original)
+++ pypy/dist/pypy/objspace/std/longobject.py Tue Apr 12 22:13:05 2005
@@ -11,6 +11,10 @@
SHORT_BIT = int(LONG_BIT // 2)
SHORT_MASK = int(LONG_MASK >> SHORT_BIT)
+SIGN_BIT = LONG_BIT-1
+SIGN_MASK = r_uint(1) << SIGN_BIT
+NONSIGN_MASK = ~SIGN_MASK
+
class W_LongObject(W_Object):
"""This is a reimplementation of longs using a list of r_uints."""
#All functions that still rely on the underlying Python's longs are marked
@@ -106,11 +110,12 @@
return W_LongObject(space, [r_uint(abs(w_intobj.intval))], sign)
def int__Long(space, w_value):
- if (len(w_value.digits) == 1 and
- w_value.digits[0] & (r_uint(1) << LONG_BIT) == 0):
- return space.newint(int(w_value.digits[0]) * w_value.sign)
- else:
- return w_value # 9999999999999L.__int__() == 9999999999999L
+ if len(w_value.digits) == 1:
+ if w_value.digits[0] & SIGN_MASK == 0:
+ return space.newint(int(w_value.digits[0]) * w_value.sign)
+ elif w_value.sign == -1 and w_value.digits[0] & NONSIGN_MASK == 0:
+ return space.newint(intmask(w_value.digits[0]))
+ return w_value # 9999999999999L.__int__() == 9999999999999L
def float__Long(space, w_longobj): #YYYYYY
try:
@@ -123,12 +128,13 @@
return W_LongObject(space, *args_from_long(long(w_floatobj.floatval)))
def int_w__Long(space, w_value):
- if (len(w_value.digits) == 1 and
- w_value.digits[0] & (r_uint(1) << LONG_BIT) == 0):
- return int(w_value.digits[0]) * w_value.sign
- else:
- raise OperationError(space.w_OverflowError,
- space.wrap("long int too large to convert to int"))
+ if len(w_value.digits) == 1:
+ if w_value.digits[0] & SIGN_MASK == 0:
+ return int(w_value.digits[0]) * w_value.sign
+ elif w_value.sign == -1 and w_value.digits[0] & NONSIGN_MASK == 0:
+ return intmask(w_value.digits[0])
+ raise OperationError(space.w_OverflowError,
+ space.wrap("long int too large to convert to int"))
def repr__Long(space, w_long): #YYYYYY
return space.wrap(repr(w_long.longval()))
Modified: pypy/dist/pypy/objspace/std/test/test_longobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_longobject.py (original)
+++ pypy/dist/pypy/objspace/std/test/test_longobject.py Tue Apr 12 22:13:05 2005
@@ -73,6 +73,28 @@
assert lobj.long__Int(self.space, self.space.wrap(42)).longval() == 42
assert lobj.long__Int(self.space, self.space.wrap(-42)).longval() == -42
+ def test_conversions(self):
+ space = self.space
+ for v in (0,1,-1,sys.maxint,-sys.maxint-1):
+ assert lobj.W_LongObject(self.space, *lobj.args_from_long(v)).longval() == v
+ w_v = space.newint(v)
+ for w_lv in (lobj.long__Int(space, w_v), lobj.delegate_Int2Long(w_v)):
+ assert w_lv.longval() == v
+ assert lobj.int_w__Long(space, w_lv) == v
+ assert space.is_true(space.isinstance(lobj.int__Long(space, w_lv), space.w_int))
+ assert space.eq_w(lobj.int__Long(space, w_lv), w_v)
+
+ w_toobig_lv1 = lobj.W_LongObject(space, *lobj.args_from_long(sys.maxint+1))
+ assert w_toobig_lv1.longval() == sys.maxint+1
+ w_toobig_lv2 = lobj.W_LongObject(space, *lobj.args_from_long(sys.maxint+2))
+ assert w_toobig_lv2.longval() == sys.maxint+2
+ w_toobig_lv3 = lobj.W_LongObject(space, *lobj.args_from_long(-sys.maxint-2))
+ assert w_toobig_lv3.longval() == -sys.maxint-2
+
+ for w_lv in (w_toobig_lv1, w_toobig_lv2, w_toobig_lv3):
+ space.raises_w(space.w_OverflowError, lobj.int_w__Long, space, w_lv)
+ assert space.is_true(space.isinstance(lobj.int__Long(space, w_lv), space.w_long))
+
def test_pow_lll(self):
x = 10L
y = 2L
More information about the Pypy-commit
mailing list