https://github.com/python/cpython/commit/2ff5eb8582939eb9182d3449d08542881caf3e0d
commit: 2ff5eb8582939eb9182d3449d08542881caf3e0d
branch: main
author: Kumar Aditya <kumaradi...@python.org>
committer: kumaraditya303 <kumaradi...@python.org>
date: 2025-04-15T19:30:33+05:30
summary:

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

files:
M Modules/_ctypes/_ctypes.c
M Modules/_ctypes/cfield.c
M Modules/_ctypes/ctypes.h

diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 15d5582c55c11a..9858ec8e714dbb 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 056e6dfd883a56..50b26f2a7e2a92 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 6e9aa359468f7f..3533cfc66fc56d 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);

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to