Skip to content

Commit 0fe7b59

Browse files
committed
ctypes: do not re-lock the destination in PyCData_set()
PyCData_set() already requires its caller to hold the critical section on the destination object and asserts it, but finished by calling KeepRef(), which takes that lock again. Every store of a Python object into a Structure or Union field, an Array item or a Pointer item went through it. Call KeepRef_lock_held() instead. Measured on the free-threaded build: Structure int field assignment 38 -> 24 ns, c_char_p field assignment 69 -> 54 ns. (cherry picked from commit ead0976)
1 parent 60f8473 commit 0fe7b59

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3563,7 +3563,7 @@ PyCData_set(ctypes_state *st,
35633563
/* KeepRef steals a refcount from it's last argument */
35643564
/* If KeepRef fails, we are stumped. The dst memory block has already
35653565
been changed */
3566-
return KeepRef(mem, index, result);
3566+
return KeepRef_lock_held(mem, index, result);
35673567
}
35683568

35693569

0 commit comments

Comments
 (0)