Skip to content

Commit 4776b07

Browse files
authored
Don't make a call at the C level when calling bound-methods from Python code. (GH-29238)
1 parent bcee6aa commit 4776b07

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Python/ceval.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -4607,8 +4607,21 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
46074607
kwnames = NULL;
46084608
postcall_shrink = 1;
46094609
call_function:
4610-
// Check if the call can be inlined or not
46114610
function = PEEK(oparg + 1);
4611+
if (Py_TYPE(function) == &PyMethod_Type) {
4612+
PyObject *meth = ((PyMethodObject *)function)->im_func;
4613+
PyObject *self = ((PyMethodObject *)function)->im_self;
4614+
Py_INCREF(meth);
4615+
Py_INCREF(self);
4616+
PEEK(oparg + 1) = self;
4617+
Py_DECREF(function);
4618+
function = meth;
4619+
oparg++;
4620+
nargs++;
4621+
assert(postcall_shrink >= 1);
4622+
postcall_shrink--;
4623+
}
4624+
// Check if the call can be inlined or not
46124625
if (Py_TYPE(function) == &PyFunction_Type && tstate->interp->eval_frame == NULL) {
46134626
int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(function))->co_flags;
46144627
int is_generator = code_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR);

0 commit comments

Comments
 (0)