summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-08-04 20:56:28 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-08-04 20:56:28 (GMT)
commit388122d43b2b6bb41774d9680b9ad3bc05682f85 (patch)
tree4b3059b3dbd916fde44702e732f507788c433fbf /Objects
parentb6c50749207688902a6378958da474f7c31f179d (diff)
downloadcpython-388122d43b2b6bb41774d9680b9ad3bc05682f85.zip
cpython-388122d43b2b6bb41774d9680b9ad3bc05682f85.tar.gz
cpython-388122d43b2b6bb41774d9680b9ad3bc05682f85.tar.bz2
Issue #9337: Make float.__str__ identical to float.__repr__.
(And similarly for complex numbers.)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/complexobject.c8
-rw-r--r--Objects/floatobject.c22
-rw-r--r--Objects/stringlib/formatter.h17
3 files changed, 17 insertions, 30 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 7594c88..674362f 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -394,12 +394,6 @@ complex_repr(PyComplexObject *v)
return complex_format(v, 0, 'r');
}
-static PyObject *
-complex_str(PyComplexObject *v)
-{
- return complex_format(v, PyFloat_STR_PRECISION, 'g');
-}
-
static long
complex_hash(PyComplexObject *v)
{
@@ -1104,7 +1098,7 @@ PyTypeObject PyComplex_Type = {
0, /* tp_as_mapping */
(hashfunc)complex_hash, /* tp_hash */
0, /* tp_call */
- (reprfunc)complex_str, /* tp_str */
+ (reprfunc)complex_repr, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index c757203..b792c19 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -305,32 +305,20 @@ convert_to_double(PyObject **v, double *dbl)
}
static PyObject *
-float_str_or_repr(PyFloatObject *v, int precision, char format_code)
+float_repr(PyFloatObject *v)
{
PyObject *result;
char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
- format_code, precision,
+ 'r', 0,
Py_DTSF_ADD_DOT_0,
NULL);
if (!buf)
- return PyErr_NoMemory();
+ return PyErr_NoMemory();
result = PyUnicode_FromString(buf);
PyMem_Free(buf);
return result;
}
-static PyObject *
-float_repr(PyFloatObject *v)
-{
- return float_str_or_repr(v, 0, 'r');
-}
-
-static PyObject *
-float_str(PyFloatObject *v)
-{
- return float_str_or_repr(v, PyFloat_STR_PRECISION, 'g');
-}
-
/* Comparison is pretty much a nightmare. When comparing float to float,
* we do it as straightforwardly (and long-windedly) as conceivable, so
* that, e.g., Python x == y delivers the same result as the platform
@@ -1169,7 +1157,7 @@ float_hex(PyObject *v)
CONVERT_TO_DOUBLE(v, x);
if (Py_IS_NAN(x) || Py_IS_INFINITY(x))
- return float_str((PyFloatObject *)v);
+ return float_repr((PyFloatObject *)v);
if (x == 0.0) {
if (copysign(1.0, x) == -1.0)
@@ -1873,7 +1861,7 @@ PyTypeObject PyFloat_Type = {
0, /* tp_as_mapping */
(hashfunc)float_hash, /* tp_hash */
0, /* tp_call */
- (reprfunc)float_str, /* tp_str */
+ (reprfunc)float_repr, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h
index ab57a82..4fdab06 100644
--- a/Objects/stringlib/formatter.h
+++ b/Objects/stringlib/formatter.h
@@ -950,11 +950,12 @@ format_float_internal(PyObject *value,
}
if (type == '\0') {
- /* Omitted type specifier. This is like 'g' but with at least one
- digit after the decimal point, and different default precision.*/
- type = 'g';
- default_precision = PyFloat_STR_PRECISION;
+ /* Omitted type specifier. Behaves in the same way as repr(x)
+ and str(x) if no precision is given, else like 'g', but with
+ at least one digit after the decimal point. */
flags |= Py_DTSF_ADD_DOT_0;
+ type = 'r';
+ default_precision = 0;
}
if (type == 'n')
@@ -974,6 +975,8 @@ format_float_internal(PyObject *value,
if (precision < 0)
precision = default_precision;
+ else if (type == 'r')
+ type = 'g';
/* Cast "type", because if we're in unicode we need to pass a
8-bit char. This is safe, because we've restricted what "type"
@@ -1134,8 +1137,8 @@ format_complex_internal(PyObject *value,
if (type == '\0') {
/* Omitted type specifier. Should be like str(self). */
- type = 'g';
- default_precision = PyFloat_STR_PRECISION;
+ type = 'r';
+ default_precision = 0;
if (re == 0.0 && copysign(1.0, re) == 1.0)
skip_re = 1;
else
@@ -1149,6 +1152,8 @@ format_complex_internal(PyObject *value,
if (precision < 0)
precision = default_precision;
+ else if (type == 'r')
+ type = 'g';
/* Cast "type", because if we're in unicode we need to pass a
8-bit char. This is safe, because we've restricted what "type"