Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/lock_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,26 @@ class RLockTests(BaseLockTests):
"""
Tests for recursive locks.
"""
def test_repr_count(self):
# see gh-134322: check that count values are correct:
# when a rlock is just created,
# in a secondary thread when rlock is acquired in the main thread.
lock = self.locktype()
self.assertIn("count=0", repr(lock))
self.assertIn("<unlocked", repr(lock))
lock.acquire()
lock.acquire()
self.assertIn("count=2", repr(lock))
self.assertIn("<locked", repr(lock))

l = []
def acquire():
l.append(repr(lock))
with Bunch(acquire, 1):
pass
self.assertIn("count=2", l[0])
self.assertIn("<locked", l[0])

def test_reacquire(self):
lock = self.locktype()
lock.acquire()
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_importlib/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ModuleLockAsRLockTests:
# lock status in repr unsupported
test_repr = None
test_locked_repr = None
test_repr_count = None

def tearDown(self):
for splitinit in init.values():
Expand Down
16 changes: 15 additions & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,14 @@ rlock_traverse(PyObject *self, visitproc visit, void *arg)
return 0;
}

/*
helper function used by rlock_locked and rlock_repr.
*/
static int
rlock_locked_impl(rlockobject *self) {
return PyMutex_IsLocked(&self->lock.mutex);
}


static void
rlock_dealloc(PyObject *self)
Expand Down Expand Up @@ -1219,7 +1227,13 @@ rlock_repr(PyObject *op)
{
rlockobject *self = rlockobject_CAST(op);
PyThread_ident_t owner = self->lock.thread;
size_t count = self->lock.level + 1;
size_t count;
if (rlock_locked_impl(self)) {
count = self->lock.level + 1;
}
else {
count = 0;
}
return PyUnicode_FromFormat(
"<%s %s object owner=%" PY_FORMAT_THREAD_IDENT_T " count=%zu at %p>",
owner ? "locked" : "unlocked",
Expand Down
Loading