Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3332)

Side by Side Diff: Objects/floatobject.c

Issue 14744: Use _PyUnicodeWriter API in str.format() internals
Patch Set: Created 1 year ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Objects/complexobject.c ('k') | Objects/longobject.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Float object implementation */ 2 /* Float object implementation */
3 3
4 /* XXX There should be overflow checks here, but it's hard to check 4 /* XXX There should be overflow checks here, but it's hard to check
5 for any kind of float exception without losing portability. */ 5 for any kind of float exception without losing portability. */
6 6
7 #include "Python.h" 7 #include "Python.h"
8 8
9 #include <ctype.h> 9 #include <ctype.h>
10 #include <float.h> 10 #include <float.h>
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 static PyObject * 266 static PyObject *
267 float_repr(PyFloatObject *v) 267 float_repr(PyFloatObject *v)
268 { 268 {
269 PyObject *result; 269 PyObject *result;
270 char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v), 270 char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
271 'r', 0, 271 'r', 0,
272 Py_DTSF_ADD_DOT_0, 272 Py_DTSF_ADD_DOT_0,
273 NULL); 273 NULL);
274 if (!buf) 274 if (!buf)
275 return PyErr_NoMemory(); 275 return PyErr_NoMemory();
276 result = PyUnicode_FromString(buf); 276 result = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND,
277 buf, strlen(buf));
277 PyMem_Free(buf); 278 PyMem_Free(buf);
278 return result; 279 return result;
279 } 280 }
280 281
281 /* Comparison is pretty much a nightmare. When comparing float to float, 282 /* Comparison is pretty much a nightmare. When comparing float to float,
282 * we do it as straightforwardly (and long-windedly) as conceivable, so 283 * we do it as straightforwardly (and long-windedly) as conceivable, so
283 * that, e.g., Python x == y delivers the same result as the platform 284 * that, e.g., Python x == y delivers the same result as the platform
284 * C x == y when x and/or y is a NaN. 285 * C x == y when x and/or y is a NaN.
285 * When mixing float with an integer type, there's no good *uniform* approach. 286 * When mixing float with an integer type, there's no good *uniform* approach.
286 * Converting the double to an integer obviously doesn't work, since we 287 * Converting the double to an integer obviously doesn't work, since we
(...skipping 1409 matching lines...) Expand 10 before | Expand all | Expand 10 after
1696 static PyObject * 1697 static PyObject *
1697 float_getzero(PyObject *v, void *closure) 1698 float_getzero(PyObject *v, void *closure)
1698 { 1699 {
1699 return PyFloat_FromDouble(0.0); 1700 return PyFloat_FromDouble(0.0);
1700 } 1701 }
1701 1702
1702 static PyObject * 1703 static PyObject *
1703 float__format__(PyObject *self, PyObject *args) 1704 float__format__(PyObject *self, PyObject *args)
1704 { 1705 {
1705 PyObject *format_spec; 1706 PyObject *format_spec;
1707 _PyUnicodeWriter writer;
1708 int ret;
1706 1709
1707 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) 1710 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
1708 return NULL; 1711 return NULL;
1709 return _PyFloat_FormatAdvanced(self, format_spec, 0, 1712
1710 PyUnicode_GET_LENGTH(format_spec)); 1713 _PyUnicodeWriter_Init(&writer, 0);
1714 ret = _PyFloat_FormatAdvancedWriter(
1715 self,
1716 format_spec, 0, PyUnicode_GET_LENGTH(format_spec),
1717 &writer);
1718 if (ret == -1) {
1719 _PyUnicodeWriter_Dealloc(&writer);
1720 return NULL;
1721 }
1722 return _PyUnicodeWriter_Finish(&writer);
1711 } 1723 }
1712 1724
1713 PyDoc_STRVAR(float__format__doc, 1725 PyDoc_STRVAR(float__format__doc,
1714 "float.__format__(format_spec) -> string\n" 1726 "float.__format__(format_spec) -> string\n"
1715 "\n" 1727 "\n"
1716 "Formats the float according to format_spec."); 1728 "Formats the float according to format_spec.");
1717 1729
1718 1730
1719 static PyMethodDef float_methods[] = { 1731 static PyMethodDef float_methods[] = {
1720 {"conjugate", (PyCFunction)float_float, METH_NOARGS, 1732 {"conjugate", (PyCFunction)float_float, METH_NOARGS,
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after
2322 } 2334 }
2323 memcpy(&x, buf, 8); 2335 memcpy(&x, buf, 8);
2324 } 2336 }
2325 else { 2337 else {
2326 memcpy(&x, p, 8); 2338 memcpy(&x, p, 8);
2327 } 2339 }
2328 2340
2329 return x; 2341 return x;
2330 } 2342 }
2331 } 2343 }
OLDNEW
« no previous file with comments | « Objects/complexobject.c ('k') | Objects/longobject.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7