8000 Remove PyFloat_AsReprString() and PyFloat_AsString() which should not · python/cpython@545686b · GitHub
[go: up one dir, main page]

Skip to content

Commit 545686b

Browse files
committed
Remove PyFloat_AsReprString() and PyFloat_AsString() which should not
have been public due to passing buffers without lengths.
1 parent 51d0022 commit 545686b

File tree

3 files changed

+27
-61
lines changed

3 files changed

+27
-61
lines changed

Include/floatobject.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,6 @@ PyAPI_FUNC(PyObject *) PyFloat_FromDouble(double);
3434
PyAPI_FUNC(double) PyFloat_AsDouble(PyObject *);
3535
#define PyFloat_AS_DOUBLE(op) (((PyFloatObject *)(op))->ob_fval)
3636

37-
/* Write repr(v) into the char buffer argument, followed by null byte. The
38-
buffer must be "big enough"; >= 100 is very safe.
39-
PyFloat_AsReprString(buf, x) strives to print enough digits so that
40-
PyFloat_FromString(buf) then reproduces x exactly. */
41-
PyAPI_FUNC(void) PyFloat_AsReprString(char*, PyFloatObject *v);
42-
43-
/* Write str(v) into the char buffer argument, followed by null byte. The
44-
buffer must be "big enough"; >= 100 is very safe. Note that it's
45-
unusual to be able to get back the float you started with from
46-
PyFloat_AsString's result -- use PyFloat_AsReprString() if you want to
47-
preserve precision across conversions. */
48-
PyAPI_FUNC(void) PyFloat_AsString(char*, PyFloatObject *v);
49-
5037
/* _PyFloat_{Pack,Unpack}{4,8}
5138
*
5239
* The struct and pickle (at least) modules need an efficient platform-
@@ -82,6 +69,11 @@ PyAPI_FUNC(void) PyFloat_AsString(char*, PyFloatObject *v);
8269
PyAPI_FUNC(int) _PyFloat_Pack4(double x, unsigned char *p, int le);
8370
PyAPI_FUNC(int) _PyFloat_Pack8(double x, unsigned char *p, int le);
8471

72+
/* Needed for the old way for marshal to store a floating point number.
73+
Returns the string length copied into p, -1 on error.
74+
*/
75+
PyAPI_FUNC(int) _PyFloat_Repr(double x, char *p, size_t len);
76+
8577
/* The unpack routines read 4 or 8 bytes, starting at p. le is a bool
8678
* argument, true if the string is in little-endian format (exponent
8779
* last, at p+3 or p+7), false if big-endian (exponent first, at p).
@@ -93,7 +85,6 @@ PyAPI_FUNC(int) _PyFloat_Pack8(double x, unsigned char *p, int le);
9385
PyAPI_FUNC(double) _PyFloat_Unpack4(const unsigned char *p, int le);
9486
PyAPI_FUNC(double) _PyFloat_Unpack8(const unsigned char *p, int le);
9587

96-
9788
#ifdef __cplusplus
9889
}
9990
#endif

Objects/floatobject.c

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,18 @@ PyFloat_AsDouble(PyObject *op)
220220
/* Methods */
221221

222222
static void
223-
format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
223+
format_double(char *buf, size_t buflen, double ob_fval, int precision)
224224
{
225225
register char *cp;
226226
char format[32];
227-
/* Subroutine for float_repr and float_print.
227+
/* Subroutine for float_repr, float_str, float_print and others.
228228
We want float numbers to be recognizable as such,
229229
i.e., they should contain a decimal point or an exponent.
230230
However, %g may print the number as an integer;
231231
in such cases, we append ".0" to the string. */
232232

233-
assert(PyFloat_Check(v));
234233
PyOS_snprintf(format, 32, "%%.%ig", precision);
235-
PyOS_ascii_formatd(buf, buflen, format, v->ob_fval);
234+
PyOS_ascii_formatd(buf, buflen, format, ob_fval);
236235
cp = buf;
237236
if (*cp == '-')
238237
cp++;
@@ -249,14 +248,11 @@ format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
249248
}
250249
}
251250

