Skip to content

Commit 1b41ace

Browse files
committed
Use a mutex around _ctypes_init_fielddesc
1 parent 870a038 commit 1b41ace

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Modules/_ctypes/cfield.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ P_get(void *ptr, Py_ssize_t size)
11941194

11951195
/* Table with info about all formats.
11961196
* Must be accessed via _ctypes_get_fielddesc, which initializes it on
1197-
* first use.
1197+
* first use. After initialization it's treated as constant & read-only.
11981198
*/
11991199

12001200
struct formattable {
@@ -1464,12 +1464,14 @@ _Py_COMP_DIAG_POP
14641464
struct fielddesc *
14651465
_ctypes_get_fielddesc(const char *fmt)
14661466
{
1467-
static int initialized = 0;
1467+
static bool initialized = 0;
1468+
static PyMutex mutex = {0};
1469+
PyMutex_Lock(&mutex);
14681470
if (!initialized) {
14691471
_ctypes_init_fielddesc();
1470-
14711472
initialized = 1;
14721473
}
1474+
PyMutex_Unlock(&mutex);
14731475
struct fielddesc *result = NULL;
14741476
switch(fmt[0]) {
14751477
/*[python input]

0 commit comments

Comments
 (0)