[3.14] gh-134323: Fix the new threading.RLock.locked method (GH-134368) (#134510)

gh-134323: Fix the new `threading.RLock.locked` method (GH-134368)
(cherry picked from commit 3effede97c)

Co-authored-by: Duprat <yduprat@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
Miss Islington (bot) 2025-05-22 16:14:53 +02:00 committed by GitHub
parent 7e7391810f
commit ced49a196f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 4 deletions

View file

@ -1011,6 +1011,11 @@ rlock_traverse(PyObject *self, visitproc visit, void *arg)
return 0;
}
static int
rlock_locked_impl(rlockobject *self)
{
return PyMutex_IsLocked(&self->lock.mutex);
}
static void
rlock_dealloc(PyObject *self)
@ -1100,7 +1105,7 @@ static PyObject *
rlock_locked(PyObject *op, PyObject *Py_UNUSED(ignored))
{
rlockobject *self = rlockobject_CAST(op);
int is_locked = _PyRecursiveMutex_IsLockedByCurrentThread(&self->lock);
int is_locked = rlock_locked_impl(self);
return PyBool_FromLong(is_locked);
}
@ -1202,10 +1207,11 @@ rlock_repr(PyObject *op)
{
rlockobject *self = rlockobject_CAST(op);
PyThread_ident_t owner = self->lock.thread;
int locked = rlock_locked_impl(self);
size_t count = self->lock.level + 1;
return PyUnicode_FromFormat(
"<%s %s object owner=%" PY_FORMAT_THREAD_IDENT_T " count=%zu at %p>",
owner ? "locked" : "unlocked",
locked ? "locked" : "unlocked",
Py_TYPE(self)->tp_name, owner,
count, self);
}