]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127945: move initialization of field desc to module exec in ctypes (#132552)
authorKumar Aditya <kumaraditya@python.org>
Tue, 15 Apr 2025 14:00:33 +0000 (19:30 +0530)
committerGitHub <noreply@github.com>
Tue, 15 Apr 2025 14:00:33 +0000 (19:30 +0530)
Modules/_ctypes/_ctypes.c
Modules/_ctypes/cfield.c
Modules/_ctypes/ctypes.h

index 15d5582c55c11a778b9e89178ddbc622cf22594c..9858ec8e714dbb196678f3b71e945db56fa5d5b4 100644 (file)
@@ -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) {
index 056e6dfd883a5634bf45b7b5952b827d50e9e8ff..50b26f2a7e2a92a1b1f5036a05648b6c69cbc46b 100644 (file)
@@ -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]
index 6e9aa359468f7f3651e7a1728f7c8ec9424f0c7f..3533cfc66fc56d992eb8746cde78f2116f47e74c 100644 (file)
@@ -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);