Issue #10833: Use PyErr_Format() and PyUnicode_FromFormat() instead of · pythoncapi/cpython@6ced7c4 · GitHub
Skip to content

Commit 6ced7c4

Browse files
author
Victor Stinner
committed
Issue python#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.
1 parent 44afe2b commit 6ced7c4

6 files changed

Lines changed: 36 additions & 65 deletions

File tree

Modules/_datetimemodule.c

Lines changed: 3 additions & 6 deletions

Modules/_testcapimodule.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,7 @@ static PyObject *TestError; /* set to exception object in init */
2222
static PyObject *
2323
raiseTestError(const char* test_name, const char* msg)
2424
{
25-
char buf[2048];
26-
27-
if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
28-
PyErr_SetString(TestError, "internal error msg too large");
29-
else {
30-
PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
31-
PyErr_SetString(TestError, buf);
32-
}
25+
PyErr_Format(TestError, "%s: %s", test_name, msg);
3326
return NULL;
3427
}
3528

Modules/_tkinter.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,11 +2400,9 @@ static PyObject *
24002400
Tktt_Repr(PyObject *self)
24012401
{
24022402
TkttObject *v = (TkttObject *)self;
2403-
char buf[100];
2404-
2405-
PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v,
2406-
v->func == NULL ? ", handler deleted" : "");
2407-
return PyUnicode_FromString(buf);
2403+
return PyUnicode_FromFormat("<tktimertoken at %p%s>",
2404+
v,
2405+
v->func == NULL ? ", handler deleted" : "");
24082406
}
24092407

24102408
static PyTypeObject Tktt_Type =

Modules/readline.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,9 @@ set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
233233
Py_XDECREF(tmp);
234234
}
235235
else {
236-
PyOS_snprintf(buf, sizeof(buf),
237-
"set_%.50s(func): argument not callable",
238-
funcname);
239-
PyErr_SetString(PyExc_TypeError, buf);
236+
PyErr_Format(PyExc_TypeError,
237+
"set_%.50s(func): argument not callable",
238+
funcname);
240239
return NULL;
241240
}
242241
Py_RETURN_NONE;
@@ -890,7 +889,7 @@ setup_readline(void)
890889
#endif
891890

892891
#ifdef __APPLE__
893-
/* the libedit readline emulation resets key bindings etc
892+
/* the libedit readline emulation resets key bindings etc
894893
* when calling rl_initialize. So call it upfront
895894
*/
896895
if (using_libedit_emulation)
@@ -930,11 +929,11 @@ setup_readline(void)
930929
*/
931930
#ifdef __APPLE__
932931
if (using_libedit_emulation)
933-
rl_read_init_file(NULL);
932+
rl_read_init_file(NULL);
934933
else
935934
#endif /* __APPLE__ */
936935
rl_initialize();
937-
936+
938937
RESTORE_LOCALE(saved_locale)
939938
}
940939

Objects/complexobject.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,10 @@ complex_repr(PyComplexObject *v)
330330
int precision = 0;
331331
char format_code = 'r';
332332
PyObject *result = NULL;
333-
Py_ssize_t len;
334333

335334
/* If these are non-NULL, they'll need to be freed. */
336335
char *pre = NULL;
337336
char *im = NULL;
338-
char *buf = NULL;
339337

340338
/* These do not need to be freed. re is either an alias
341339
for pre or a pointer to a constant. lead and tail
@@ -374,20 +372,10 @@ complex_repr(PyComplexObject *v)
374372
lead = "(";
375373
tail = ")";
376374
}
377-
/* Alloc the final buffer. Add one for the "j" in the format string,
378-
and one for the trailing zero byte. */
379-
len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
380-
buf = PyMem_Malloc(len);
381-
if (!buf) {
382-
PyErr_NoMemory();
383-
goto done;
384-
}
385-
PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail);
386-
result = PyUnicode_FromString(buf);
375+
result = PyUnicode_FromFormat("%s%s%sj%s", lead, re, im, tail);
387376
done:
388377
PyMem_Free(im);
389378
PyMem_Free(pre);
390-
PyMem_Free(buf);
391379

392380
return result;
393381
}

Python/getargs.c

Lines changed: 22 additions & 26 deletions

0 commit comments

Comments
 (0)