[pypy-svn] pypy ootype-virtualrefs: implement rstring_to_float for ootype
antocuni
commits-noreply at bitbucket.org
Thu Apr 7 16:33:34 CEST 2011
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ootype-virtualrefs
Changeset: r43198:764ef40b550c
Date: 2011-04-07 16:33 +0200
http://bitbucket.org/pypy/pypy/changeset/764ef40b550c/
Log: implement rstring_to_float for ootype
diff --git a/pypy/rlib/rfloat.py b/pypy/rlib/rfloat.py
--- a/pypy/rlib/rfloat.py
+++ b/pypy/rlib/rfloat.py
@@ -4,6 +4,7 @@
from pypy.rpython.tool import rffi_platform
from pypy.translator.tool.cbuild import ExternalCompilationInfo
from pypy.rlib import objectmodel
+from pypy.rpython.extfunc import register_external
USE_SHORT_FLOAT_REPR = True # XXX make it a translation option?
@@ -24,16 +25,33 @@
globals().update(rffi_platform.configure(CConfig))
def rstring_to_float(s):
+ return rstring_to_float_impl(s)
+
+def rstring_to_float_impl(s):
if USE_SHORT_FLOAT_REPR:
from pypy.rlib.rdtoa import strtod
return strtod(s)
-
sign, before_point, after_point, exponent = break_up_float(s)
-
if not before_point and not after_point:
raise ValueError
+ return parts_to_float(sign, before_point, after_point, exponent)
- return parts_to_float(sign, before_point, after_point, exponent)
+def oo_rstring_to_float(s):
+ from pypy.rpython.annlowlevel import oostr
+ from pypy.rpython.ootypesystem import ootype
+ lls = oostr(s)
+ return ootype.ooparse_float(lls)
+
+def ll_rstring_to_float(lls):
+ from pypy.rpython.annlowlevel import hlstr
+ s = hlstr(lls)
+ assert s is not None
+ return rstring_to_float_impl(s)
+
+register_external(rstring_to_float, [str], float,
+ llimpl=ll_rstring_to_float,
+ ooimpl=oo_rstring_to_float)
+
# float as string -> sign, beforept, afterpt, exponent
def break_up_float(s):
diff --git a/pypy/rpython/test/test_rfloat.py b/pypy/rpython/test/test_rfloat.py
--- a/pypy/rpython/test/test_rfloat.py
+++ b/pypy/rpython/test/test_rfloat.py
@@ -163,6 +163,13 @@
assert self.interpret(fn, [42, -1]) == -42
assert self.interpret(fn, [42, -0.0]) == -42
assert self.interpret(fn, [42, 0.0]) == 42
+
+ def test_rstring_to_float(self):
+ from pypy.rlib.rfloat import rstring_to_float
+ def fn(i):
+ s = ['42.3', '123.4'][i]
+ return rstring_to_float(s)
+ assert self.interpret(fn, [0]) == 42.3
class TestLLtype(BaseTestRfloat, LLRtypeMixin):
More information about the Pypy-commit
mailing list