[pypy-svn] r27378 - in pypy/dist/pypy/rpython: . lltypesystem ootypesystem test

antocuni at codespeak.net antocuni at codespeak.net
Wed May 17 23:18:00 CEST 2006


Author: antocuni
Date: Wed May 17 23:17:44 2006
New Revision: 27378

Added:
   pypy/dist/pypy/rpython/lltypesystem/ll_str.py   (contents, props changed)
   pypy/dist/pypy/rpython/ootypesystem/ll_str.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/lltypesystem/rstr.py
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/rint.py
   pypy/dist/pypy/rpython/test/test_rint.py
   pypy/dist/pypy/rpython/typesystem.py
Log:
Added support for int to string conversion (including hex and oct) to ootypesystem.



Added: pypy/dist/pypy/rpython/lltypesystem/ll_str.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/lltypesystem/ll_str.py	Wed May 17 23:17:44 2006
@@ -0,0 +1,104 @@
+from pypy.rpython.lltypesystem.lltype import GcArray, Array, Char, malloc
+from pypy.rpython.rarithmetic import r_uint
+
+CHAR_ARRAY = GcArray(Char)
+
+def ll_int2dec(i):
+    from pypy.rpython.lltypesystem.rstr import STR
+    temp = malloc(CHAR_ARRAY, 20)
+    len = 0
+    sign = 0
+    if i < 0:
+        sign = 1
+        i = r_uint(-i)
+    else:
+        i = r_uint(i)
+    if i == 0:
+        len = 1
+        temp[0] = '0'
+    else:
+        while i:
+            temp[len] = chr(i%10+ord('0'))
+            i //= 10
+            len += 1
+    len += sign
+    result = malloc(STR, len)
+    if sign:
+        result.chars[0] = '-'
+        j = 1
+    else:
+        j = 0
+    while j < len:
+        result.chars[j] = temp[len-j-1]
+        j += 1
+    return result
+
+hex_chars = malloc(Array(Char), 16, immortal=True)
+
+for i in range(16):
+    hex_chars[i] = "%x"%i
+
+def ll_int2hex(i, addPrefix):
+    from pypy.rpython.lltypesystem.rstr import STR
+    temp = malloc(CHAR_ARRAY, 20)
+    len = 0
+    sign = 0
+    if i < 0:
+        sign = 1
+        i = -i
+    if i == 0:
+        len = 1
+        temp[0] = '0'
+    else:
+        while i:
+            temp[len] = hex_chars[i%16]
+            i //= 16
+            len += 1
+    len += sign
+    if addPrefix:
+        len += 2
+    result = malloc(STR, len)
+    j = 0
+    if sign:
+        result.chars[0] = '-'
+        j = 1
+    if addPrefix:
+        result.chars[j] = '0'
+        result.chars[j+1] = 'x'
+        j += 2
+    while j < len:
+        result.chars[j] = temp[len-j-1]
+        j += 1
+    return result
+
+def ll_int2oct(i, addPrefix):
+    from pypy.rpython.lltypesystem.rstr import STR
+    if i == 0:
+        result = malloc(STR, 1)
+        result.chars[0] = '0'
+        return result
+    temp = malloc(CHAR_ARRAY, 25)
+    len = 0
+    sign = 0
+    if i < 0:
+        sign = 1
+        i = -i
+    while i:
+        temp[len] = hex_chars[i%8]
+        i //= 8
+        len += 1
+    len += sign
+    if addPrefix:
+        len += 1
+    result = malloc(STR, len)
+    j = 0
+    if sign:
+        result.chars[0] = '-'
+        j = 1
+    if addPrefix:
+        result.chars[j] = '0'
+        j += 1
+    while j < len:
+        result.chars[j] = temp[len-j-1]
+        j += 1
+    return result

Modified: pypy/dist/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rstr.py	Wed May 17 23:17:44 2006
@@ -649,9 +649,6 @@
     do_stringformat = classmethod(do_stringformat)
 
 
-
-
-
 # TODO: make the public interface of the rstr module cleaner
 ll_strconcat = LLHelpers.ll_strconcat
 ll_join = LLHelpers.ll_join
@@ -704,3 +701,5 @@
 list_str_open_bracket = string_repr.convert_const("[")
 list_str_close_bracket = string_repr.convert_const("]")
 list_str_sep = string_repr.convert_const(", ")
+
+import ll_str # side-effects due to __extend__

