From 2ff5eb8582939eb9182d3449d08542881caf3e0d Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 15 Apr 2025 19:30:33 +0530 Subject: gh-127945: move initialization of field desc to module exec in ctypes (#132552) --- Modules/_ctypes/_ctypes.c | 2 ++ Modules/_ctypes/cfield.c | 20 ++------------------ Modules/_ctypes/ctypes.h | 2 ++ 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 15d5582..9858ec8 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -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) { diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index 056e6df..50b26f2 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -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 */ @@ -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] diff --git a/Modules/_ctypes/ctypes.h b/Modules/_ctypes/ctypes.h index 6e9aa35..3533cfc 100644 --- a/Modules/_ctypes/ctypes.h +++ b/Modules/_ctypes/ctypes.h @@ -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); -- cgit v0.12