8000 bpo-46323: Use PyObject_Vectorcall while calling ctypes callback function by corona10 · Pull Request #31138 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46323: Use PyObject_Vectorcall while calling ctypes callback function #31138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
bpo-46323: Use PyObject_Vectorcall while calling ctypes callback funtion
  • Loading branch information
corona10 committed Feb 8, 2022
commit 53bcb976217a7b1aa2fae328ac98f08558be1495
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use :c:func:`PyObject_Vectorcall` while calling ctypes callback funtion.
Patch by Dong-hee Na.
32 changes: 21 additions & 11 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ static void _CallPythonObject(void *mem,
int flags,
void **pArgs)
{
Py_ssize_t i;
PyObject *result;
PyObject *arglist = NULL;
Py_ssize_t nArgs;
PyObject *error_object = NULL;
int *space;
Expand All @@ -163,12 +161,19 @@ static void _CallPythonObject(void *mem,
goto Done;
}

arglist = PyTuple_New(nArgs);
if (!arglist) {
PrintError("PyTuple_New()");
goto Done;
PyObject *args_stack[_PY_FASTCALL_SMALL_STACK];
PyObject **args;
if (nArgs <= (Py_ssize_t)Py_ARRAY_LENGTH(args_stack)) {
args = args_stack;
}
for (i = 0; i < nArgs; ++i) {
else {
args = PyMem_Malloc(nArgs * sizeof(PyObject *));
if (args == NULL) {
PyErr_NoMemory();
goto Done;
}
}
for (Py_ssize_t i = 0; i < nArgs; ++i) {
/* Note: new reference! */
PyObject *cnv = PySequence_GetItem(converters, i);
StgDictObject *dict;
Expand All @@ -186,7 +191,7 @@ static void _CallPythonObject(void *mem,
Py_DECREF(cnv);
goto Done;
}
PyTuple_SET_ITEM(arglist, i, v);
args[i] = v;
/* XXX XXX XX
We have the problem that c_byte or c_short have dict->size of
1 resp. 4, but these parameters are pushed as sizeof(int) bytes.
Expand All @@ -207,7 +212,7 @@ static void _CallPythonObject(void *mem,
goto Done;
}
memcpy(obj->b_ptr, *pArgs, dict->size);
PyTuple_SET_ITEM(arglist, i, (PyObject *)obj);
args[i] = (PyObject *)obj;
#ifdef MS_WIN32
TryAddRef(dict, obj);
#endif
Expand Down Expand Up @@ -241,7 +246,7 @@ static void _CallPythonObject(void *mem,
#endif
}

result = PyObject_CallObject(callable, arglist);
result = PyObject_Vectorcall(callable, args, nArgs, NULL);
if (result == NULL) {
_PyErr_WriteUnraisableMsg("on calling ctypes callback function",
callable);
Expand Down Expand Up @@ -308,7 +313,12 @@ static void _CallPythonObject(void *mem,
Py_XDECREF(result);

Done:
Py_XDECREF(arglist);
for (Py_ssize_t i = 0; i < nArgs; i++) {
Py_XDECREF(args[i]);
}
if (args != args_stack) {
PyMem_Free(args);
}
PyGILState_Release(state);
}

Expand Down
0