Added: pypy/dist/pypy/rpython/ootypesystem/ll_str.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/ootypesystem/ll_str.py	Wed May 17 23:17:44 2006
@@ -0,0 +1,17 @@
+from pypy.rpython.ootypesystem import ootype
+
+def const(c):
+    return c
+
+def ll_int2dec(i):
+    return ootype.oostring(i, const(10))
+
+# TODO: add support for addPrefix == False
+def ll_int2hex(i, addPrefix):
+    #assert addPrefix
+    return ootype.oostring(i, const(16))
+
+def ll_int2oct(i, addPrefix):
+    #assert addPrefix
+    return ootype.oostring(i, const(8))
+

Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Wed May 17 23:17:44 2006
@@ -1169,8 +1169,8 @@
     """
     if isinstance(obj, int):
         assert base in (-1, 8, 10, 16)
-        fmt = {-1:'%d', 8:'%o', 10:'%d', 16:'%x'}[base]
-        obj = fmt % obj
+        fn = {-1: str, 8: oct, 10: str, 16: hex}[base]
+        obj = fn(obj)
     elif isinstance(obj, _view):
         obj = '<%s object>' % obj._inst._TYPE._name
     return make_string(str(obj))

Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py	(original)
+++ pypy/dist/pypy/rpython/rint.py	Wed May 17 23:17:44 2006
@@ -3,9 +3,8 @@
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow.objspace import op_appendices
 from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Bool, Float, \
-     Void, Char, UniChar, GcArray, malloc, Array, pyobjectptr, \
-     UnsignedLongLong, SignedLongLong, build_number, Number, cast_primitive, \
-     typeOf
+     Void, Char, UniChar, malloc, pyobjectptr, UnsignedLongLong, \
+     SignedLongLong, build_number, Number, cast_primitive, typeOf
 from pypy.rpython.rmodel import IntegerRepr, inputconst
 from pypy.rpython.robject import PyObjRepr, pyobj_repr
 from pypy.rpython.rarithmetic import intmask, r_int, r_uint, r_ulonglong, r_longlong
@@ -299,121 +298,23 @@
         vlist = hop.inputargs(Float)
         return vlist[0]
 
-    def ll_str(self, i):
-        from pypy.rpython.lltypesystem.rstr import STR
-        temp = malloc(CHAR_ARRAY, 20)
-        len = 0
-        sign = 0
-        if i < 0:
-            sign = 1
-            i = r_uint(-i)
-        else:
-            i = r_uint(i)
-        if i == 0:
-            len = 1
-            temp[0] = '0'
-        else:
-            while i:
-                temp[len] = chr(i%10+ord('0'))
-                i //= 10
-                len += 1
-        len += sign
-        result = malloc(STR, len)
-        if sign:
-            result.chars[0] = '-'
-            j = 1
-        else:
-            j = 0
-        while j < len:
-            result.chars[j] = temp[len-j-1]
-            j += 1
-        return result
+    def rtype_str(self, hop):
+        fn = hop.rtyper.type_system.ll_str.ll_int2dec
+        return hop.gendirectcall(fn, hop.args_v[0])
 
     def rtype_hex(self, hop):
         self = self.as_int
         varg = hop.inputarg(self, 0)
         true = inputconst(Bool, True)
-        return hop.gendirectcall(ll_int2hex, varg, true)
+        fn = hop.rtyper.type_system.ll_str.ll_int2hex
+        return hop.gendirectcall(fn, varg, true)
 
     def rtype_oct(self, hop):
         self = self.as_int
         varg = hop.inputarg(self, 0)
         true = inputconst(Bool, True)
-        return hop.gendirectcall(ll_int2oct, varg, true)
-
-
-
-CHAR_ARRAY = GcArray(Char)
-
-hex_chars = malloc(Array(Char), 16, immortal=True)
-
-for i in range(16):
-    hex_chars[i] = "%x"%i
-
-def ll_int2hex(i, addPrefix):
-    from pypy.rpython.lltypesystem.rstr import STR
-    temp = malloc(CHAR_ARRAY, 20)
-    len = 0
-    sign = 0
-    if i < 0:
-        sign = 1
-        i = -i
-    if i == 0:
-        len = 1
-        temp[0] = '0'
-    else:
-        while i:
-            temp[len] = hex_chars[i%16]
-            i //= 16
-            len += 1
-    len += sign
-    if addPrefix:
-        len += 2
-    result = malloc(STR, len)
-    j = 0
-    if sign:
-        result.chars[0] = '-'
-        j = 1
-    if addPrefix:
-        result.chars[j] = '0'
-        result.chars[j+1] = 'x'
-        j += 2
-    while j < len:
-        result.chars[j] = temp[len-j-1]
-        j += 1
-    return result
-
-def ll_int2oct(i, addPrefix):
-    from pypy.rpython.lltypesystem.rstr import STR
-    if i == 0:
-        result = malloc(STR, 1)
-        result.chars[0] = '0'
-        return result
-    temp = malloc(CHAR_ARRAY, 25)
-    len = 0
-    sign = 0
-    if i < 0:
-        sign = 1
-        i = -i
-    while i:
-        temp[len] = hex_chars[i%8]
-        i //= 8
-        len += 1
-    len += sign
-    if addPrefix:
-        len += 1
-    result = malloc(STR, len)
-    j = 0
-    if sign:
-        result.chars[0] = '-'
-        j = 1
-    if addPrefix:
-        result.chars[j] = '0'
-        j += 1
-    while j < len:
-        result.chars[j] = temp[len-j-1]
-        j += 1
-    return result
+        fn = hop.rtyper.type_system.ll_str.ll_int2oct        
+        return hop.gendirectcall(fn, varg, true)
 
 def ll_identity(n):
     return n

Modified: pypy/dist/pypy/rpython/test/test_rint.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rint.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rint.py	Wed May 17 23:17:44 2006
@@ -4,7 +4,7 @@
 from pypy.rpython.test import snippet
 from pypy.rpython.test.test_llinterp import interpret
 from pypy.rpython.rarithmetic import r_uint, r_longlong, r_ulonglong
-
+from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
 
 class TestSnippet(object):
 
@@ -37,58 +37,6 @@
             print 'BINARY_OPERATIONS:', opname
 
 
-def test_char_constant():
-    def dummyfn(i):
-        return chr(i)
-    res = interpret(dummyfn, [ord(' ')])
-    assert res == ' '
-    res = interpret(dummyfn, [0])
-    assert res == '\0'
-    res = interpret(dummyfn, [ord('a')])
-    assert res == 'a'
-    
-def test_str_of_int():
-    def dummy(i):
-        return str(i)
-    
-    res = interpret(dummy, [0])
-    assert ''.join(res.chars) == '0'
-
-    res = interpret(dummy, [1034])
-    assert ''.join(res.chars) == '1034'
-
-    res = interpret(dummy, [-123])
-    assert ''.join(res.chars) == '-123'
-
-    res = interpret(dummy, [-sys.maxint-1])
-    assert ''.join(res.chars) == str(-sys.maxint-1)
-
-def test_hex_of_int():
-    def dummy(i):
-        return hex(i)
-    
-    res = interpret(dummy, [0])
-    assert ''.join(res.chars) == '0x0'
-
-    res = interpret(dummy, [1034])
-    assert ''.join(res.chars) == '0x40a'
-
-    res = interpret(dummy, [-123])
-    assert ''.join(res.chars) == '-0x7b'
-
-def test_oct_of_int():
-    def dummy(i):
-        return oct(i)
-    
-    res = interpret(dummy, [0])
-    assert ''.join(res.chars) == '0'
-
-    res = interpret(dummy, [1034])
-    assert ''.join(res.chars) == '02012'
-
-    res = interpret(dummy, [-123])
-    assert ''.join(res.chars) == '-0173'
-
 def test_unsigned():
     def dummy(i):
         i = r_uint(i)
@@ -172,3 +120,65 @@
         res = interpret(f, [inttype(0)])
         assert res == f(inttype(0))
         assert type(res) == inttype
+
+
+class BaseTestRint(BaseRtypingTest):
+    
+    def test_char_constant(self):
+        def dummyfn(i):
+            return chr(i)
+        res = self.interpret(dummyfn, [ord(' ')])
+        assert res == ' '
+        res = self.interpret(dummyfn, [0])
+        assert res == '\0'
+        res = self.interpret(dummyfn, [ord('a')])
+        assert res == 'a'
+
+    def test_str_of_int(self):
+        def dummy(i):
+            return str(i)
+
+        res = self.interpret(dummy, [0])
+        assert self.ll_to_string(res) == '0'
+
+        res = self.interpret(dummy, [1034])
+        assert self.ll_to_string(res) == '1034'
+
+        res = self.interpret(dummy, [-123])
+        assert self.ll_to_string(res) == '-123'
+
+        res = self.interpret(dummy, [-sys.maxint-1])
+        assert self.ll_to_string(res) == str(-sys.maxint-1)
+
+    def test_hex_of_int(self):
+        def dummy(i):
+            return hex(i)
+
+        res = self.interpret(dummy, [0])
+        assert self.ll_to_string(res) == '0x0'
+
+        res = self.interpret(dummy, [1034])
+        assert self.ll_to_string(res) == '0x40a'
+
+        res = self.interpret(dummy, [-123])
+        assert self.ll_to_string(res) == '-0x7b'
+
+    def test_oct_of_int(self):
+        def dummy(i):
+            return oct(i)
+
+        res = self.interpret(dummy, [0])
+        assert self.ll_to_string(res) == '0'
+
+        res = self.interpret(dummy, [1034])
+        assert self.ll_to_string(res) == '02012'
+
+        res = self.interpret(dummy, [-123])
+        assert self.ll_to_string(res) == '-0173'
+
+
+class TestLLtype(BaseTestRint, LLRtypeMixin):
+    pass
+
+class TestOOtype(BaseTestRint, OORtypeMixin):
+    pass

Modified: pypy/dist/pypy/rpython/typesystem.py
==============================================================================
--- pypy/dist/pypy/rpython/typesystem.py	(original)
+++ pypy/dist/pypy/rpython/typesystem.py	Wed May 17 23:17:44 2006
@@ -21,7 +21,7 @@
             except ImportError:
                 return None
         if name in ('rclass', 'rpbc', 'rbuiltin', 'rtuple', 'rlist',
-                'rslice', 'rdict', 'rrange', 'rstr', 'exceptiondata'):
+                'rslice', 'rdict', 'rrange', 'rstr', 'll_str', 'exceptiondata'):
             mod = load(name)
             if mod is not None:
                 setattr(self, name, mod)



More information about the Pypy-commit mailing list