Skip to content

Commit 5abaa2b

Browse files
committed
Use PyObject_CallFunctionObjArgs()
Issue #28915: Replace PyObject_CallFunction() with PyObject_CallFunctionObjArgs() when the format string was only made of "O" formats, PyObject* arguments. PyObject_CallFunctionObjArgs() avoids the creation of a temporary tuple and doesn't have to parse a format string.
1 parent 55ba38a commit 5abaa2b

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

Modules/_elementtree.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,11 +2496,13 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
24962496
attrib = PyDict_New();
24972497
if (!attrib)
24982498
return NULL;
2499-
node = PyObject_CallFunction(self->element_factory, "OO", tag, attrib);
2499+
node = PyObject_CallFunctionObjArgs(self->element_factory,
2500+
tag, attrib, NULL);
25002501
Py_DECREF(attrib);
25012502
}
25022503
else {
2503-
node = PyObject_CallFunction(self->element_factory, "OO", tag, attrib);
2504+
node = PyObject_CallFunctionObjArgs(self->element_factory,
2505+
tag, attrib, NULL);
25042506
}
25052507
if (!node) {
25062508
return NULL;
@@ -2977,7 +2979,8 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
29772979
return;
29782980
}
29792981
}
2980-
res = PyObject_CallFunction(self->handle_start, "OO", tag, attrib);
2982+
res = PyObject_CallFunctionObjArgs(self->handle_start,
2983+
tag, attrib, NULL);
29812984
} else
29822985
res = NULL;
29832986

@@ -3143,8 +3146,9 @@ expat_start_doctype_handler(XMLParserObject *self,
31433146

31443147
/* If the target has a handler for doctype, call it. */
31453148
if (self->handle_doctype) {
3146-
res = PyObject_CallFunction(self->handle_doctype, "OOO",
3147-
doctype_name_obj, pubid_obj, sysid_obj);
3149+
res = PyObject_CallFunctionObjArgs(self->handle_doctype,
3150+
doctype_name_obj, pubid_obj,
3151+
sysid_obj, NULL);
31483152
Py_CLEAR(res);
31493153
}
31503154
else {
@@ -3163,8 +3167,9 @@ expat_start_doctype_handler(XMLParserObject *self,
31633167
if (!res)
31643168
goto clear;
31653169
Py_DECREF(res);
3166-
res = PyObject_CallFunction(parser_doctype, "OOO",
3167-
doctype_name_obj, pubid_obj, sysid_obj);
3170+
res = PyObject_CallFunctionObjArgs(parser_doctype,
3171+
doctype_name_obj, pubid_obj,
3172+
sysid_obj, NULL);
31683173
Py_CLEAR(res);
31693174
}
31703175
}
@@ -3191,7 +3196,8 @@ expat_pi_handler(XMLParserObject* self, const XML_Char* target_in,
31913196
target = PyUnicode_DecodeUTF8(target_in, strlen(target_in), "strict");
31923197
data = PyUnicode_DecodeUTF8(data_in, strlen(data_in), "strict");
31933198
if (target && data) {
3194-
res = PyObject_CallFunction(self->handle_pi, "OO", target, data);
3199+
res = PyObject_CallFunctionObjArgs(self->handle_pi,
3200+
target, data, NULL);
31953201
Py_XDECREF(res);
31963202
Py_DECREF(data);
31973203
Py_DECREF(target);

Objects/abstract.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2549,7 +2549,7 @@ _PyObject_CallFunctionVa(PyObject *callable, const char *format,
25492549
}
25502550

25512551
if (nargs == 1 && PyTuple_Check(stack[0])) {
2552-
/* Special cases:
2552+
/* Special cases for backward compatibility:
25532553
- PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
25542554
- PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
25552555
func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */

Objects/descrobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
14541454
doc = pold->prop_doc ? pold->prop_doc : Py_None;
14551455
}
14561456

1457-
new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
1457+
new = PyObject_CallFunctionObjArgs(type, get, set, del, doc, NULL);
14581458
Py_DECREF(type);
14591459
if (new == NULL)
14601460
return NULL;

0 commit comments

Comments
 (0)