Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`ctypes` now allocates memory on the stack instead of on the heap
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There was no heap allocation directly before this PR, and there will be no heap allocation after, right? It's just eliminating that dead code path.

Copy link
Copy Markdown
Member Author

@corona10 corona10 Feb 9, 2022

Choose a reason for hiding this comment

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

@sweeneyde Yeah but this is the result of bpo-46323

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Victor and I decided to add NEWS entry for all this change at this moment.
Theoretically, we had to add NEWS entry at the first PR, but it's too late.

to pass arguments while calling a Python callback function.
Patch by Dong-hee Na.
35 changes: 13 additions & 22 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@

#include <stdbool.h>

#ifdef MS_WIN32
# include <malloc.h>
#endif

#include <ffi.h>
#include "ctypes.h"

#ifdef HAVE_ALLOCA_H
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same workaroud

#include "ctypes.h"
#ifdef HAVE_ALLOCA_H
/* AIX needs alloca.h for alloca() */
#include <alloca.h>
#endif

/* AIX needs alloca.h for alloca() */
#include <alloca.h>
#endif

/**************************************************************/

static void
Expand Down Expand Up @@ -147,32 +156,17 @@ static void _CallPythonObject(void *mem,
void **pArgs)
{
PyObject *result = NULL;
PyObject **args = NULL;
Py_ssize_t i = 0, j = 0, nargs = 0;
PyObject *error_object = NULL;
int *space;
PyGILState_STATE state = PyGILState_Ensure();

assert(PyTuple_Check(converters));
nargs = PyTuple_GET_SIZE(converters);
/* Hm. What to return in case of error?
For COM, 0xFFFFFFFF seems better than 0.
*/
if (nargs < 0) {
PrintError("BUG: PySequence_Length");
goto Done;
}

PyObject *args_stack[CTYPES_MAX_ARGCOUNT];
if (nargs <= CTYPES_MAX_ARGCOUNT) {
args = args_stack;
}
else {
args = PyMem_Malloc(nargs * sizeof(PyObject *));
if (args == NULL) {
PyErr_NoMemory();
goto Done;
}
assert(nargs <= CTYPES_MAX_ARGCOUNT);
PyObject **args = NULL;
if (nargs > 0) {
args = alloca(nargs * sizeof(PyObject *));
}

PyObject **cnvs = PySequence_Fast_ITEMS(converters);
Expand Down Expand Up @@ -309,9 +303,6 @@ static void _CallPythonObject(void *mem,
for (j = 0; j < i; j++) {
Py_DECREF(args[j]);
}
if (args != args_stack) {
PyMem_Free(args);
}
PyGILState_Release(state);
}

Expand Down
6 changes: 1 addition & 5 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1160,11 +1160,7 @@ PyObject *_ctypes_callproc(PPROC pProc,
return NULL;
}

args = (struct argument *)alloca(sizeof(struct argument) * argcount);
if (!args) {
PyErr_NoMemory();
return NULL;
}
args = alloca(sizeof(struct argument) * argcount);
memset(args, 0, sizeof(struct argument) * argcount);
argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0;
#ifdef MS_WIN32
Expand Down