Skip to content

gh-127945: move initialization of field desc to module exec in ctypes #132552

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 1 commit into from
Apr 15, 2025
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
2 changes: 2 additions & 0 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -6239,6 +6239,8 @@ _ctypes_mod_exec(PyObject *mod)
}
Py_ffi_closure_free(ptr);

_ctypes_init_fielddesc();

ctypes_state *st = get_module_state(mod);
st->_unpickle = PyObject_GetAttrString(mod, "_unpickle");
if (st->_unpickle == NULL) {
Expand Down
20 changes: 2 additions & 18 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -1461,8 +1461,8 @@ _Py_COMP_DIAG_PUSH

/* Delayed initialization. Windows cannot statically reference dynamically
loaded addresses from DLLs. */
static void
_ctypes_init_fielddesc_locked(void)
void
_ctypes_init_fielddesc(void)
{
/* Fixed-width integers */

Expand Down Expand Up @@ -1659,30 +1659,14 @@ print(f" formattable.simple_type_chars[i] = 0;")
#undef FIXINT_FIELDDESC_FOR
_Py_COMP_DIAG_POP

static void
_ctypes_init_fielddesc(void)
{
static bool initialized = false;
static PyMutex mutex = {0};
PyMutex_Lock(&mutex);
if (!initialized) {
_ctypes_init_fielddesc_locked();
initialized = true;
}
PyMutex_Unlock(&mutex);
}

char *
_ctypes_get_simple_type_chars(void) {
_ctypes_init_fielddesc();
return formattable.simple_type_chars;
}

struct fielddesc *
_ctypes_get_fielddesc(const char *fmt)
{
_ctypes_init_fielddesc();

struct fielddesc *result = NULL;
switch(fmt[0]) {
/*[python input]
Expand Down
2 changes: 2 additions & 0 deletions Modules/_ctypes/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ extern int _ctypes_simple_instance(ctypes_state *st, PyObject *obj);

PyObject *_ctypes_get_errobj(ctypes_state *st, int **pspace);

extern void _ctypes_init_fielddesc(void);

#ifdef USING_MALLOC_CLOSURE_DOT_C
void Py_ffi_closure_free(void *p);
void *Py_ffi_closure_alloc(size_t size, void** codeloc);
Expand Down
Loading