Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ced49a1

Browse filesBrowse files
miss-islingtonYvesDupkumaraditya303
authored
[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 3effede) Co-authored-by: Duprat <yduprat@gmail.com> Co-authored-by: Kumar Aditya <kumaraditya@python.org>
1 parent 7e73918 commit ced49a1
Copy full SHA for ced49a1

File tree

Expand file treeCollapse file tree

4 files changed

+29
-4
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+29
-4
lines changed

‎Lib/test/lock_tests.py

Copy file name to clipboardExpand all lines: Lib/test/lock_tests.py
+18Lines 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

Copy file name to clipboardExpand all lines: Lib/threading.py
+2-2Lines 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

+1Lines 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

Copy file name to clipboardExpand all lines: Modules/_threadmodule.c
+8-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.