[pypy-commit] pypy default: pystrtod.py edited online with Bitbucket

yufeiz noreply at buildbot.pypy.org
Fri Nov 13 05:29:43 EST 2015


Author: Faye Zhao <yufeizhao at google.com>
Branch: 
Changeset: r80658:4d1ea0f21109
Date: 2015-11-13 00:16 +0000
http://bitbucket.org/pypy/pypy/changeset/4d1ea0f21109/

Log:	pystrtod.py edited online with Bitbucket

diff --git a/pypy/module/cpyext/pystrtod.py b/pypy/module/cpyext/pystrtod.py
--- a/pypy/module/cpyext/pystrtod.py
+++ b/pypy/module/cpyext/pystrtod.py
@@ -9,6 +9,18 @@
 from rpython.rtyper.lltypesystem import rffi
 
 
+# PyOS_double_to_string's "type", if non-NULL, will be set to one of:
+Py_DTST_FINITE = 0
+Py_DTST_INFINITE = 1
+Py_DTST_NAN = 2
+
+# Match the "type" back to values in CPython
+DOUBLE_TO_STRING_TYPES_MAP = {
+    rfloat.DIST_FINITE: Py_DTST_FINITE,
+    rfloat.DIST_INFINITY: Py_DTST_INFINITE,
+    rfloat.DIST_NAN: Py_DTST_NAN
+}
+
 @cpython_api([rffi.CCHARP, rffi.CCHARPP, PyObject], rffi.DOUBLE, error=-1.0)
 @jit.dont_look_inside       # direct use of _get_errno()
 def PyOS_string_to_double(space, s, endptr, w_overflow_exception):
@@ -68,3 +80,40 @@
     finally:
         if not user_endptr:
             lltype.free(endptr, flavor='raw')
+
+ at cpython_api([rffi.DOUBLE, lltype.Char, rffi.INT_real, rffi.INT_real, rffi.INTP], rffi.CCHARP)
+def PyOS_double_to_string(space, val, format_code, precision, flags, ptype):
+    """Convert a double val to a string using supplied
+    format_code, precision, and flags.
+
+    format_code must be one of 'e', 'E', 'f', 'F',
+    'g', 'G' or 'r'.  For 'r', the supplied precision
+    must be 0 and is ignored.  The 'r' format code specifies the
+    standard repr() format.
+
+    flags can be zero or more of the values Py_DTSF_SIGN,
+    Py_DTSF_ADD_DOT_0, or Py_DTSF_ALT, or-ed together:
+
+    Py_DTSF_SIGN means to always precede the returned string with a sign
+    character, even if val is non-negative.
+
+    Py_DTSF_ADD_DOT_0 means to ensure that the returned string will not look
+    like an integer.
+
+    Py_DTSF_ALT means to apply "alternate" formatting rules.  See the
+    documentation for the PyOS_snprintf() '#' specifier for
+    details.
+
+    If ptype is non-NULL, then the value it points to will be set to one of
+    Py_DTST_FINITE, Py_DTST_INFINITE, or Py_DTST_NAN, signifying that
+    val is a finite number, an infinite number, or not a number, respectively.
+
+    The return value is a pointer to buffer with the converted string or
+    NULL if the conversion failed. The caller is responsible for freeing the
+    returned string by calling PyMem_Free().
+    """
+    buffer, rtype = rfloat.double_to_string(val, format_code, precision, flags)
+    if ptype != lltype.nullptr(rffi.INTP.TO):
+        ptype[0] = rffi.cast(rffi.INT, DOUBLE_TO_STRING_TYPES_MAP[rtype])
+    bufp = rffi.str2charp(buffer)
+    return bufp


More information about the pypy-commit mailing list