Repository navigation
Expose PyMutex_IsLocked in the public C API #134009
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on 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
PyMutexAPI is so limited.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:
if (PyMutex_IsLocked(lock)) { PyMutex_Unlock(lock); }
Ideally, we should document that it's for use in assertions only
maybe something like
#define ASSERT_PY_MUTEX_IS_LOCKED(mutex, msg) assert(PyMutex_IsLocked(mutex) && msg)
?
but not sure if this y'all's style (i came here from mlir...)
It's important to expose an API like
PyMutex_IsLockedeven 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.Reacted by Sam GrossYeah, 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.Reacted by Emma SmithWe should also open a corresponding issue in the C API working group
- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on May 15, 2025 - changed the title
[-]Expose `PyMutex_IsLocked` in the public API for free-threading[/-][+]Expose `PyMutex_IsLocked` in the public API[/+]on May 15, 2025 - changed the title
[-]Expose `PyMutex_IsLocked` in the public API[/-][+]Expose `PyMutex_IsLocked` in the public C API[/+]on May 15, 2025 - added a commit that references this issue
on May 20, 2025 - added a commit that references this issue
on May 27, 2025
Feature or enhancement
Proposal:
In my C extension, I wanted to write:
to verify that my own code is maintaining its own locking invariants.
PyMutex_IsLockedexists, 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
Linked PRs
PyMutex_IsLockedin the public C API #134365PyMutex_IsLockedin the public C API (gh-134365) #136971