Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2990fa1

Browse files
committed
Issue #27809: Use _PyObject_FastCallDict()
Modify: * builtin_sorted() * classmethoddescr_call() * methoddescr_call() * wrapperdescr_call()
1 parent 6fea7f7 commit 2990fa1

2 files changed

Lines changed: 15 additions & 34 deletions

File tree

Objects/descrobject.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static PyObject *
213213
methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
214214
{
215215
Py_ssize_t argc;
216-
PyObject *self, *func, *result;
216+
PyObject *self, *func, *result, **stack;
217217

218218
/* Make sure that the first argument is acceptable as 'self' */
219219
assert(PyTuple_Check(args));
@@ -242,13 +242,8 @@ methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
242242
func = PyCFunction_NewEx(descr->d_method, self, NULL);
243243
if (func == NULL)
244244
return NULL;
245-
args = PyTuple_GetSlice(args, 1, argc);
246-
if (args == NULL) {
247-
Py_DECREF(func);
248-
return NULL;
249-
}
250-
result = PyEval_CallObjectWithKeywords(func, args, kwds);
251-
Py_DECREF(args);
245+
stack = &PyTuple_GET_ITEM(args, 1);
246+
result = _PyObject_FastCallDict(func, stack, argc - 1, kwds);
252247
Py_DECREF(func);
253248
return result;
254249
}
@@ -258,7 +253,7 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
258253
PyObject *kwds)
259254
{
260255
Py_ssize_t argc;
261-
PyObject *self, *func, *result;
256+
PyObject *self, *func, *result, **stack;
262257

263258
/* Make sure that the first argument is acceptable as 'self' */
264259
assert(PyTuple_Check(args));
@@ -295,22 +290,17 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
295290
func = PyCFunction_NewEx(descr->d_method, self, NULL);
296291
if (func == NULL)
297292
return NULL;
298-
args = PyTuple_GetSlice(args, 1, argc);
299-
if (args == NULL) {
300-
Py_DECREF(func);
301-
return NULL;
302-
}
303-
result = PyEval_CallObjectWithKeywords(func, args, kwds);
293+
stack = &PyTuple_GET_ITEM(args, 1);
294+
result = _PyObject_FastCallDict(func, stack, argc - 1, kwds);
304295
Py_DECREF(func);
305-
Py_DECREF(args);
306296
return result;
307297
}
308298

309299
static PyObject *
310300
wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
311301
{
312302
Py_ssize_t argc;
313-
PyObject *self, *func, *result;
303+
PyObject *self, *func, *result, **stack;
314304

315305
/* Make sure that the first argument is acceptable as 'self' */
316306
assert(PyTuple_Check(args));
@@ -339,13 +329,9 @@ wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
339329
func = PyWrapper_New((PyObject *)descr, self);
340330
if (func == NULL)
341331
return NULL;
342-
args = PyTuple_GetSlice(args, 1, argc);
343-
if (args == NULL) {
344-
Py_DECREF(func);
345-
return NULL;
346-
}
347-
result = PyEval_CallObjectWithKeywords(func, args, kwds);
348-
Py_DECREF(args);
332+
333+
stack = &PyTuple_GET_ITEM(args, 1);
334+
result = _PyObject_FastCallDict(func, stack, argc - 1, kwds);
349335
Py_DECREF(func);
350336
return result;
351337
}

Python/bltinmodule.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,10 +2087,11 @@ PyDoc_STRVAR(builtin_sorted__doc__,
20872087
static PyObject *
20882088
builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
20892089
{
2090-
PyObject *newlist, *v, *seq, *keyfunc=NULL, *newargs;
2090+
PyObject *newlist, *v, *seq, *keyfunc=NULL, **newargs;
20912091
PyObject *callable;
20922092
static char *kwlist[] = {"iterable", "key", "reverse", 0};
20932093
int reverse;
2094+
int nargs;
20942095

20952096
/* args 1-3 should match listsort in Objects/listobject.c */
20962097
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted",
@@ -2107,15 +2108,9 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
21072108
return NULL;
21082109
}
21092110

2110-
newargs = PyTuple_GetSlice(args, 1, 4);
2111-
if (newargs == NULL) {
2112-
Py_DECREF(newlist);
2113-
Py_DECREF(callable);
2114-
return NULL;
2115-
}
2116-
2117-
v = PyObject_Call(callable, newargs, kwds);
2118-
Py_DECREF(newargs);
2111+
newargs = &PyTuple_GET_ITEM(args, 1);
2112+
nargs = PyTuple_GET_SIZE(args) - 1;
2113+
v = _PyObject_FastCallDict(callable, newargs, nargs, kwds);
21192114
Py_DECREF(callable);
21202115
if (v == NULL) {
21212116
Py_DECREF(newlist);

0 commit comments

Comments
 (0)