Skip to content
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ typedef struct {
vectorcallfunc vectorcall;
} methodcallerobject;

#ifndef Py_GIL_DISABLED

static int _methodcaller_initialize_vectorcall(methodcallerobject* mc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static int _methodcaller_initialize_vectorcall(methodcallerobject* mc)
static int
_methodcaller_initialize_vectorcall(methodcallerobject *mc)

{
PyObject* args = mc->xargs;
Expand Down Expand Up @@ -1659,13 +1659,26 @@ methodcaller_vectorcall(
}

assert(mc->vectorcall_args != 0);
mc->vectorcall_args[0] = args[0];

Py_ssize_t number_of_arguments = PyTuple_GET_SIZE(mc->xargs) +
(mc->vectorcall_kwnames? PyTuple_GET_SIZE(mc->vectorcall_kwnames):0) + 1;
size_t buffer_size = sizeof(PyObject *) * (number_of_arguments + 1);

// for number_of_arguments == 1 we could optimize by setting tmp_args equal to &args
PyObject **tmp_args = (PyObject **) PyMem_Malloc(buffer_size);
if (tmp_args == NULL) {
PyErr_NoMemory();
return NULL;
}
memcpy(tmp_args, mc->vectorcall_args, buffer_size);
tmp_args[0] = args[0];
return PyObject_VectorcallMethod(
mc->name, mc->vectorcall_args,
mc->name, tmp_args,
(PyTuple_GET_SIZE(mc->xargs)) | PY_VECTORCALL_ARGUMENTS_OFFSET,
mc->vectorcall_kwnames);

PyMem_Free(tmp_args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyMem_Free is after the return statement

}
#endif


/* AC 3.5: variable number of arguments, not currently support by AC */
Expand Down Expand Up @@ -1704,15 +1717,7 @@ methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
mc->kwds = Py_XNewRef(kwds);
mc->vectorcall_args = 0;


#ifdef Py_GIL_DISABLED
// gh-127065: The current implementation of methodcaller_vectorcall
// is not thread-safe because it modifies the `vectorcall_args` array,
// which is shared across calls.
mc->vectorcall = NULL;
#else
mc->vectorcall = (vectorcallfunc)methodcaller_vectorcall;
#endif

PyObject_GC_Track(mc);
return (PyObject *)mc;
Expand Down
Loading