Skip to content

Commit cc32a11

Browse files
committed
Issue #5835: Deprecate PyOS_ascii_formatd.
1 parent 886b40a commit cc32a11

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

Doc/c-api/conversion.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@ The following functions provide locale-independent string to number conversions.
7373
The return value is a pointer to *buffer* with the converted string or NULL if
7474
the conversion failed.
7575

76+
.. deprecated:: 3.1
77+
Use :cfunc:`PyOS_double_to_string` instead.
78+
79+
80+
.. cfunction:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
81+
82+
Convert a :ctype:`double` *val* to a string using supplied
83+
*format_code*, *precision*, and *flags*.
84+
85+
*format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``, ``'g'``,
86+
``'G'``, ``'s'``, or ``'r'``. For ``'s'`` and ``'r'``, the supplied
87+
*precision* must be 0 and is ignored. These specify the standard
88+
:func:`str` and :func:`repr` formats, respectively.
89+
90+
*flags* can be zero or more of the values *Py_DTSF_SIGN*,
91+
*Py_DTSF_ADD_DOT_0*, or *Py_DTSF_ALT*, or-ed together:
92+
93+
* *Py_DTSF_SIGN* means to always precede the returned string with a sign
94+
character, even if *val* is non-negative.
95+
96+
* *Py_DTSF_ADD_DOT_0* means to ensure that the returned string will not look
97+
like an integer.
98+
99+
* *Py_DTSF_ALT* means to apply "alternate" formatting rules. See the
100+
documentation for the :cfunc:`PyOS_snprintf` ``'#'`` specifier for
101+
details.
102+
103+
If *ptype* is non-NULL, then the value it points to will be set to one of
104+
*Py_DTST_FINITE*, *Py_DTST_INFINITE*, or *Py_DTST_NAN*, signifying that
105+
*val* is a finite number, an infinite number, or not a number, respectively.
106+
107+
The return value is a pointer to *buffer* with the converted string or
108+
*NULL* if the conversion failed. The caller is responsible for freeing the
109+
returned string by calling :cfunc:`PyMem_Free`.
110+
111+
.. versionadded:: 3.1
112+
76113

77114
.. cfunction:: double PyOS_ascii_atof(const char *nptr)
78115

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 3.1 beta 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #5835: Deprecate PyOS_ascii_formatd.
16+
1517
- Issue #4971: Fix titlecase for characters that are their own
1618
titlecase, but not their own uppercase.
1719

Python/pystrtod.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ ensure_decimal_point(char* buffer, size_t buf_size)
433433
* Return value: The pointer to the buffer with the converted string.
434434
**/
435435
char *
436-
PyOS_ascii_formatd(char *buffer,
436+
_PyOS_ascii_formatd(char *buffer,
437437
size_t buf_size,
438438
const char *format,
439439
double d)
@@ -508,6 +508,20 @@ PyOS_ascii_formatd(char *buffer,
508508
return buffer;
509509
}
510510

511+
char *
512+
PyOS_ascii_formatd(char *buffer,
513+
size_t buf_size,
514+
const char *format,
515+
double d)
516+
{
517+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
518+
"PyOS_ascii_formatd is deprecated, "
519+
"use PyOS_double_to_string instead", 1) < 0)
520+
return NULL;
521+
522+
return _PyOS_ascii_formatd(buffer, buf_size, format, d);
523+
}
524+
511525
#ifdef PY_NO_SHORT_FLOAT_REPR
512526

513527
/* The fallback code to use if _Py_dg_dtoa is not available. */
@@ -638,8 +652,10 @@ PyAPI_FUNC(char *) PyOS_double_to_string(double val,
638652
if ((flags & Py_DTSF_ADD_DOT_0) && (format_code != 'e'))
639653
format_code = 'Z';
640654

641-
PyOS_snprintf(format, 32, "%%%s.%i%c", (flags & Py_DTSF_ALT ? "#" : ""), precision, format_code);
642-
PyOS_ascii_formatd(buf, sizeof(buf), format, val);
655+
PyOS_snprintf(format, sizeof(format), "%%%s.%i%c",
656+
(flags & Py_DTSF_ALT ? "#" : ""), precision,
657+
format_code);
658+
_PyOS_ascii_formatd(buf, sizeof(buf), format, val);
643659
/* remove trailing zeros if necessary */
644660
if (strip_trailing_zeros)
645661
remove_trailing_zeros(buf);

0 commit comments

Comments
 (0)