Skip to content

Commit 26d4bf4

Browse files
committed
Apply suggestions from code review
1 parent b448d4b commit 26d4bf4

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Lib/test/lock_tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,22 @@ def test_locked(self):
371371
self.assertFalse(lock.locked())
372372

373373
def test_locked_2threads(self):
374+
# see gh-134323: check that a rlock which
375+
# is acquired in a secaondary thread,
376+
# is still locked in the main thread.
374377
l = []
375378
rlock = self.locktype()
379+
self.assertFalse(rlock.locked())
376380
def acquire():
381+
l.append(rlock.locked())
377382
rlock.acquire()
378383
l.append(rlock.locked())
379384

380385
with Bunch(acquire, 1):
381386
pass
382387
self.assertTrue(rlock.locked())
383-
self.assertTrue(l[0])
388+
self.assertFalse(l[0])
389+
self.assertTrue(l[1])
384390
del rlock
385391

386392
def test_release_save_unacquired(self):
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Fix the public :meth:`locked<threading.RLock>` and its implementation in the ``_thread`` module.
2-
Also adding a unit test in ``lock_test.py`` file.
1+
Fix the :meth:`threading.RLock.locked` method.

Modules/_threadmodule.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,13 @@ rlock_traverse(PyObject *self, visitproc visit, void *arg)
10221022
return 0;
10231023
}
10241024

1025+
/*
1026+
helper function used by rlock_locked and rlock_repr.
1027+
*/
1028+
static int
1029+
rlock_locked_impl(rlockobject *self) {
1030+
return PyMutex_IsLocked(&self->lock.mutex);
1031+
}
10251032

10261033
static void
10271034
rlock_dealloc(PyObject *self)
@@ -1114,8 +1121,8 @@ rlock_locked(PyObject *op, PyObject *Py_UNUSED(ignored))
11141121
see gh-134323: the `_PyRecursiveMutex_IsLocked` function does not exist, so we cast the `op`
11151122
to `lockobject` in order to call `PyMutex_IsLocked`.
11161123
*/
1117-
lockobject *self = lockobject_CAST(op);
1118-
int is_locked = PyMutex_IsLocked(&self->lock);
1124+
rlockobject *self = rlockobject_CAST(op);
1125+
int is_locked = rlock_locked_impl(self);
11191126
return PyBool_FromLong(is_locked);
11201127
}
11211128

0 commit comments

Comments
 (0)