annotate Python/pystrtod.c @ 58664:993f703a61ba

Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77484 | skip.montanaro | 2010-01-13 19:12:34 -0600 (Wed, 13 Jan 2010) | 4 lines Update PyEval_EvalFrame to PyEval_EvalFrameEx. This looks to have been done partially before. Also add a comment describing how this might have to work with different versions of the interpreter. ........ r77487 | ezio.melotti | 2010-01-14 05:34:10 -0600 (Thu, 14 Jan 2010) | 1 line Fixed typo ........ r77561 | georg.brandl | 2010-01-17 02:42:30 -0600 (Sun, 17 Jan 2010) | 1 line #7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such. ........ r77570 | georg.brandl | 2010-01-17 06:14:42 -0600 (Sun, 17 Jan 2010) | 1 line Add note about usage of STRINGLIB_EMPTY. ........ r77593 | georg.brandl | 2010-01-17 17:33:53 -0600 (Sun, 17 Jan 2010) | 1 line Fix internal reference. ........ r77603 | benjamin.peterson | 2010-01-18 17:07:56 -0600 (Mon, 18 Jan 2010) | 8 lines data descriptors do not override the class dictionary if __get__ is not defined Adjust documentation and add a test to verify this behavior. See http://mail.python.org/pipermail/python-dev/2010-January/095637.html for discussion. ........ r77608 | gregory.p.smith | 2010-01-19 02:19:03 -0600 (Tue, 19 Jan 2010) | 6 lines Do not compile stubs for the sha2 series hashes in the openssl hashlib module when the openssl version is too old to support them. That leads both compiled code bloat and to unittests attempting to test implementations that don't exist for comparison purposes on such platforms. ........ r77667 | mark.dickinson | 2010-01-21 12:32:27 -0600 (Thu, 21 Jan 2010) | 1 line Add two more test_strtod test values. ........ r77702 | georg.brandl | 2010-01-23 02:43:31 -0600 (Sat, 23 Jan 2010) | 1 line #7762: fix refcount annotation of PyUnicode_Tailmatch(). ........ r77703 | georg.brandl | 2010-01-23 02:47:54 -0600 (Sat, 23 Jan 2010) | 1 line #7725: fix referencing issue. ........ r77739 | benjamin.peterson | 2010-01-24 21:52:52 -0600 (Sun, 24 Jan 2010) | 1 line mention from_float() in error message ........ r77858 | georg.brandl | 2010-01-30 11:57:48 -0600 (Sat, 30 Jan 2010) | 1 line #7802: fix invalid example (heh). ........ r77887 | georg.brandl | 2010-01-31 12:51:49 -0600 (Sun, 31 Jan 2010) | 5 lines Fix-up ftplib documentation: move exception descriptions to toplevel, not inside a class remove attribution in "versionadded" spell and grammar check docstring of FTP_TLS ........ r77889 | michael.foord | 2010-01-31 13:59:26 -0600 (Sun, 31 Jan 2010) | 1 line Minor modification to unittest documentation. ........
author Benjamin Peterson <benjamin@python.org>
date Wed, 03 Feb 2010 02:35:45 +0000
parents be12d3621519
children f0acf2138a96
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
1 /* -*- Mode: C; c-file-style: "python" -*- */
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
2
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
3 #include <Python.h>
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
4 #include <locale.h>
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
5
56752
f598843f3695 Move some comments to more appropriate places
Mark Dickinson <dickinsm@gmail.com>
parents: 54232
diff changeset
6 /* Case-insensitive string match used for nan and inf detection; t should be
f598843f3695 Move some comments to more appropriate places
Mark Dickinson <dickinsm@gmail.com>
parents: 54232
diff changeset
7 lower-case. Returns 1 for a successful match, 0 otherwise. */
54232
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
8
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
9 static int
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
10 case_insensitive_match(const char *s, const char *t)
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
11 {
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
12 while(*t && Py_TOLOWER(*s) == *t) {
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
13 s++;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
14 t++;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
15 }
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
16 return *t ? 0 : 1;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
17 }
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
18
56752
f598843f3695 Move some comments to more appropriate places
Mark Dickinson <dickinsm@gmail.com>
parents: 54232
diff changeset
19 /* _Py_parse_inf_or_nan: Attempt to parse a string of the form "nan", "inf" or
f598843f3695 Move some comments to more appropriate places
Mark Dickinson <dickinsm@gmail.com>
parents: 54232
diff changeset
20 "infinity", with an optional leading sign of "+" or "-". On success,
f598843f3695 Move some comments to more appropriate places
Mark Dickinson <dickinsm@gmail.com>
parents: 54232
diff changeset
21 return the NaN or Infinity as a double and set *endptr to point just beyond
f598843f3695 Move some comments to more appropriate places
Mark Dickinson <dickinsm@gmail.com>
parents: 54232
diff changeset
22 the successfully parsed portion of the string. On failure, return -1.0 and
f598843f3695 Move some comments to more appropriate places
Mark Dickinson <dickinsm@gmail.com>
parents: 54232
diff changeset
23 set *endptr to point to the start of the string. */
f598843f3695 Move some comments to more appropriate places
Mark Dickinson <dickinsm@gmail.com>
parents: 54232
diff changeset
24
54232
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
25 double
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
26 _Py_parse_inf_or_nan(const char *p, char **endptr)
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
27 {
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
28 double retval;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
29 const char *s;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
30 int negate = 0;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
31
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
32 s = p;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
33 if (*s == '-') {
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
34 negate = 1;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
35 s++;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
36 }
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
37 else if (*s == '+') {
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
38 s++;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
39 }
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
40 if (case_insensitive_match(s, "inf")) {
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
41 s += 3;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
42 if (case_insensitive_match(s, "inity"))
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
43 s += 5;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
44 retval = negate ? -Py_HUGE_VAL : Py_HUGE_VAL;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
45 }
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
46 #ifdef Py_NAN
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
47 else if (case_insensitive_match(s, "nan")) {
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
48 s += 3;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
49 retval = negate ? -Py_NAN : Py_NAN;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
50 }
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
51 #endif
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
52 else {
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
53 s = p;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
54 retval = -1.0;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
55 }
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
56 *endptr = (char *)s;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
57 return retval;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
58 }
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
59
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
60 /**
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
61 * PyOS_ascii_strtod:
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
62 * @nptr: the string to convert to a numeric value.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
63 * @endptr: if non-%NULL, it returns the character after
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
64 * the last character used in the conversion.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
65 *
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
66 * Converts a string to a #gdouble value.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
67 * This function behaves like the standard strtod() function
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
68 * does in the C locale. It does this without actually
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
69 * changing the current locale, since that would not be
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
70 * thread-safe.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
71 *
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
72 * This function is typically used when reading configuration
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
73 * files or other non-user input that should be locale independent.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
74 * To handle input from the user you should normally use the
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
75 * locale-sensitive system strtod() function.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
76 *
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
77 * If the correct value would cause overflow, plus or minus %HUGE_VAL
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
78 * is returned (according to the sign of the value), and %ERANGE is
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
79 * stored in %errno. If the correct value would cause underflow,
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
80 * zero is returned and %ERANGE is stored in %errno.
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
81 * If memory allocation fails, %ENOMEM is stored in %errno.
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
82 *
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
83 * This function resets %errno before calling strtod() so that
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
84 * you can reliably detect overflow and underflow.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
85 *
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
86 * Return value: the #gdouble value.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
87 **/
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
88
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
89 #ifndef PY_NO_SHORT_FLOAT_REPR
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
90
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
91 double
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
92 _PyOS_ascii_strtod(const char *nptr, char **endptr)
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
93 {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
94 double result;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
95 _Py_SET_53BIT_PRECISION_HEADER;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
96
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
97 assert(nptr != NULL);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
98 /* Set errno to zero, so that we can distinguish zero results
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
99 and underflows */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
100 errno = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
101
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
102 _Py_SET_53BIT_PRECISION_START;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
103 result = _Py_dg_strtod(nptr, endptr);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
104 _Py_SET_53BIT_PRECISION_END;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
105
54232
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
106 if (*endptr == nptr)
58467
be12d3621519 Merged revisions 77691,77698,77713-77714 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 56752
diff changeset
107 /* string might represent an inf or nan */
54232
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
108 result = _Py_parse_inf_or_nan(nptr, endptr);
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
109
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
110 return result;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
111
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
112 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
113
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
114 #else
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
115
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
116 /*
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
117 Use system strtod; since strtod is locale aware, we may
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
118 have to first fix the decimal separator.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
119
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
120 Note that unlike _Py_dg_strtod, the system strtod may not always give
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
121 correctly rounded results.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
122 */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
123
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
124 double
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
125 _PyOS_ascii_strtod(const char *nptr, char **endptr)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
126 {
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
127 char *fail_pos;
36080
10440bcfa061 Fix compiler warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 36077
diff changeset
128 double val = -1.0;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
129 struct lconv *locale_data;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
130 const char *decimal_point;
36230
ded4a497eb76 Fix icc warnings: strlen() returns size_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 36080
diff changeset
131 size_t decimal_point_len;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
132 const char *p, *decimal_point_pos;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
133 const char *end = NULL; /* Silence gcc */
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
134 const char *digits_pos = NULL;
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
135 int negate = 0;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
136
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
137 assert(nptr != NULL);
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
138
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
139 fail_pos = NULL;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
140
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
141 locale_data = localeconv();
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
142 decimal_point = locale_data->decimal_point;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
143 decimal_point_len = strlen(decimal_point);
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
144
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
145 assert(decimal_point_len != 0);
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
146
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
147 decimal_point_pos = NULL;
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
148
54232
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
149 /* Parse infinities and nans */
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
150 val = _Py_parse_inf_or_nan(nptr, endptr);
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
151 if (*endptr != nptr)
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
152 return val;
4501b638fc3f Refactor to remove duplicated nan/inf parsing code in
Mark Dickinson <dickinsm@gmail.com>
parents: 53787
diff changeset
153
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
154 /* Set errno to zero, so that we can distinguish zero results
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
155 and underflows */
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
156 errno = 0;
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
157
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
158 /* We process the optional sign manually, then pass the remainder to
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
159 the system strtod. This ensures that the result of an underflow
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
160 has the correct sign. (bug #1725) */
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
161 p = nptr;
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
162 /* Process leading sign, if present */
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
163 if (*p == '-') {
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
164 negate = 1;
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
165 p++;
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
166 }
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
167 else if (*p == '+') {
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
168 p++;
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
169 }
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
170
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
171 /* Some platform strtods accept hex floats; Python shouldn't (at the
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
172 moment), so we check explicitly for strings starting with '0x'. */
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
173 if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X'))
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
174 goto invalid_string;
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
175
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
176 /* Check that what's left begins with a digit or decimal point */
53543
4b4b59f278bf Merged revisions 72040 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53489
diff changeset
177 if (!Py_ISDIGIT(*p) && *p != '.')
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
178 goto invalid_string;
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
179
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
180 digits_pos = p;
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
181 if (decimal_point[0] != '.' ||
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
182 decimal_point[1] != 0)
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
183 {
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
184 /* Look for a '.' in the input; if present, it'll need to be
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
185 swapped for the current locale's decimal point before we
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
186 call strtod. On the other hand, if we find the current
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
187 locale's decimal point then the input is invalid. */
53543
4b4b59f278bf Merged revisions 72040 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53489
diff changeset
188 while (Py_ISDIGIT(*p))
36077
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
189 p++;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
190
36077
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
191 if (*p == '.')
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
192 {
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
193 decimal_point_pos = p++;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
194
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
195 /* locate end of number */
53543
4b4b59f278bf Merged revisions 72040 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53489
diff changeset
196 while (Py_ISDIGIT(*p))
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
197 p++;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
198
36077
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
199 if (*p == 'e' || *p == 'E')
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
200 p++;
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
201 if (*p == '+' || *p == '-')
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
202 p++;
53543
4b4b59f278bf Merged revisions 72040 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53489
diff changeset
203 while (Py_ISDIGIT(*p))
36077
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
204 p++;
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
205 end = p;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
206 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
207 else if (strncmp(p, decimal_point, decimal_point_len) == 0)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
208 /* Python bug #1417699 */
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
209 goto invalid_string;
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
210 /* For the other cases, we need not convert the decimal
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
211 point */
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
212 }
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
213
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
214 if (decimal_point_pos) {
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
215 char *copy, *c;
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
216 /* Create a copy of the input, with the '.' converted to the
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
217 locale-specific decimal point */
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
218 copy = (char *)PyMem_MALLOC(end - digits_pos +
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
219 1 + decimal_point_len);
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
220 if (copy == NULL) {
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
221 *endptr = (char *)nptr;
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
222 errno = ENOMEM;
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
223 return val;
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
224 }
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
225
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
226 c = copy;
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
227 memcpy(c, digits_pos, decimal_point_pos - digits_pos);
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
228 c += decimal_point_pos - digits_pos;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
229 memcpy(c, decimal_point, decimal_point_len);
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
230 c += decimal_point_len;
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
231 memcpy(c, decimal_point_pos + 1,
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
232 end - (decimal_point_pos + 1));
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
233 c += end - (decimal_point_pos + 1);
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
234 *c = 0;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
235
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
236 val = strtod(copy, &fail_pos);
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
237
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
238 if (fail_pos)
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
239 {
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
240 if (fail_pos > decimal_point_pos)
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
241 fail_pos = (char *)digits_pos +
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
242 (fail_pos - copy) -
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
243 (decimal_point_len - 1);
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
244 else
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
245 fail_pos = (char *)digits_pos +
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
246 (fail_pos - copy);
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
247 }
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
248
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
249 PyMem_FREE(copy);
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
250
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
251 }
36077
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
252 else {
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
253 val = strtod(digits_pos, &fail_pos);
36077
3b90258fec9f Get float() to be more portable across platforms. Disable hex strings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
254 }
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
255
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
256 if (fail_pos == digits_pos)
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
257 goto invalid_string;
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
258
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
259 if (negate && fail_pos != nptr)
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 39442
diff changeset
260 val = -val;
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
261 *endptr = fail_pos;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
262
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
263 return val;
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
264
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
265 invalid_string:
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
266 *endptr = (char*)nptr;
53468
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
267 errno = EINVAL;
f76572108dd7 Internal plumbing changes for float parsing:
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
268 return -1.0;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
269 }
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
270
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
271 #endif
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
272
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
273 /* PyOS_ascii_strtod is DEPRECATED in Python 3.1 */
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
274
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
275 double
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
276 PyOS_ascii_strtod(const char *nptr, char **endptr)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
277 {
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
278 char *fail_pos;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
279 const char *p;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
280 double x;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
281
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
282 if (PyErr_WarnEx(PyExc_DeprecationWarning,
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
283 "PyOS_ascii_strtod and PyOS_ascii_atof are "
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
284 "deprecated. Use PyOS_string_to_double "
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
285 "instead.", 1) < 0)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
286 return -1.0;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
287
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
288 /* _PyOS_ascii_strtod already does everything that we want,
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
289 except that it doesn't parse leading whitespace */
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
290 p = nptr;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
291 while (Py_ISSPACE(*p))
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
292 p++;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
293 x = _PyOS_ascii_strtod(p, &fail_pos);
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
294 if (fail_pos == p)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
295 fail_pos = (char *)nptr;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
296 if (endptr)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
297 *endptr = (char *)fail_pos;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
298 return x;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
299 }
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
300
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
301 /* PyOS_ascii_strtod is DEPRECATED in Python 3.1 */
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
302
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
303 double
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
304 PyOS_ascii_atof(const char *nptr)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
305 {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
306 return PyOS_ascii_strtod(nptr, NULL);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
307 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
308
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
309 /* PyOS_string_to_double is the recommended replacement for the deprecated
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
310 PyOS_ascii_strtod and PyOS_ascii_atof functions. It converts a
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
311 null-terminated byte string s (interpreted as a string of ASCII characters)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
312 to a float. The string should not have leading or trailing whitespace (in
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
313 contrast, PyOS_ascii_strtod allows leading whitespace but not trailing
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
314 whitespace). The conversion is independent of the current locale.
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
315
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
316 If endptr is NULL, try to convert the whole string. Raise ValueError and
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
317 return -1.0 if the string is not a valid representation of a floating-point
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
318 number.
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
319
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
320 If endptr is non-NULL, try to convert as much of the string as possible.
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
321 If no initial segment of the string is the valid representation of a
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
322 floating-point number then *endptr is set to point to the beginning of the
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
323 string, -1.0 is returned and again ValueError is raised.
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
324
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
325 On overflow (e.g., when trying to convert '1e500' on an IEEE 754 machine),
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
326 if overflow_exception is NULL then +-Py_HUGE_VAL is returned, and no Python
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
327 exception is raised. Otherwise, overflow_exception should point to a
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
328 a Python exception, this exception will be raised, -1.0 will be returned,
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
329 and *endptr will point just past the end of the converted value.
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
330
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
331 If any other failure occurs (for example lack of memory), -1.0 is returned
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
332 and the appropriate Python exception will have been set.
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
333 */
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
334
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
335 double
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
336 PyOS_string_to_double(const char *s,
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
337 char **endptr,
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
338 PyObject *overflow_exception)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
339 {
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
340 double x, result=-1.0;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
341 char *fail_pos;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
342
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
343 errno = 0;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
344 PyFPE_START_PROTECT("PyOS_string_to_double", return -1.0)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
345 x = _PyOS_ascii_strtod(s, &fail_pos);
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
346 PyFPE_END_PROTECT(x)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
347
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
348 if (errno == ENOMEM) {
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
349 PyErr_NoMemory();
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
350 fail_pos = (char *)s;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
351 }
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
352 else if (!endptr && (fail_pos == s || *fail_pos != '\0'))
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
353 PyErr_Format(PyExc_ValueError,
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
354 "could not convert string to float: "
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
355 "%.200s", s);
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
356 else if (fail_pos == s)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
357 PyErr_Format(PyExc_ValueError,
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
358 "could not convert string to float: "
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
359 "%.200s", s);
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
360 else if (errno == ERANGE && fabs(x) >= 1.0 && overflow_exception)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
361 PyErr_Format(overflow_exception,
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
362 "value too large to convert to float: "
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
363 "%.200s", s);
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
364 else
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
365 result = x;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
366
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
367 if (endptr != NULL)
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
368 *endptr = fail_pos;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
369 return result;
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53640
diff changeset
370 }
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
371
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
372 /* Given a string that may have a decimal point in the current
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
373 locale, change it back to a dot. Since the string cannot get
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
374 longer, no need for a maximum buffer size parameter. */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
375 Py_LOCAL_INLINE(void)
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
376 change_decimal_from_locale_to_dot(char* buffer)
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
377 {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
378 struct lconv *locale_data = localeconv();
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
379 const char *decimal_point = locale_data->decimal_point;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
380
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
381 if (decimal_point[0] != '.' || decimal_point[1] != 0) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
382 size_t decimal_point_len = strlen(decimal_point);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
383
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
384 if (*buffer == '+' || *buffer == '-')
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
385 buffer++;
53543
4b4b59f278bf Merged revisions 72040 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53489
diff changeset
386 while (Py_ISDIGIT(*buffer))
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
387 buffer++;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
388 if (strncmp(buffer, decimal_point, decimal_point_len) == 0) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
389 *buffer = '.';
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
390 buffer++;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
391 if (decimal_point_len > 1) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
392 /* buffer needs to get smaller */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
393 size_t rest_len = strlen(buffer +
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
394 (decimal_point_len - 1));
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
395 memmove(buffer,
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
396 buffer + (decimal_point_len - 1),
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
397 rest_len);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
398 buffer[rest_len] = 0;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
399 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
400 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
401 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
402 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
403
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
404
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
405 /* From the C99 standard, section 7.19.6:
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
406 The exponent always contains at least two digits, and only as many more digits
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
407 as necessary to represent the exponent.
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
408 */
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
409 #define MIN_EXPONENT_DIGITS 2
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
410
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
411 /* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
412 in length. */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
413 Py_LOCAL_INLINE(void)
53479
785ea299fe23 Merged revisions 71976 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53468
diff changeset
414 ensure_minimum_exponent_length(char* buffer, size_t buf_size)
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
415 {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
416 char *p = strpbrk(buffer, "eE");
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
417 if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
418 char *start = p + 2;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
419 int exponent_digit_cnt = 0;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
420 int leading_zero_cnt = 0;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
421 int in_leading_zeros = 1;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
422 int significant_digit_cnt;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
423
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
424 /* Skip over the exponent and the sign. */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
425 p += 2;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
426
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
427 /* Find the end of the exponent, keeping track of leading
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
428 zeros. */
53543
4b4b59f278bf Merged revisions 72040 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53489
diff changeset
429 while (*p && Py_ISDIGIT(*p)) {
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
430 if (in_leading_zeros && *p == '0')
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
431 ++leading_zero_cnt;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
432 if (*p != '0')
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
433 in_leading_zeros = 0;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
434 ++p;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
435 ++exponent_digit_cnt;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
436 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
437
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
438 significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
439 if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
440 /* If there are 2 exactly digits, we're done,
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
441 regardless of what they contain */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
442 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
443 else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
444 int extra_zeros_cnt;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
445
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
446 /* There are more than 2 digits in the exponent. See
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
447 if we can delete some of the leading zeros */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
448 if (significant_digit_cnt < MIN_EXPONENT_DIGITS)
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
449 significant_digit_cnt = MIN_EXPONENT_DIGITS;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
450 extra_zeros_cnt = exponent_digit_cnt -
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
451 significant_digit_cnt;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
452
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
453 /* Delete extra_zeros_cnt worth of characters from the
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
454 front of the exponent */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
455 assert(extra_zeros_cnt >= 0);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
456
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
457 /* Add one to significant_digit_cnt to copy the
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
458 trailing 0 byte, thus setting the length */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
459 memmove(start,
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
460 start + extra_zeros_cnt,
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
461 significant_digit_cnt + 1);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
462 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
463 else {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
464 /* If there are fewer than 2 digits, add zeros
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
465 until there are 2, if there's enough room */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
466 int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
467 if (start + zeros + exponent_digit_cnt + 1
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
468 < buffer + buf_size) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
469 memmove(start + zeros, start,
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
470 exponent_digit_cnt + 1);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
471 memset(start, '0', zeros);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
472 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
473 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
474 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
475 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
476
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
477 /* Remove trailing zeros after the decimal point from a numeric string; also
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
478 remove the decimal point if all digits following it are zero. The numeric
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
479 string must end in '\0', and should not have any leading or trailing
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
480 whitespace. Assumes that the decimal point is '.'. */
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
481 Py_LOCAL_INLINE(void)
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
482 remove_trailing_zeros(char *buffer)
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
483 {
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
484 char *old_fraction_end, *new_fraction_end, *end, *p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
485
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
486 p = buffer;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
487 if (*p == '-' || *p == '+')
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
488 /* Skip leading sign, if present */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
489 ++p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
490 while (Py_ISDIGIT(*p))
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
491 ++p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
492
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
493 /* if there's no decimal point there's nothing to do */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
494 if (*p++ != '.')
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
495 return;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
496
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
497 /* scan any digits after the point */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
498 while (Py_ISDIGIT(*p))
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
499 ++p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
500 old_fraction_end = p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
501
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
502 /* scan up to ending '\0' */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
503 while (*p != '\0')
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
504 p++;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
505 /* +1 to make sure that we move the null byte as well */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
506 end = p+1;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
507
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
508 /* scan back from fraction_end, looking for removable zeros */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
509 p = old_fraction_end;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
510 while (*(p-1) == '0')
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
511 --p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
512 /* and remove point if we've got that far */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
513 if (*(p-1) == '.')
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
514 --p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
515 new_fraction_end = p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
516
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
517 memmove(new_fraction_end, old_fraction_end, end-old_fraction_end);
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
518 }
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
519
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
520 /* Ensure that buffer has a decimal point in it. The decimal point will not
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
521 be in the current locale, it will always be '.'. Don't add a decimal point
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
522 if an exponent is present. Also, convert to exponential notation where
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
523 adding a '.0' would produce too many significant digits (see issue 5864).
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
524
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
525 Returns a pointer to the fixed buffer, or NULL on failure.
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
526 */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
527 Py_LOCAL_INLINE(char *)
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
528 ensure_decimal_point(char* buffer, size_t buf_size, int precision)
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
529 {
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
530 int digit_count, insert_count = 0, convert_to_exp = 0;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
531 char *chars_to_insert, *digits_start;
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
532
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
533 /* search for the first non-digit character */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
534 char *p = buffer;
48537
48e83e66935a Merged revisions 65125 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
535 if (*p == '-' || *p == '+')
48e83e66935a Merged revisions 65125 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
536 /* Skip leading sign, if present. I think this could only
48e83e66935a Merged revisions 65125 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
537 ever be '-', but it can't hurt to check for both. */
48e83e66935a Merged revisions 65125 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
538 ++p;
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
539 digits_start = p;
53543
4b4b59f278bf Merged revisions 72040 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53489
diff changeset
540 while (*p && Py_ISDIGIT(*p))
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
541 ++p;
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
542 digit_count = Py_SAFE_DOWNCAST(p - digits_start, Py_ssize_t, int);
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
543
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
544 if (*p == '.') {
53543
4b4b59f278bf Merged revisions 72040 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 53489
diff changeset
545 if (Py_ISDIGIT(*(p+1))) {
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
546 /* Nothing to do, we already have a decimal
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
547 point and a digit after it */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
548 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
549 else {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
550 /* We have a decimal point, but no following
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
551 digit. Insert a zero after the decimal. */
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
552 /* can't ever get here via PyOS_double_to_string */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
553 assert(precision == -1);
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
554 ++p;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
555 chars_to_insert = "0";
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
556 insert_count = 1;
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
557 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
558 }
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
559 else if (!(*p == 'e' || *p == 'E')) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
560 /* Don't add ".0" if we have an exponent. */
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
561 if (digit_count == precision) {
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
562 /* issue 5864: don't add a trailing .0 in the case
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
563 where the '%g'-formatted result already has as many
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
564 significant digits as were requested. Switch to
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
565 exponential notation instead. */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
566 convert_to_exp = 1;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
567 /* no exponent, no point, and we shouldn't land here
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
568 for infs and nans, so we must be at the end of the
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
569 string. */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
570 assert(*p == '\0');
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
571 }
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
572 else {
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
573 assert(precision == -1 || digit_count < precision);
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
574 chars_to_insert = ".0";
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
575 insert_count = 2;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
576 }
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
577 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
578 if (insert_count) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
579 size_t buf_len = strlen(buffer);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
580 if (buf_len + insert_count + 1 >= buf_size) {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
581 /* If there is not enough room in the buffer
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
582 for the additional text, just skip it. It's
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
583 not worth generating an error over. */
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
584 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
585 else {
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
586 memmove(p + insert_count, p,
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
587 buffer + strlen(buffer) - p + 1);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
588 memcpy(p, chars_to_insert, insert_count);
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
589 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
590 }
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
591 if (convert_to_exp) {
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
592 int written;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
593 size_t buf_avail;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
594 p = digits_start;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
595 /* insert decimal point */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
596 assert(digit_count >= 1);
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
597 memmove(p+2, p+1, digit_count); /* safe, but overwrites nul */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
598 p[1] = '.';
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
599 p += digit_count+1;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
600 assert(p <= buf_size+buffer);
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
601 buf_avail = buf_size+buffer-p;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
602 if (buf_avail == 0)
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
603 return NULL;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
604 /* Add exponent. It's okay to use lower case 'e': we only
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
605 arrive here as a result of using the empty format code or
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
606 repr/str builtins and those never want an upper case 'E' */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
607 written = PyOS_snprintf(p, buf_avail, "e%+.02d", digit_count-1);
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
608 if (!(0 <= written &&
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
609 written < Py_SAFE_DOWNCAST(buf_avail, size_t, int)))
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
610 /* output truncated, or something else bad happened */
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
611 return NULL;
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
612 remove_trailing_zeros(buffer);
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
613 }
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
614 return buffer;
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
615 }
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
616
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
617 /* see FORMATBUFLEN in unicodeobject.c */
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
618 #define FLOAT_FORMATBUFLEN 120
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
619
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
620 /**
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
621 * PyOS_ascii_formatd:
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
622 * @buffer: A buffer to place the resulting string in
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
623 * @buf_size: The length of the buffer.
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
624 * @format: The printf()-style format to use for the
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
625 * code to use for converting.
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
626 * @d: The #gdouble to convert
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
627 *
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
628 * Converts a #gdouble to a string, using the '.' as
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
629 * decimal point. To format the number you pass in
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
630 * a printf()-style format string. Allowed conversion
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
631 * specifiers are 'e', 'E', 'f', 'F', 'g', 'G', and 'Z'.
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
632 *
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
633 * 'Z' is the same as 'g', except it always has a decimal and
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
634 * at least one digit after the decimal.
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
635 *
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
636 * Return value: The pointer to the buffer with the converted string.
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
637 * On failure returns NULL but does not set any Python exception.
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
638 **/
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
639 char *
53489
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
640 _PyOS_ascii_formatd(char *buffer,
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
641 size_t buf_size,
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
642 const char *format,
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
643 double d,
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
644 int precision)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
645 {
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
646 char format_char;
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
647 size_t format_len = strlen(format);
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
648
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
649 /* Issue 2264: code 'Z' requires copying the format. 'Z' is 'g', but
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
650 also with at least one character past the decimal. */
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
651 char tmp_format[FLOAT_FORMATBUFLEN];
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
652
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
653 /* The last character in the format string must be the format char */
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
654 format_char = format[format_len - 1];
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
655
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
656 if (format[0] != '%')
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
657 return NULL;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
658
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
659 /* I'm not sure why this test is here. It's ensuring that the format
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
660 string after the first character doesn't have a single quote, a
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
661 lowercase l, or a percent. This is the reverse of the commented-out
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
662 test about 10 lines ago. */
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
663 if (strpbrk(format + 1, "'l%"))
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
664 return NULL;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
665
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
666 /* Also curious about this function is that it accepts format strings
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
667 like "%xg", which are invalid for floats. In general, the
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
668 interface to this function is not very good, but changing it is
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
669 difficult because it's a public API. */
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
670
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
671 if (!(format_char == 'e' || format_char == 'E' ||
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
672 format_char == 'f' || format_char == 'F' ||
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
673 format_char == 'g' || format_char == 'G' ||
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
674 format_char == 'Z'))
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
675 return NULL;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
676
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
677 /* Map 'Z' format_char to 'g', by copying the format string and
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
678 replacing the final char with a 'g' */
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
679 if (format_char == 'Z') {
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
680 if (format_len + 1 >= sizeof(tmp_format)) {
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
681 /* The format won't fit in our copy. Error out. In
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
682 practice, this will never happen and will be
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
683 detected by returning NULL */
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
684 return NULL;
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
685 }
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
686 strcpy(tmp_format, format);
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
687 tmp_format[format_len - 1] = 'g';
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
688 format = tmp_format;
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
689 }
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
690
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
691
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
692 /* Have PyOS_snprintf do the hard work */
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
693 PyOS_snprintf(buffer, buf_size, format, d);
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
694
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
695 /* Do various fixups on the return string */
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
696
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
697 /* Get the current locale, and find the decimal point string.
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
698 Convert that string back to a dot. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
699 change_decimal_from_locale_to_dot(buffer);
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
700
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
701 /* If an exponent exists, ensure that the exponent is at least
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
702 MIN_EXPONENT_DIGITS digits, providing the buffer is large enough
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
703 for the extra zeros. Also, if there are more than
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
704 MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
705 back to MIN_EXPONENT_DIGITS */
53479
785ea299fe23 Merged revisions 71976 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53468
diff changeset
706 ensure_minimum_exponent_length(buffer, buf_size);
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
707
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
708 /* If format_char is 'Z', make sure we have at least one character
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
709 after the decimal point (and make sure we have a decimal point);
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
710 also switch to exponential notation in some edge cases where the
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
711 extra character would produce more significant digits that we
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
712 really want. */
46601
7b4081bec783 Merged revisions 62586 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 45852
diff changeset
713 if (format_char == 'Z')
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
714 buffer = ensure_decimal_point(buffer, buf_size, precision);
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
715
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
716 return buffer;
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
717 }
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
718
53489
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
719 char *
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
720 PyOS_ascii_formatd(char *buffer,
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
721 size_t buf_size,
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
722 const char *format,
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
723 double d)
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
724 {
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
725 if (PyErr_WarnEx(PyExc_DeprecationWarning,
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
726 "PyOS_ascii_formatd is deprecated, "
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
727 "use PyOS_double_to_string instead", 1) < 0)
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
728 return NULL;
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
729
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
730 return _PyOS_ascii_formatd(buffer, buf_size, format, d, -1);
53489
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
731 }
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
732
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
733 #ifdef PY_NO_SHORT_FLOAT_REPR
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
734
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
735 /* The fallback code to use if _Py_dg_dtoa is not available. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
736
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
737 PyAPI_FUNC(char *) PyOS_double_to_string(double val,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
738 char format_code,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
739 int precision,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
740 int flags,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
741 int *type)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
742 {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
743 char format[32];
53640
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
744 Py_ssize_t bufsize;
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
745 char *buf;
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
746 int t, exp;
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
747 int upper = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
748
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
749 /* Validate format_code, and map upper and lower case */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
750 switch (format_code) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
751 case 'e': /* exponent */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
752 case 'f': /* fixed */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
753 case 'g': /* general */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
754 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
755 case 'E':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
756 upper = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
757 format_code = 'e';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
758 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
759 case 'F':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
760 upper = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
761 format_code = 'f';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
762 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
763 case 'G':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
764 upper = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
765 format_code = 'g';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
766 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
767 case 'r': /* repr format */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
768 /* Supplied precision is unused, must be 0. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
769 if (precision != 0) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
770 PyErr_BadInternalCall();
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
771 return NULL;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
772 }
53787
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
773 /* The repr() precision (17 significant decimal digits) is the
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
774 minimal number that is guaranteed to have enough precision
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
775 so that if the number is read back in the exact same binary
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
776 value is recreated. This is true for IEEE floating point
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
777 by design, and also happens to work for all other modern
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
778 hardware. */
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
779 precision = 17;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
780 format_code = 'g';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
781 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
782 default:
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
783 PyErr_BadInternalCall();
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
784 return NULL;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
785 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
786
53640
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
787 /* Here's a quick-and-dirty calculation to figure out how big a buffer
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
788 we need. In general, for a finite float we need:
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
789
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
790 1 byte for each digit of the decimal significand, and
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
791
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
792 1 for a possible sign
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
793 1 for a possible decimal point
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
794 2 for a possible [eE][+-]
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
795 1 for each digit of the exponent; if we allow 19 digits
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
796 total then we're safe up to exponents of 2**63.
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
797 1 for the trailing nul byte
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
798
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
799 This gives a total of 24 + the number of digits in the significand,
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
800 and the number of digits in the significand is:
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
801
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
802 for 'g' format: at most precision, except possibly
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
803 when precision == 0, when it's 1.
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
804 for 'e' format: precision+1
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
805 for 'f' format: precision digits after the point, at least 1
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
806 before. To figure out how many digits appear before the point
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
807 we have to examine the size of the number. If fabs(val) < 1.0
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
808 then there will be only one digit before the point. If
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
809 fabs(val) >= 1.0, then there are at most
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
810
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
811 1+floor(log10(ceiling(fabs(val))))
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
812
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
813 digits before the point (where the 'ceiling' allows for the
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
814 possibility that the rounding rounds the integer part of val
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
815 up). A safe upper bound for the above quantity is
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
816 1+floor(exp/3), where exp is the unique integer such that 0.5
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
817 <= fabs(val)/2**exp < 1.0. This exp can be obtained from
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
818 frexp.
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
819
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
820 So we allow room for precision+1 digits for all formats, plus an
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
821 extra floor(exp/3) digits for 'f' format.
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
822
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
823 */
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
824
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
825 if (Py_IS_NAN(val) || Py_IS_INFINITY(val))
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
826 /* 3 for 'inf'/'nan', 1 for sign, 1 for '\0' */
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
827 bufsize = 5;
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
828 else {
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
829 bufsize = 25 + precision;
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
830 if (format_code == 'f' && fabs(val) >= 1.0) {
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
831 frexp(val, &exp);
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
832 bufsize += exp/3;
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
833 }
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
834 }
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
835
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
836 buf = PyMem_Malloc(bufsize);
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
837 if (buf == NULL) {
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
838 PyErr_NoMemory();
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
839 return NULL;
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
840 }
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
841
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
842 /* Handle nan and inf. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
843 if (Py_IS_NAN(val)) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
844 strcpy(buf, "nan");
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
845 t = Py_DTST_NAN;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
846 } else if (Py_IS_INFINITY(val)) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
847 if (copysign(1., val) == 1.)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
848 strcpy(buf, "inf");
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
849 else
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
850 strcpy(buf, "-inf");
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
851 t = Py_DTST_INFINITE;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
852 } else {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
853 t = Py_DTST_FINITE;
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
854 if (flags & Py_DTSF_ADD_DOT_0)
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
855 format_code = 'Z';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
856
53489
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
857 PyOS_snprintf(format, sizeof(format), "%%%s.%i%c",
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
858 (flags & Py_DTSF_ALT ? "#" : ""), precision,
de1d0f71e209 Issue #5835: Deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 53479
diff changeset
859 format_code);
53640
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
860 _PyOS_ascii_formatd(buf, bufsize, format, val, precision);
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
861 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
862
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
863 /* Add sign when requested. It's convenient (esp. when formatting
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
864 complex numbers) to include a sign even for inf and nan. */
53640
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
865 if (flags & Py_DTSF_SIGN && buf[0] != '-') {
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
866 size_t len = strlen(buf);
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
867 /* the bufsize calculations above should ensure that we've got
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
868 space to add a sign */
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
869 assert((size_t)bufsize >= len+2);
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
870 memmove(buf+1, buf, len+1);
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
871 buf[0] = '+';
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
872 }
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
873 if (upper) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
874 /* Convert to upper case. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
875 char *p1;
53640
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
876 for (p1 = buf; *p1; p1++)
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
877 *p1 = Py_TOUPPER(*p1);
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
878 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
879
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
880 if (type)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
881 *type = t;
53640
e08e80e768c1 Issue #5859: Remove use of fixed-length buffers for float formatting
Mark Dickinson <dickinsm@gmail.com>
parents: 53603
diff changeset
882 return buf;
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
883 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
884
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
885 #else
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
886
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
887 /* _Py_dg_dtoa is available. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
888
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
889 /* I'm using a lookup table here so that I don't have to invent a non-locale
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
890 specific way to convert to uppercase */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
891 #define OFS_INF 0
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
892 #define OFS_NAN 1
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
893 #define OFS_E 2
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
894
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
895 /* The lengths of these are known to the code below, so don't change them */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
896 static char *lc_float_strings[] = {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
897 "inf",
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
898 "nan",
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
899 "e",
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
900 };
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
901 static char *uc_float_strings[] = {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
902 "INF",
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
903 "NAN",
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
904 "E",
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
905 };
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
906
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
907
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
908 /* Convert a double d to a string, and return a PyMem_Malloc'd block of
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
909 memory contain the resulting string.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
910
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
911 Arguments:
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
912 d is the double to be converted
53787
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
913 format_code is one of 'e', 'f', 'g', 'r'. 'e', 'f' and 'g'
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
914 correspond to '%e', '%f' and '%g'; 'r' corresponds to repr.
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
915 mode is one of '0', '2' or '3', and is completely determined by
53787
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
916 format_code: 'e' and 'g' use mode 2; 'f' mode 3, 'r' mode 0.
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
917 precision is the desired precision
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
918 always_add_sign is nonzero if a '+' sign should be included for positive
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
919 numbers
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
920 add_dot_0_if_integer is nonzero if integers in non-exponential form
53787
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
921 should have ".0" added. Only applies to format codes 'r' and 'g'.
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
922 use_alt_formatting is nonzero if alternative formatting should be
53787
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
923 used. Only applies to format codes 'e', 'f' and 'g'. For code 'g',
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
924 at most one of use_alt_formatting and add_dot_0_if_integer should
c6376d3fa968 Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53718
diff changeset
925 be nonzero.
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
926 type, if non-NULL, will be set to one of these constants to identify
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
927 the type of the 'd' argument:
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
928 Py_DTST_FINITE
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
929 Py_DTST_INFINITE
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
930 Py_DTST_NAN
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
931
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
932 Returns a PyMem_Malloc'd block of memory containing the resulting string,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
933 or NULL on error. If NULL is returned, the Python error has been set.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
934 */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
935
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
936 static char *
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
937 format_float_short(double d, char format_code,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
938 int mode, Py_ssize_t precision,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
939 int always_add_sign, int add_dot_0_if_integer,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
940 int use_alt_formatting, char **float_strings, int *type)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
941 {
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
942 char *buf = NULL;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
943 char *p = NULL;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
944 Py_ssize_t bufsize = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
945 char *digits, *digits_end;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
946 int decpt_as_int, sign, exp_len, exp = 0, use_exp = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
947 Py_ssize_t decpt, digits_len, vdigits_start, vdigits_end;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
948 _Py_SET_53BIT_PRECISION_HEADER;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
949
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
950 /* _Py_dg_dtoa returns a digit string (no decimal point or exponent).
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
951 Must be matched by a call to _Py_dg_freedtoa. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
952 _Py_SET_53BIT_PRECISION_START;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
953 digits = _Py_dg_dtoa(d, mode, precision, &decpt_as_int, &sign,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
954 &digits_end);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
955 _Py_SET_53BIT_PRECISION_END;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
956
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
957 decpt = (Py_ssize_t)decpt_as_int;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
958 if (digits == NULL) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
959 /* The only failure mode is no memory. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
960 PyErr_NoMemory();
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
961 goto exit;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
962 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
963 assert(digits_end != NULL && digits_end >= digits);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
964 digits_len = digits_end - digits;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
965
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
966 if (digits_len && !Py_ISDIGIT(digits[0])) {
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
967 /* Infinities and nans here; adapt Gay's output,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
968 so convert Infinity to inf and NaN to nan, and
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
969 ignore sign of nan. Then return. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
970
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
971 /* ignore the actual sign of a nan */
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
972 if (digits[0] == 'n' || digits[0] == 'N')
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
973 sign = 0;
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
974
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
975 /* We only need 5 bytes to hold the result "+inf\0" . */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
976 bufsize = 5; /* Used later in an assert. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
977 buf = (char *)PyMem_Malloc(bufsize);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
978 if (buf == NULL) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
979 PyErr_NoMemory();
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
980 goto exit;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
981 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
982 p = buf;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
983
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
984 if (sign == 1) {
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
985 *p++ = '-';
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
986 }
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
987 else if (always_add_sign) {
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
988 *p++ = '+';
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 53210
diff changeset
989 }
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
990 if (digits[0] == 'i' || digits[0] == 'I') {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
991 strncpy(p, float_strings[OFS_INF], 3);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
992 p += 3;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
993
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
994 if (type)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
995 *type = Py_DTST_INFINITE;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
996 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
997 else if (digits[0] == 'n' || digits[0] == 'N') {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
998 strncpy(p, float_strings[OFS_NAN], 3);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
999 p += 3;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1000
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1001 if (type)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1002 *type = Py_DTST_NAN;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1003 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1004 else {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1005 /* shouldn't get here: Gay's code should always return
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1006 something starting with a digit, an 'I', or 'N' */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1007 strncpy(p, "ERR", 3);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1008 p += 3;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1009 assert(0);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1010 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1011 goto exit;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1012 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1013
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1014 /* The result must be finite (not inf or nan). */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1015 if (type)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1016 *type = Py_DTST_FINITE;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1017
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1018
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1019 /* We got digits back, format them. We may need to pad 'digits'
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1020 either on the left or right (or both) with extra zeros, so in
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1021 general the resulting string has the form
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1022
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1023 [<sign>]<zeros><digits><zeros>[<exponent>]
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1024
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1025 where either of the <zeros> pieces could be empty, and there's a
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1026 decimal point that could appear either in <digits> or in the
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1027 leading or trailing <zeros>.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1028
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1029 Imagine an infinite 'virtual' string vdigits, consisting of the
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1030 string 'digits' (starting at index 0) padded on both the left and
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1031 right with infinite strings of zeros. We want to output a slice
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1032
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1033 vdigits[vdigits_start : vdigits_end]
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1034
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1035 of this virtual string. Thus if vdigits_start < 0 then we'll end
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1036 up producing some leading zeros; if vdigits_end > digits_len there
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1037 will be trailing zeros in the output. The next section of code
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1038 determines whether to use an exponent or not, figures out the
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1039 position 'decpt' of the decimal point, and computes 'vdigits_start'
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1040 and 'vdigits_end'. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1041 vdigits_end = digits_len;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1042 switch (format_code) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1043 case 'e':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1044 use_exp = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1045 vdigits_end = precision;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1046 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1047 case 'f':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1048 vdigits_end = decpt + precision;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1049 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1050 case 'g':
53603
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
1051 if (decpt <= -4 || decpt >
3759839b9fa5 Issue #5864: Fix problem with empty code formatting for floats,
Mark Dickinson <dickinsm@gmail.com>
parents: 53543
diff changeset
1052 (add_dot_0_if_integer ? precision-1 : precision))
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1053 use_exp = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1054 if (use_alt_formatting)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1055 vdigits_end = precision;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1056 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1057 case 'r':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1058 /* convert to exponential format at 1e16. We used to convert
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1059 at 1e17, but that gives odd-looking results for some values
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1060 when a 16-digit 'shortest' repr is padded with bogus zeros.
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1061 For example, repr(2e16+8) would give 20000000000000010.0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1062 the true value is 20000000000000008.0. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1063 if (decpt <= -4 || decpt > 16)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1064 use_exp = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1065 break;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1066 default:
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1067 PyErr_BadInternalCall();
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1068 goto exit;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1069 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1070
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1071 /* if using an exponent, reset decimal point position to 1 and adjust
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1072 exponent accordingly.*/
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1073 if (use_exp) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1074 exp = decpt - 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1075 decpt = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1076 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1077 /* ensure vdigits_start < decpt <= vdigits_end, or vdigits_start <
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1078 decpt < vdigits_end if add_dot_0_if_integer and no exponent */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1079 vdigits_start = decpt <= 0 ? decpt-1 : 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1080 if (!use_exp && add_dot_0_if_integer)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1081 vdigits_end = vdigits_end > decpt ? vdigits_end : decpt + 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1082 else
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1083 vdigits_end = vdigits_end > decpt ? vdigits_end : decpt;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1084
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1085 /* double check inequalities */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1086 assert(vdigits_start <= 0 &&
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1087 0 <= digits_len &&
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1088 digits_len <= vdigits_end);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1089 /* decimal point should be in (vdigits_start, vdigits_end] */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1090 assert(vdigits_start < decpt && decpt <= vdigits_end);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1091
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1092 /* Compute an upper bound how much memory we need. This might be a few
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1093 chars too long, but no big deal. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1094 bufsize =
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1095 /* sign, decimal point and trailing 0 byte */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1096 3 +
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1097
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1098 /* total digit count (including zero padding on both sides) */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1099 (vdigits_end - vdigits_start) +
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1100
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1101 /* exponent "e+100", max 3 numerical digits */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1102 (use_exp ? 5 : 0);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1103
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1104 /* Now allocate the memory and initialize p to point to the start of
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1105 it. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1106 buf = (char *)PyMem_Malloc(bufsize);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1107 if (buf == NULL) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1108 PyErr_NoMemory();
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1109 goto exit;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1110 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1111 p = buf;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1112
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1113 /* Add a negative sign if negative, and a plus sign if non-negative
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1114 and always_add_sign is true. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1115 if (sign == 1)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1116 *p++ = '-';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1117 else if (always_add_sign)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1118 *p++ = '+';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1119
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1120 /* note that exactly one of the three 'if' conditions is true,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1121 so we include exactly one decimal point */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1122 /* Zero padding on left of digit string */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1123 if (decpt <= 0) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1124 memset(p, '0', decpt-vdigits_start);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1125 p += decpt - vdigits_start;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1126 *p++ = '.';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1127 memset(p, '0', 0-decpt);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1128 p += 0-decpt;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1129 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1130 else {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1131 memset(p, '0', 0-vdigits_start);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1132 p += 0 - vdigits_start;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1133 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1134
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1135 /* Digits, with included decimal point */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1136 if (0 < decpt && decpt <= digits_len) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1137 strncpy(p, digits, decpt-0);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1138 p += decpt-0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1139 *p++ = '.';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1140 strncpy(p, digits+decpt, digits_len-decpt);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1141 p += digits_len-decpt;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1142 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1143 else {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1144 strncpy(p, digits, digits_len);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1145 p += digits_len;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1146 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1147
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1148 /* And zeros on the right */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1149 if (digits_len < decpt) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1150 memset(p, '0', decpt-digits_len);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1151 p += decpt-digits_len;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1152 *p++ = '.';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1153 memset(p, '0', vdigits_end-decpt);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1154 p += vdigits_end-decpt;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1155 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1156 else {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1157 memset(p, '0', vdigits_end-digits_len);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1158 p += vdigits_end-digits_len;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1159 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1160
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1161 /* Delete a trailing decimal pt unless using alternative formatting. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1162 if (p[-1] == '.' && !use_alt_formatting)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1163 p--;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1164
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1165 /* Now that we've done zero padding, add an exponent if needed. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1166 if (use_exp) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1167 *p++ = float_strings[OFS_E][0];
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1168 exp_len = sprintf(p, "%+.02d", exp);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1169 p += exp_len;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1170 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1171 exit:
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1172 if (buf) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1173 *p = '\0';
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1174 /* It's too late if this fails, as we've already stepped on
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1175 memory that isn't ours. But it's an okay debugging test. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1176 assert(p-buf < bufsize);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1177 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1178 if (digits)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1179 _Py_dg_freedtoa(digits);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1180
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1181 return buf;
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
1182 }
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1183
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1184
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1185 PyAPI_FUNC(char *) PyOS_double_to_string(double val,
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1186 char format_code,
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1187 int precision,
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1188 int flags,
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1189 int *type)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1190 {
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1191 char **float_strings = lc_float_strings;
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1192 int mode;
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1193
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1194 /* Validate format_code, and map upper and lower case. Compute the
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1195 mode and make any adjustments as needed. */
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1196 switch (format_code) {
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1197 /* exponent */
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1198 case 'E':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1199 float_strings = uc_float_strings;
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1200 format_code = 'e';
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1201 /* Fall through. */
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1202 case 'e':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1203 mode = 2;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1204 precision++;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1205 break;
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1206
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1207 /* fixed */
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1208 case 'F':
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1209 float_strings = uc_float_strings;
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1210 format_code = 'f';
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1211 /* Fall through. */
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1212 case 'f':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1213 mode = 3;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1214 break;
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1215
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1216 /* general */
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1217 case 'G':
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1218 float_strings = uc_float_strings;
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1219 format_code = 'g';
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1220 /* Fall through. */
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1221 case 'g':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1222 mode = 2;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1223 /* precision 0 makes no sense for 'g' format; interpret as 1 */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1224 if (precision == 0)
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1225 precision = 1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1226 break;
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1227
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1228 /* repr format */
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1229 case 'r':
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1230 mode = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1231 /* Supplied precision is unused, must be 0. */
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1232 if (precision != 0) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1233 PyErr_BadInternalCall();
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1234 return NULL;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1235 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1236 break;
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1237
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1238 default:
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1239 PyErr_BadInternalCall();
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1240 return NULL;
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1241 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1242
53190
d84ad973cde2 Simplify PyOS_double_to_string.
Eric Smith <eric@trueblade.com>
parents: 53187
diff changeset
1243 return format_float_short(val, format_code, mode, precision,
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1244 flags & Py_DTSF_SIGN,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1245 flags & Py_DTSF_ADD_DOT_0,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1246 flags & Py_DTSF_ALT,
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1247 float_strings, type);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1248 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52773
diff changeset
1249 #endif /* ifdef PY_NO_SHORT_FLOAT_REPR */