Skip to content

Commit 2ff5eb8

Browse files
gh-127945: move initialization of field desc to module exec in ctypes (#132552)
1 parent b6c552f commit 2ff5eb8

File tree

3 files changed

+6
-18
lines changed

3 files changed

+6
-18
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6239,6 +6239,8 @@ _ctypes_mod_exec(PyObject *mod)
62396239
}
62406240
Py_ffi_closure_free(ptr);
62416241

6242+
_ctypes_init_fielddesc();
6243+
62426244
ctypes_state *st = get_module_state(mod);
62436245
st->_unpickle = PyObject_GetAttrString(mod, "_unpickle");
62446246
if (st->_unpickle == NULL) {

Modules/_ctypes/cfield.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,8 +1461,8 @@ _Py_COMP_DIAG_PUSH
14611461

14621462
/* Delayed initialization. Windows cannot statically reference dynamically
14631463
loaded addresses from DLLs. */
1464-
static void
1465-
_ctypes_init_fielddesc_locked(void)
1464+
void
1465+
_ctypes_init_fielddesc(void)
14661466
{
14671467
/* Fixed-width integers */
14681468

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

1662-
static void
1663-
_ctypes_init_fielddesc(void)
1664-
{
1665-
static bool initialized = false;
1666-
static PyMutex mutex = {0};
1667-
PyMutex_Lock(&mutex);
1668-
if (!initialized) {
1669-
_ctypes_init_fielddesc_locked();
1670-
initialized = true;
1671-
}
1672-
PyMutex_Unlock(&mutex);
1673-
}
1674-
16751662
char *
16761663
_ctypes_get_simple_type_chars(void) {
1677-
_ctypes_init_fielddesc();
16781664
return formattable.simple_type_chars;
16791665
}
16801666

16811667
struct fielddesc *
16821668
_ctypes_get_fielddesc(const char *fmt)
16831669
{
1684-
_ctypes_init_fielddesc();
1685-
16861670
struct fielddesc *result = NULL;
16871671
switch(fmt[0]) {
16881672
/*[python input]

Modules/_ctypes/ctypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ extern int _ctypes_simple_instance(ctypes_state *st, PyObject *obj);
537537

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

540+
extern void _ctypes_init_fielddesc(void);
541+
540542
#ifdef USING_MALLOC_CLOSURE_DOT_C
541543
void Py_ffi_closure_free(void *p);
542544
void *Py_ffi_closure_alloc(size_t size, void** codeloc);

0 commit comments

Comments
 (0)