Skip to content

Commit 3e871a1

Browse files
YvesDupkumaraditya303
authored andcommitted
pythongh-134323: Fix the new threading.RLock.locked method (pythonGH-134368)
(cherry picked from commit 3effede) Co-authored-by: Duprat <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
1 parent 85c8c0a commit 3e871a1

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

Lib/test/lock_tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,24 @@ def test_locked(self):
365365
lock.release()
366366
self.assertFalse(lock.locked())
367367

368+
def test_locked_with_2threads(self):
369+
# see gh-134323: check that a rlock which
370+
# is acquired in a different thread,
371+
# is still locked in the main thread.
372+
result = []
373+
rlock = self.locktype()
374+
self.assertFalse(rlock.locked())
375+
def acquire():
376+
result.append(rlock.locked())
377+
rlock.acquire()
378+
result.append(rlock.locked())
379+
380+
with Bunch(acquire, 1):
381+
pass
382+
self.assertTrue(rlock.locked())
383+
self.assertFalse(result[0])
384+
self.assertTrue(result[1])
385+
368386
def test_release_save_unacquired(self):
369387
# Cannot _release_save an unacquired lock
370388
lock = self.locktype()

Lib/threading.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def __repr__(self):
165165
except KeyError:
166166
pass
167167
return "<%s %s.%s object owner=%r count=%d at %s>" % (
168-
"locked" if self._block.locked() else "unlocked",
168+
"locked" if self.locked() else "unlocked",
169169
self.__class__.__module__,
170170
self.__class__.__qualname__,
171171
owner,
@@ -244,7 +244,7 @@ def __exit__(self, t, v, tb):
244244

245245
def locked(self):
246246
"""Return whether this object is locked."""
247-
return self._count > 0
247+
return self._block.locked()
248248

249249
# Internal methods used by condition variables
250250

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the :meth:`threading.RLock.locked` method.

Modules/_threadmodule.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,11 @@ rlock_traverse(PyObject *self, visitproc visit, void *arg)
10111011
return 0;
10121012
}
10131013

1014+
static int
1015+
rlock_locked_impl(rlockobject *self)
1016+
{
1017+
return PyMutex_IsLocked(&self->lock.mutex);
1018+
}
10141019

10151020
static void
10161021
rlock_dealloc(PyObject *self)
@@ -1100,7 +1105,7 @@ static PyObject *
11001105
rlock_locked(PyObject *op, PyObject *Py_UNUSED(ignored))
11011106
{
11021107
rlockobject *self = rlockobject_CAST(op);
1103-
int is_locked = _PyRecursiveMutex_IsLockedByCurrentThread(&self->lock);
1108+
int is_locked = rlock_locked_impl(self);
11041109
return PyBool_FromLong(is_locked);
11051110
}
11061111

@@ -1202,10 +1207,11 @@ rlock_repr(PyObject *op)
12021207
{
12031208
rlockobject *self = rlockobject_CAST(op);
12041209
PyThread_ident_t owner = self->lock.thread;
1210+
int locked = rlock_locked_impl(self);
12051211
size_t count = self->lock.level + 1;
12061212
return PyUnicode_FromFormat(
12071213
"<%s %s object owner=%" PY_FORMAT_THREAD_IDENT_T " count=%zu at %p>",
1208-
owner ? "locked" : "unlocked",
1214+
locked ? "locked" : "unlocked",
12091215
Py_TYPE(self)->tp_name, owner,
12101216
count, self);
12111217
}

0 commit comments

Comments
 (0)