summaryrefslogtreecommitdiffstats
path: root/Objects/floatobject.c
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-11-28 22:43:45 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-11-28 22:43:45 (GMT)
commit97019e41102df409556a76ca347ce9cf020bf23f (patch)
treeb25cb1deaeceb983795ac3924ca742020bcda120 /Objects/floatobject.c
parentb048b26db00b57fe5f23b4b81eae90a06c98fb0b (diff)
downloadcpython-97019e41102df409556a76ca347ce9cf020bf23f.zip
cpython-97019e41102df409556a76ca347ce9cf020bf23f.tar.gz
cpython-97019e41102df409556a76ca347ce9cf020bf23f.tar.bz2
PyFloat_AsStringEx(): This function takes an output char* but doesn't
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.
Diffstat (limited to 'Objects/floatobject.c')
-rw-r--r--Objects/floatobject.c32
1 files changed, 24 insertions, 8 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 02a1e1a..ea162df 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -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);
}