[pypy-svn] r10572 - in pypy/dist/pypy: interpreter objspace/std objspace/std/test

pedronis at codespeak.net pedronis at codespeak.net
Wed Apr 13 13:16:11 CEST 2005


Author: pedronis
Date: Wed Apr 13 13:16:10 2005
New Revision: 10572

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/objspace/std/default.py
   pypy/dist/pypy/objspace/std/intobject.py
   pypy/dist/pypy/objspace/std/longobject.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/objspace/std/test/test_intobject.py
   pypy/dist/pypy/objspace/std/test/test_longobject.py
   pypy/dist/pypy/objspace/std/test/test_stdobjspace.py
Log:
implementation of uint_w with some tests



Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Wed Apr 13 13:16:10 2005
@@ -458,6 +458,7 @@
 #                              str_w(w_str) -> str
 #              int_w(w_ival or w_long_ival) -> ival
 #                       float_w(w_floatval) -> floatval
+#             uint_w(w_ival or w_long_ival) -> r_uint_val (unsigned int value)
 #interpclass_w(w_interpclass_inst or w_obj) -> interpclass_inst|w_obj
 #                               unwrap(w_x) -> x
 #                              is_true(w_x) -> True or False
@@ -473,6 +474,7 @@
     'str_w',
     'int_w',
     'float_w',
+    'uint_w',
     'interpclass_w',
     'unwrap',
     'is_true',

Modified: pypy/dist/pypy/objspace/std/default.py
==============================================================================
--- pypy/dist/pypy/objspace/std/default.py	(original)
+++ pypy/dist/pypy/objspace/std/default.py	Wed Apr 13 13:16:10 2005
@@ -233,5 +233,8 @@
     raise OperationError(space.w_TypeError,
                          typed_unwrap_error_msg(space, "float", w_obj))
 
+def uint_w__ANY(space,w_obj):
+    raise OperationError(space.w_TypeError,
+                         typed_unwrap_error_msg(space, "integer", w_obj))
 
 register_all(vars())

Modified: pypy/dist/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/intobject.py	(original)
+++ pypy/dist/pypy/objspace/std/intobject.py	Wed Apr 13 13:16:10 2005
@@ -1,6 +1,6 @@
 from pypy.objspace.std.objspace import *
 from pypy.objspace.std.noneobject import W_NoneObject
