Skip to content

Commit 7b10320

Browse files
Fix data race on func_code in free-threaded Python
1 parent d926271 commit 7b10320

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

Include/cpython/funcobject.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,13 @@ PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
8888
/* Static inline functions for direct access to these values.
8989
Type checks are *not* done, so use with care. */
9090
static inline PyObject* PyFunction_GET_CODE(PyObject *func) {
91-
return _PyFunction_CAST(func)->func_code;
91+
PyFunctionObject *op = _PyFunction_CAST(func);
92+
#ifdef Py_GIL_DISABLED
93+
return (PyObject *)_Py_atomic_load_ptr(&op->func_code);
94+
#else
95+
return op->func_code;
96+
#endif
9297
}
93-
#define PyFunction_GET_CODE(func) PyFunction_GET_CODE(_PyObject_CAST(func))
94-
9598
static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) {
9699
return _PyFunction_CAST(func)->func_globals;
97100
}

0 commit comments

Comments
 (0)