]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
PyFloat_AsStringEx(): This function takes an output char* but doesn't
authorTim Peters <tim.peters@gmail.com>
Wed, 28 Nov 2001 22:43:45 +0000 (22:43 +0000)
committerTim Peters <tim.peters@gmail.com>
Wed, 28 Nov 2001 22:43:45 +0000 (22:43 +0000)
pass the buffer length.  Stop using it.  It should be deprecated, but too
late in the release cycle to do that now.
New static format_float() does the same thing but requires passing the
buffer length too.  Use it instead.

Objects/floatobject.c

index 02a1e1aa57e98fe01e2f846d784b2dd82ca14edb..ea162dfde7b6ebd231ec07fd2babf037eb9dae82 100644 (file)
@@ -225,8 +225,8 @@ PyFloat_AsDouble(PyObject *op)
 
 /* Methods */
 
-void
-PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
+static void
+format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
 {
        register char *cp;
        /* Subroutine for float_repr and float_print.
@@ -234,7 +234,9 @@ PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
           i.e., they should contain a decimal point or an exponent.
           However, %g may print the number as an integer;
           in such cases, we append ".0" to the string. */
-       sprintf(buf, "%.*g", precision, v->ob_fval);
+
+       assert(PyFloat_Check(v));
+       PyOS_snprintf(buf, buflen, "%.*g", precision, v->ob_fval);
        cp = buf;
        if (*cp == '-')
                cp++;
@@ -251,6 +253,16 @@ PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
        }
 }
 
+/* XXX PyFloat_AsStringEx should not be a public API function (for one
+   XXX thing, its signature passes a buffer without a length; for another,
+   XXX it isn't useful outside this file).
+*/
+void
+PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
+{
+       format_float(buf, 100, v, precision);
+}
+
 /* Macro and helper that convert PyObject obj to a C double and store
    the value in dbl; this replaces the functionality of the coercion
    slot function */
@@ -301,16 +313,19 @@ convert_to_double(PyObject **v, double *dbl)
 #define PREC_REPR      17
 #define PREC_STR       12
 
+/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
+   XXX they pass a char buffer without passing a length.
+*/
 void
 PyFloat_AsString(char *buf, PyFloatObject *v)
 {
-       PyFloat_AsStringEx(buf, v, PREC_STR);
+       format_float(buf, 100, v, PREC_STR);
 }
 
 void
 PyFloat_AsReprString(char *buf, PyFloatObject *v)
 {
-       PyFloat_AsStringEx(buf, v, PREC_REPR);
+       format_float(buf, 100, v, PREC_REPR);
 }
 
 /* ARGSUSED */
@@ -318,7 +333,8 @@ static int
 float_print(PyFloatObject *v, FILE *fp, int flags)
 {
        char buf[100];
-       PyFloat_AsStringEx(buf, v, flags&Py_PRINT_RAW ? PREC_STR : PREC_REPR);
+       format_float(buf, sizeof(buf), v,
+                    (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
        fputs(buf, fp);
        return 0;
 }
@@ -327,7 +343,7 @@ static PyObject *
 float_repr(PyFloatObject *v)
 {
        char buf[100];
-       PyFloat_AsStringEx(buf, v, PREC_REPR);
+       format_float(buf, sizeof(buf), v, PREC_REPR);
        return PyString_FromString(buf);
 }
 
@@ -335,7 +351,7 @@ static PyObject *
 float_str(PyFloatObject *v)
 {
        char buf[100];
-       PyFloat_AsStringEx(buf, v, PREC_STR);
+       format_float(buf, sizeof(buf), v, PREC_STR);
        return PyString_FromString(buf);
 }