summaryrefslogtreecommitdiff
path: root/Objects/complexobject.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-03-21 18:15:42 +0100
committerVictor Stinner <victor.stinner@haypocalc.com>2011-03-21 18:15:42 +0100
commit6ced7c433353208e5b9d4dc25018937f1c9ae87d (patch)
tree61efdd2c527ab2eeeff1dd306218b91efbe1b91d /Objects/complexobject.c
parent44afe2b35a3f82f26d35a2cec507ec6d59e6d6d3 (diff)
downloadcpython-git-6ced7c433353208e5b9d4dc25018937f1c9ae87d.tar.gz
Issue #10833: Use PyErr_Format() and PyUnicode_FromFormat() instead of
PyOS_snprintf() to avoid temporary buffer allocated on the stack and a conversion from bytes to Unicode.
Diffstat (limited to 'Objects/complexobject.c')
-rw-r--r--Objects/complexobject.c14
1 files changed, 1 insertions, 13 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index e247ba9ba8..3c6c32ae10 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -330,12 +330,10 @@ complex_repr(PyComplexObject *v)
int precision = 0;
char format_code = 'r';
PyObject *result = NULL;
- Py_ssize_t len;
/* If these are non-NULL, they'll need to be freed. */
char *pre = NULL;
char *im = NULL;
- char *buf = NULL;
/* These do not need to be freed. re is either an alias
for pre or a pointer to a constant. lead and tail
@@ -374,20 +372,10 @@ complex_repr(PyComplexObject *v)
lead = "(";
tail = ")";
}
- /* Alloc the final buffer. Add one for the "j" in the format string,
- and one for the trailing zero byte. */
- len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
- buf = PyMem_Malloc(len);
- if (!buf) {
- PyErr_NoMemory();
- goto done;
- }
- PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail);
- result = PyUnicode_FromString(buf);
+ result = PyUnicode_FromFormat("%s%s%sj%s", lead, re, im, tail);
done:
PyMem_Free(im);
PyMem_Free(pre);
- PyMem_Free(buf);
return result;
}