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.
This commit is contained in:
Tim Peters 2001-11-28 22:43:45 +00:00
parent b048b26db0
commit 97019e4110

View File

@ -225,8 +225,8 @@ PyFloat_AsDouble(PyObject *op)
/* Methods */ /* Methods */
void static void
PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision) format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
{ {
register char *cp; register char *cp;
/* Subroutine for float_repr and float_print. /* 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. i.e., they should contain a decimal point or an exponent.
However, %g may print the number as an integer; However, %g may print the number as an integer;
in such cases, we append ".0" to the string. */ 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; cp = buf;
if (*cp == '-') if (*cp == '-')
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 /* Macro and helper that convert PyObject obj to a C double and store
the value in dbl; this replaces the functionality of the coercion the value in dbl; this replaces the functionality of the coercion
slot function */ slot function */
@ -301,16 +313,19 @@ convert_to_double(PyObject **v, double *dbl)
#define PREC_REPR 17 #define PREC_REPR 17
#define PREC_STR 12 #define PREC_STR 12
/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
XXX they pass a char buffer without passing a length.
*/
void void
PyFloat_AsString(char *buf, PyFloatObject *v) PyFloat_AsString(char *buf, PyFloatObject *v)
{ {
PyFloat_AsStringEx(buf, v, PREC_STR); format_float(buf, 100, v, PREC_STR);
} }
void void
PyFloat_AsReprString(char *buf, PyFloatObject *v) PyFloat_AsReprString(char *buf, PyFloatObject *v)
{ {
PyFloat_AsStringEx(buf, v, PREC_REPR); format_float(buf, 100, v, PREC_REPR);
} }
/* ARGSUSED */ /* ARGSUSED */
@ -318,7 +333,8 @@ static int
float_print(PyFloatObject *v, FILE *fp, int flags) float_print(PyFloatObject *v, FILE *fp, int flags)
{ {
char buf[100]; 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); fputs(buf, fp);
return 0; return 0;
} }
@ -327,7 +343,7 @@ static PyObject *
float_repr(PyFloatObject *v) float_repr(PyFloatObject *v)
{ {
char buf[100]; char buf[100];
PyFloat_AsStringEx(buf, v, PREC_REPR); format_float(buf, sizeof(buf), v, PREC_REPR);
return PyString_FromString(buf); return PyString_FromString(buf);
} }
@ -335,7 +351,7 @@ static PyObject *
float_str(PyFloatObject *v) float_str(PyFloatObject *v)
{ {
char buf[100]; char buf[100];
PyFloat_AsStringEx(buf, v, PREC_STR); format_float(buf, sizeof(buf), v, PREC_STR);
return PyString_FromString(buf); return PyString_FromString(buf);
} }