summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
Diffstat (limited to 'Objects')
-rw-r--r--Objects/complexobject.c17
-rw-r--r--Objects/floatobject.c15
-rw-r--r--Objects/stringlib/formatter.h12
3 files changed, 28 insertions, 16 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index f0f2541..8bba241 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -354,7 +354,7 @@ complex_dealloc(PyObject *op)
static PyObject *
-complex_format(PyComplexObject *v, char format_code)
+complex_format(PyComplexObject *v, int precision, char format_code)
{
PyObject *result = NULL;
Py_ssize_t len;
@@ -374,7 +374,7 @@ complex_format(PyComplexObject *v, char format_code)
if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
re = "";
im = PyOS_double_to_string(v->cval.imag, format_code,
- 0, 0, NULL);
+ precision, 0, NULL);
if (!im) {
PyErr_NoMemory();
goto done;
@@ -382,7 +382,7 @@ complex_format(PyComplexObject *v, char format_code)
} else {
/* Format imaginary part with sign, real part without */
pre = PyOS_double_to_string(v->cval.real, format_code,
- 0, 0, NULL);
+ precision, 0, NULL);
if (!pre) {
PyErr_NoMemory();
goto done;
@@ -390,7 +390,7 @@ complex_format(PyComplexObject *v, char format_code)
re = pre;
im = PyOS_double_to_string(v->cval.imag, format_code,
- 0, Py_DTSF_SIGN, NULL);
+ precision, Py_DTSF_SIGN, NULL);
if (!im) {
PyErr_NoMemory();
goto done;
@@ -421,7 +421,10 @@ complex_print(PyComplexObject *v, FILE *fp, int flags)
{
PyObject *formatv;
char *buf;
- formatv = complex_format(v, (flags & Py_PRINT_RAW) ? 's' : 'r');
+ if (flags & Py_PRINT_RAW)
+ formatv = complex_format(v, PyFloat_STR_PRECISION, 'g');
+ else
+ formatv = complex_format(v, 0, 'r');
if (formatv == NULL)
return -1;
buf = PyString_AS_STRING(formatv);
@@ -435,13 +438,13 @@ complex_print(PyComplexObject *v, FILE *fp, int flags)
static PyObject *
complex_repr(PyComplexObject *v)
{
- return complex_format(v, 'r');
+ return complex_format(v, 0, 'r');
}
static PyObject *
complex_str(PyComplexObject *v)
{
- return complex_format(v, 's');
+ return complex_format(v, PyFloat_STR_PRECISION, 'g');
}
static long
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 9a0dbb3..6b9a310 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -352,7 +352,7 @@ convert_to_double(PyObject **v, double *dbl)
void
PyFloat_AsString(char *buf, PyFloatObject *v)
{
- _PyOS_double_to_string(buf, 100, v->ob_fval, 's', 0,
+ _PyOS_double_to_string(buf, 100, v->ob_fval, 'g', PyFloat_STR_PRECISION,
Py_DTSF_ADD_DOT_0, NULL);
}
@@ -368,9 +368,13 @@ static int
float_print(PyFloatObject *v, FILE *fp, int flags)
{
char buf[100];
- _PyOS_double_to_string(buf, sizeof(buf), v->ob_fval,
- (flags & Py_PRINT_RAW) ? 's' : 'r',
- 0, Py_DTSF_ADD_DOT_0, NULL);
+ if (flags & Py_PRINT_RAW)
+ _PyOS_double_to_string(buf, sizeof(buf), v->ob_fval,
+ 'g', PyFloat_STR_PRECISION,
+ Py_DTSF_ADD_DOT_0, NULL);
+ else
+ _PyOS_double_to_string(buf, sizeof(buf), v->ob_fval,
+ 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Py_BEGIN_ALLOW_THREADS
fputs(buf, fp);
Py_END_ALLOW_THREADS
@@ -390,7 +394,8 @@ static PyObject *
float_str(PyFloatObject *v)
{
char buf[100];
- _PyOS_double_to_string(buf, sizeof(buf), v->ob_fval, 's', 0,
+ _PyOS_double_to_string(buf, sizeof(buf), v->ob_fval, 'g',
+ PyFloat_STR_PRECISION,
Py_DTSF_ADD_DOT_0, NULL);
return PyString_FromString(buf);
}
diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h
index 1f3c535..3b22181 100644
--- a/Objects/stringlib/formatter.h
+++ b/Objects/stringlib/formatter.h
@@ -881,6 +881,7 @@ format_float_internal(PyObject *value,
int has_decimal;
double val;
Py_ssize_t precision = format->precision;
+ Py_ssize_t default_precision = 6;
STRINGLIB_CHAR type = format->type;
int add_pct = 0;
STRINGLIB_CHAR *p;
@@ -907,9 +908,10 @@ 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. */
+ /* 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;
flags |= Py_DTSF_ADD_DOT_0;
}
@@ -933,7 +935,7 @@ format_float_internal(PyObject *value,
}
if (precision < 0)
- precision = 6;
+ precision = default_precision;
#if PY_VERSION_HEX < 0x03010000
/* 3.1 no longer converts large 'f' to 'g'. */
@@ -1039,6 +1041,7 @@ format_complex_internal(PyObject *value,
int re_has_decimal;
int im_has_decimal;
Py_ssize_t precision = format->precision;
+ Py_ssize_t default_precision = 6;
STRINGLIB_CHAR type = format->type;
STRINGLIB_CHAR *p_re;
STRINGLIB_CHAR *p_im;
@@ -1100,6 +1103,7 @@ format_complex_internal(PyObject *value,
if (type == '\0') {
/* Omitted type specifier. Should be like str(self). */
type = 'g';
+ default_precision = PyFloat_STR_PRECISION;
add_parens = 1;
if (re == 0.0)
skip_re = 1;
@@ -1115,7 +1119,7 @@ format_complex_internal(PyObject *value,
type = 'f';
if (precision < 0)
- precision = 6;
+ precision = default_precision;
/* 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"