Open
Description
Feature or enhancement
Proposal:
In my C extension, I wanted to write:
assert(PyMutex_IsLocked(&mutex));
to verify that my own code is maintaining its own locking invariants.
PyMutex_IsLocked
exists, but is private:
cpython/Include/internal/pycore_lock.h
Line 30 in 0afbd4e
Could we make it public and document it, please?
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Activity
ngoldbaum commentedon May 14, 2025
I ran into this when I was writing PyMutex bindings for PyO3 - it was very difficult to test the bindings because the
PyMutex
API is so limited.ZeroIntensity commentedon May 14, 2025
Ideally, we should document that it's for use in assertions only. We need to make sure we're not enabling people to write thread-unsafe code like this:
makslevental commentedon May 14, 2025
maybe something like
?
but not sure if this y'all's style (i came here from mlir...)
hawkinsp commentedon May 14, 2025
It's important to expose an API like
PyMutex_IsLocked
even if you expose some sort of assertion helper, because people want to use their own debugging/printing/logging libraries to handle this sort of failure.colesbury commentedon May 14, 2025
Yeah, I just want to echo @hawkinsp comment. I think we should expose
PyMutex_IsLocked()
and document its intended purposes. I don't think we need to be overly concerned about people misusing it.colesbury commentedon May 14, 2025
We should also open a corresponding issue in the C API working group
[-]Expose `PyMutex_IsLocked` in the public API for free-threading[/-][+]Expose `PyMutex_IsLocked` in the public API[/+][-]Expose `PyMutex_IsLocked` in the public API[/-][+]Expose `PyMutex_IsLocked` in the public C API[/+]PyMutex_IsLocked
in the public C API capi-workgroup/decisions#66PyMutex_IsLocked
in the public C API #134365pythongh-134009: Expose `PyMutex_IsLocked` in the public C API
2 remaining items