Skip to content

Commit cfaf79c

Browse files
committed
Start to remove _PyOS_double_to_string, as mentioned in issue 7117.
1 parent 0e0e215 commit cfaf79c

1 file changed

Lines changed: 36 additions & 22 deletions

File tree

‎Objects/floatobject.c‎

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -335,58 +335,72 @@ convert_to_double(PyObject **v, double *dbl)
335335
return 0;
336336
}
337337

338-
/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
338+
/* XXX PyFloat_AsString and PyFloat_AsReprString are deprecated:
339339
XXX they pass a char buffer without passing a length.
340340
*/
341341
void
342342
PyFloat_AsString(char *buf, PyFloatObject *v)
343343
{
344-
_PyOS_double_to_string(buf, 100, v->ob_fval, 'g', PyFloat_STR_PRECISION,
345-
Py_DTSF_ADD_DOT_0, NULL);
344+
char *tmp = PyOS_double_to_string(v->ob_fval, 'g',
345+
PyFloat_STR_PRECISION,
346+
Py_DTSF_ADD_DOT_0, NULL);
347+
strcpy(buf, tmp);
348+
PyMem_Free(tmp);
346349
}
347350

348351
void
349352
PyFloat_AsReprString(char *buf, PyFloatObject *v)
350353
{
351-
_PyOS_double_to_string(buf, 100, v->ob_fval, 'r', 0,
352-
Py_DTSF_ADD_DOT_0, NULL);
354+
char * tmp = PyOS_double_to_string(v->ob_fval, 'r', 0,
355+
Py_DTSF_ADD_DOT_0, NULL);
356+
strcpy(buf, tmp);
357+
PyMem_Free(tmp);
353358
}
354359

355360
/* ARGSUSED */
356361
static int
357362
float_print(PyFloatObject *v, FILE *fp, int flags)
358363
{
359-
char buf[100];
360-
if (flags & Py_PRINT_RAW)
361-
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval,
362-
'g', PyFloat_STR_PRECISION,
363-
Py_DTSF_ADD_DOT_0, NULL);
364-
else
365-
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval,
366-
'r', 0, Py_DTSF_ADD_DOT_0, NULL);
364+
char *buf;
365+
if (flags & Py_PRINT_RAW)
366+
buf = PyOS_double_to_string(v->ob_fval,
367+
'g', PyFloat_STR_PRECISION,
368+
Py_DTSF_ADD_DOT_0, NULL);
369+
else
370+
buf = PyOS_double_to_string(v->ob_fval,
371+
'r', 0, Py_DTSF_ADD_DOT_0, NULL);
367372
Py_BEGIN_ALLOW_THREADS
368373
fputs(buf, fp);
369374
Py_END_ALLOW_THREADS
375+
PyMem_Free(buf);
370376
return 0;
371377
}
372378

379+
static PyObject *
380+
float_str_or_repr(PyFloatObject *v, int precision, char format_code)
381+
{
382+
PyObject *result;
383+
char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
384+
format_code, precision,
385+
Py_DTSF_ADD_DOT_0,
386+
NULL);
387+
if (!buf)
388+
return PyErr_NoMemory();
389+
result = PyString_FromString(buf);
390+
PyMem_Free(buf);
391+
return result;
392+
}
393+
373394
static PyObject *
374395
float_repr(PyFloatObject *v)
375396
{
376-
char buf[100];
377-
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval, 'r', 0,
378-
Py_DTSF_ADD_DOT_0, NULL);
379-
return PyString_FromString(buf);
397+
return float_str_or_repr(v, 0, 'r');
380398
}
381399

382400
static PyObject *
383401
float_str(PyFloatObject *v)
384402
{
385-
char buf[100];
386-
_PyOS_double_to_string(buf, sizeof(buf), v->ob_fval, 'g',
387-
PyFloat_STR_PRECISION,
388-
Py_DTSF_ADD_DOT_0, NULL);
389-
return PyString_FromString(buf);
403+
return float_str_or_repr(v, PyFloat_STR_PRECISION, 'g');
390404
}
391405

392406
/* Comparison is pretty much a nightmare. When comparing float to float,

0 commit comments

Comments
 (0)