-from pypy.tool.rarithmetic import ovfcheck, ovfcheck_lshift, LONG_BIT
+from pypy.tool.rarithmetic import ovfcheck, ovfcheck_lshift, LONG_BIT, r_uint
 
 """
 In order to have the same behavior running
@@ -38,6 +38,14 @@
 def int_w__Int(space, w_int1):
     return int(w_int1.intval)
 
+def uint_w__Int(space, w_int1):
+    intval = w_int1.intval
+    if intval < 0:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("cannot convert negative integer to unsigned"))
+    else:
+        return r_uint(intval)
+
 def repr__Int(space, w_int1):
     a = w_int1.intval
     res = str(a)

Modified: pypy/dist/pypy/objspace/std/longobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/longobject.py	(original)
+++ pypy/dist/pypy/objspace/std/longobject.py	Wed Apr 13 13:16:10 2005
@@ -137,7 +137,16 @@
         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"))           
+                         space.wrap("long int too large to convert to int"))
+
+def uint_w__Long(space, w_value):
+    if w_value.sign == -1:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("cannot convert negative integer to unsigned"))
+    if len(w_value.digits) == 1:
+        return w_value.digits[0]
+    raise OperationError(space.w_OverflowError,
+                         space.wrap("long int too large to convert to unsigned int"))    
 
 def repr__Long(space, w_long): #YYYYYY
     return space.wrap(repr(w_long.longval()))

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Wed Apr 13 13:16:10 2005
@@ -300,6 +300,7 @@
         int_w   = MultiMethod('int_w', 1, [])     # returns an unwrapped int
         str_w   = MultiMethod('str_w', 1, [])     # returns an unwrapped string
         float_w = MultiMethod('float_w', 1, [])   # returns an unwrapped float
+        uint_w  = MultiMethod('uint_w', 1, [])    # returns an unwrapped unsigned int (r_uint)
 
         # add all regular multimethods here
         for _name, _symbol, _arity, _specialnames in ObjSpace.MethodTable:

Modified: pypy/dist/pypy/objspace/std/test/test_intobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_intobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_intobject.py	Wed Apr 13 13:16:10 2005
@@ -2,6 +2,7 @@
 import autopath
 from pypy.objspace.std import intobject as iobj
 from pypy.objspace.std.objspace import FailedToImplement
+from pypy.tool.rarithmetic import r_uint
 
 objspacename = 'std'
 
@@ -29,6 +30,12 @@
     def test_int_w(self):
         assert self.space.int_w(self.space.wrap(42)) == 42
 
+    def test_uint_w(self):
+        space = self.space
+        assert space.uint_w(space.wrap(42)) == 42
+        assert isinstance(space.uint_w(space.wrap(42)), r_uint)
+        space.raises_w(space.w_ValueError, space.uint_w, space.wrap(-1))
+        
     def test_repr(self):
         x = 1
         f1 = iobj.W_IntObject(self.space, x)

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	Wed Apr 13 13:16:10 2005
@@ -3,6 +3,7 @@
 from random import random, randint
 from pypy.objspace.std import longobject as lobj
 from pypy.objspace.std.objspace import FailedToImplement
+from pypy.tool.rarithmetic import r_uint
 
 objspacename = 'std'
 
@@ -73,6 +74,10 @@
         assert lobj.long__Int(self.space, self.space.wrap(42)).longval() == 42
         assert lobj.long__Int(self.space, self.space.wrap(-42)).longval() == -42
 
+        u = lobj.uint_w__Long(self.space, f2)
+        assert u == 12332
+        assert isinstance(u, r_uint)
+
     def test_conversions(self):
         space = self.space
         for v in (0,1,-1,sys.maxint,-sys.maxint-1):
@@ -84,6 +89,13 @@
                 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)
 
+                if v>=0:
+                    u = lobj.uint_w__Long(space, w_lv)
+                    assert u == v
+                    assert isinstance(u, r_uint)
+                else:
+                    space.raises_w(space.w_ValueError, lobj.uint_w__Long, space, w_lv)
+
         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))
@@ -95,6 +107,17 @@
             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))
 
+        w_lmaxuint = lobj.W_LongObject(space, *lobj.args_from_long(2*sys.maxint+1))
+        w_toobig_lv4 = lobj.W_LongObject(space, *lobj.args_from_long(2*sys.maxint+2))        
+
+        u = lobj.uint_w__Long(space, w_lmaxuint)
+        assert u == 2*sys.maxint+1
+        assert isinstance(u, r_uint)
+        
+        space.raises_w(space.w_ValueError, lobj.uint_w__Long, space, w_toobig_lv3)       
+        space.raises_w(space.w_OverflowError, lobj.uint_w__Long, space, w_toobig_lv4)
+
+
 
     def test_pow_lll(self):
         x = 10L

Modified: pypy/dist/pypy/objspace/std/test/test_stdobjspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_stdobjspace.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_stdobjspace.py	Wed Apr 13 13:16:10 2005
@@ -19,6 +19,11 @@
         raises(OperationError,self.space.int_w,self.space.wrap(None))
         raises(OperationError,self.space.int_w,self.space.wrap(""))        
 
+    def test_uint_w_non_int(self):
+        raises(OperationError,self.space.uint_w,self.space.wrap(None))
+        raises(OperationError,self.space.uint_w,self.space.wrap(""))        
+
+
     def hopeful_test_exceptions(self):
         self.apptest("self.failUnless(issubclass(ArithmeticError, Exception))")
         self.apptest("self.failIf(issubclass(ArithmeticError, KeyError))")



More information about the Pypy-commit mailing list