252-
/* XXX PyFloat_AsStringEx should not be a public API function (for one
253-
XXX thing, its signature passes a buffer without a length; for another,
254-
XXX it isn't useful outside this file).
255-
*/
256-
void
257-
PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
251+
static void
252+
format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
258253
{
259-
format_float(buf, 100, v, precision);
254+
assert(PyFloat_Check(v));
255+
format_double(buf, buflen, PyFloat_AS_DOUBLE(v), precision);
260256
}
261257

262258
/* Macro and helper that convert PyObject obj to a C double and store
@@ -312,21 +308,6 @@ convert_to_double(PyObject **v, double *dbl)
312308
#define PREC_REPR 17
313309
#define PREC_STR 12
314310

315-
/* XXX PyFloat_AsString and PyFloat_AsReprString should be deprecated:
316-
XXX they pass a char buffer without passing a length.
317-
*/
318-
void
319-
PyFloat_AsString(char *buf, PyFloatObject *v)
320-
{
321-
format_float(buf, 100, v, PREC_STR);
322-
}
323-
324-
void
325-
PyFloat_AsReprString(char *buf, PyFloatObject *v)
326-
{
327-
format_float(buf, 100, v, PREC_REPR);
328-
}
329-
330311
/* ARGSUSED */
331312
static int
332313
float_print(PyFloatObject *v, FILE *fp, int flags)
@@ -1275,7 +1256,7 @@ PyFloat_Fini(void)
12751256
if (PyFloat_CheckExact(p) &&
12761257
p->ob_refcnt != 0) {
12771258
char buf[100];
1278-
PyFloat_AsString(buf, p);
1259+
format_float(buf, sizeof(buf), p, PREC_STR);
12791260
/* XXX(twouters) cast refcount to
12801261
long until %zd is universally
12811262
available
@@ -1527,6 +1508,14 @@ _PyFloat_Pack8(double x, unsigned char *p, int le)
15271508
}
15281509
}
15291510

1511+
/* Should only be used by marshal. */
1512+
int
1513+
_PyFloat_Repr(double x, char *p, size_t len)
1514+
{
1515+
format_double(p, len, x, PREC_REPR);
1516+
return (int)strlen(p);
1517+
}
1518+
15301519
double
15311520
_PyFloat_Unpack4(const unsigned char *p, int le)
15321521
{

Python/marshal.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ w_object(PyObject *v, WFILE *p)
182182
}
183183
else {
184184
char buf[256]; /* Plenty to format any double */
185-
PyFloat_AsReprString(buf, (PyFloatObject *)v);
186-
n = strlen(buf);
185+
n = _PyFloat_Repr(PyFloat_AS_DOUBLE(v),
186+
buf, sizeof(buf));
187187
w_byte(TYPE_FLOAT, p);
188188
w_byte((int)n, p);
189189
w_string(buf, (int)n, p);
@@ -209,28 +209,14 @@ w_object(PyObject *v, WFILE *p)
209209
}
210210
else {
211211
char buf[256]; /* Plenty to format any double */
212-
PyFloatObject *temp;
213212
w_byte(TYPE_COMPLEX, p);
214-
temp = (PyFloatObject*)PyFloat_FromDouble(
215-
PyComplex_RealAsDouble(v));
216-
if (!temp) {
217-
p->error = 1;
218-
return;
219-
}
220-
PyFloat_AsReprString(buf, temp);
221-
Py_DECREF(temp);
213+
n = _PyFloat_Repr(PyComplex_RealAsDouble(v),
214+
buf, sizeof(buf));
222215
n = strlen(buf);
223216
w_byte((int)n, p);
224217
w_string(buf, (int)n, p);
225-
temp = (PyFloatObject*)PyFloat_FromDouble(
226-
PyComplex_ImagAsDouble(v));
227-
if (!temp) {
228-
p->error = 1;
229-
return;
230-
}
231-
PyFloat_AsReprString(buf, temp);
232-
Py_DECREF(temp);
233-
n = strlen(buf);
218+
n = _PyFloat_Repr(PyComplex_ImagAsDouble(v),
219+
buf, sizeof(buf));
234220
w_byte((int)n, p);
235221
w_string(buf, (int)n, p);
236222
}

0 commit comments

Comments
 (0)
0