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.
This commit is contained in:
Victor Stinner 2011-03-21 18:15:42 +01:00
parent 44afe2b35a
commit 6ced7c4333
6 changed files with 36 additions and 65 deletions

View file

@ -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;
}