[pypy-svn] r35735 - in pypy/dist/pypy: rpython/ootypesystem translator/cli translator/cli/src translator/cli/test

antocuni at codespeak.net antocuni at codespeak.net
Thu Dec 14 13:59:24 CET 2006


Author: antocuni
Date: Thu Dec 14 13:59:18 2006
New Revision: 35735

Modified:
   pypy/dist/pypy/rpython/ootypesystem/ooregistry.py
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/rstr.py
   pypy/dist/pypy/translator/cli/opcodes.py
   pypy/dist/pypy/translator/cli/src/pypylib.cs
   pypy/dist/pypy/translator/cli/test/test_float.py
Log:
Support for float(string) in ootypesystem and gencli.



Modified: pypy/dist/pypy/rpython/ootypesystem/ooregistry.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ooregistry.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ooregistry.py	Thu Dec 14 13:59:18 2006
@@ -49,6 +49,24 @@
         hop.exception_is_here()
         return hop.genop('ooparse_int', vlist, resulttype = ootype.Signed)
 
+
+class Entry_ooparse_float(ExtRegistryEntry):
+    _about_ = ootype.ooparse_float
+
+    def compute_result_annotation(self, str_s):
+        assert isinstance(str_s, annmodel.SomeOOInstance)\
+               and str_s.ootype is ootype.String
+        return annmodel.SomeFloat()
+
+    def specialize_call(self, hop):
+        assert isinstance(hop.args_s[0], annmodel.SomeOOInstance)\
+               and hop.args_s[0].ootype is ootype.String
+        vlist = hop.inputargs(hop.args_r[0])
+        hop.has_implicit_exception(ValueError)
+        hop.exception_is_here()
+        return hop.genop('ooparse_float', vlist, resulttype = ootype.Float)
+
+
 class Entry_oohash(ExtRegistryEntry):
     _about_ = ootype.oohash
 

Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Thu Dec 14 13:59:18 2006
@@ -1417,6 +1417,9 @@
 def ooparse_int(s, base):
     return int(s._str, base)
 
+def ooparse_float(s):
+    return float(s._str)
+
 def setItemType(LIST, ITEMTYPE):
     return LIST._set_itemtype(ITEMTYPE)
 

Modified: pypy/dist/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rstr.py	Thu Dec 14 13:59:18 2006
@@ -193,6 +193,9 @@
             raise ValueError
         return sign * val
 
+    def ll_float(ll_str):
+        return ootype.ooparse_float(ll_str)
+    
     # interface to build strings:
     #   x = ll_build_start(n)
     #   ll_build_push(x, next_string, 0)

Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Thu Dec 14 13:59:18 2006
@@ -49,6 +49,7 @@
     'oohash':                   [PushAllArgs, 'callvirt instance int32 object::GetHashCode()'],    
     'oostring':                 [OOString],
     'ooparse_int':              [PushAllArgs, 'call int32 [pypylib]pypy.runtime.Utils::OOParseInt(string, int32)'],
+    'ooparse_float':            [PushAllArgs, 'call float64 [pypylib]pypy.runtime.Utils::OOParseFloat(string)'],
     'oonewcustomdict':          [NewCustomDict],
     
     'same_as':                  DoNothing,

Modified: pypy/dist/pypy/translator/cli/src/pypylib.cs
==============================================================================
--- pypy/dist/pypy/translator/cli/src/pypylib.cs	(original)
+++ pypy/dist/pypy/translator/cli/src/pypylib.cs	Thu Dec 14 13:59:18 2006
@@ -108,6 +108,17 @@
             return Convert.ToInt32(s, base_);
         }
 
+        public static double OOParseFloat(string s)
+        {
+            try {
+                return Double.Parse(s.Trim());
+            }
+            catch(FormatException e) {
+                Helpers.raise_ValueError();
+                return -1;
+            }
+        }
+
         public static bool Equal<T>(T t1, T t2) 
         { 
             return t1.Equals(t2);

Modified: pypy/dist/pypy/translator/cli/test/test_float.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_float.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_float.py	Thu Dec 14 13:59:18 2006
@@ -5,3 +5,16 @@
 class TestCliFloat(CliTest, BaseTestRfloat):
     pass
 
+    def test_float(self):
+        ex = ['', '    ', '0', '1', '-1.5', '1.5E2', '2.5e-1', ' 0 ', '?']
+        def fn(i):
+            s = ex[i]
+            try:
+                return float(s)
+            except ValueError:
+                return -999.0
+        
+        for i in range(len(ex)):
+            expected = fn(i)
+            res = self.interpret(fn, [i])
+            assert res == expected



More information about the Pypy-commit mailing list