[pypy-svn] pypy psycopg2compatibility: Added support for PyFloat_FromString() and test

ademan commits-noreply at bitbucket.org
Sat Dec 18 04:51:05 CET 2010


Author: Daniel Roberts <Ademan555 at gmail.com>
Branch: psycopg2compatibility
Changeset: r40119:898b357c14ba
Date: 2010-12-17 19:43 -0800
http://bitbucket.org/pypy/pypy/changeset/898b357c14ba/

Log:	Added support for PyFloat_FromString() and test

diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -928,13 +928,6 @@
     failure; the appropriate exception will be set."""
     raise NotImplementedError
 
- at cpython_api([PyObject, rffi.CCHARPP], PyObject)
-def PyFloat_FromString(space, str, pend):
-    """Create a PyFloatObject object based on the string value in str, or
-    NULL on failure.  The pend argument is ignored.  It remains only for
-    backward compatibility."""
-    raise NotImplementedError
-
 @cpython_api([rffi.VOIDP_real], PyObject)
 def PyFloat_GetInfo(space, info):
     """Return a structseq instance which contains information about the

diff --git a/pypy/module/cpyext/test/test_floatobject.py b/pypy/module/cpyext/test/test_floatobject.py
--- a/pypy/module/cpyext/test/test_floatobject.py
+++ b/pypy/module/cpyext/test/test_floatobject.py
@@ -1,4 +1,6 @@
 from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.module.cpyext.api import PyObject
 
 class TestFloatObject(BaseApiTest):
     def test_floatobject(self, space, api):
@@ -17,3 +19,18 @@
                 return 42.5
         assert space.eq_w(api.PyNumber_Float(space.wrap(Coerce())),
                           space.wrap(42.5))
+
+    def test_from_string(self, space, api):
+        def test_number(n, expectfail=False):
+            np = lltype.nullptr(rffi.CCHARPP.TO)
+            n_str = rffi.str2charp(str(n))
+            f = api.PyFloat_FromString(n_str, np)
+            rffi.free_charp(n_str)
+            if expectfail:
+                assert f == None
+            else:
+                assert space.eq_w(f, space.wrap(n))
+
+        test_number(0.0)
+        test_number(42.0)
+        test_number("abcd", True)

diff --git a/pypy/module/cpyext/floatobject.py b/pypy/module/cpyext/floatobject.py
--- a/pypy/module/cpyext/floatobject.py
+++ b/pypy/module/cpyext/floatobject.py
@@ -3,6 +3,8 @@
                                     build_type_checkers)
 from pypy.interpreter.error import OperationError
 
+from pypy.objspace.std.strutil import interp_string_to_float, ParseStringError
+
 PyFloat_Check, PyFloat_CheckExact = build_type_checkers("Float")
 
 @cpython_api([lltype.Float], PyObject)
@@ -25,3 +27,17 @@
     Returns the o converted to a float object on success, or NULL on failure.
     This is the equivalent of the Python expression float(o)."""
     return space.float(w_obj)
+
+ at cpython_api([PyObject, rffi.CCHARPP], PyObject)
+def PyFloat_FromString(space, str, pend):
+    """Create a PyFloatObject object based on the string value in str, or
+    NULL on failure.  The pend argument is ignored.  It remains only for
+    backward compatibility."""
+    
+    str = rffi.charp2str(str)
+    try:
+        float_value = interp_string_to_float(space, str)
+    except ParseStringError, e:
+        return None
+    return space.wrap(float_value)
+


More information about the Pypy-commit mailing list