Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

gh-134323: Fix the newthreading.RLock.locked method#134368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 10 commits intopython:mainfromYvesDup:rlock-threading-locked-failed
May 22, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
18 changes: 18 additions & 0 deletionsLib/test/lock_tests.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -370,6 +370,24 @@ def test_locked(self):
lock.release()
self.assertFalse(lock.locked())

def test_locked_with_2threads(self):
# see gh-134323: check that a rlock which
# is acquired in a different thread,
# is still locked in the main thread.
result = []
rlock = self.locktype()
self.assertFalse(rlock.locked())
def acquire():
result.append(rlock.locked())
rlock.acquire()
result.append(rlock.locked())

with Bunch(acquire, 1):
pass
self.assertTrue(rlock.locked())
self.assertFalse(result[0])
self.assertTrue(result[1])

def test_release_save_unacquired(self):
# Cannot _release_save an unacquired lock
lock = self.locktype()
Expand Down
4 changes: 2 additions & 2 deletionsLib/threading.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -158,7 +158,7 @@ def __repr__(self):
except KeyError:
pass
return "<%s %s.%s object owner=%r count=%d at %s>" % (
"locked" if self._block.locked() else "unlocked",
"locked" if self.locked() else "unlocked",
self.__class__.__module__,
self.__class__.__qualname__,
owner,
Expand DownExpand Up@@ -237,7 +237,7 @@ def __exit__(self, t, v, tb):

def locked(self):
"""Return whether this object is locked."""
return self._count > 0
return self._block.locked()

# Internal methods used by condition variables

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Fix the :meth:`threading.RLock.locked` method.
10 changes: 8 additions & 2 deletionsModules/_threadmodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1022,6 +1022,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)
Expand DownExpand Up@@ -1111,7 +1116,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);
}

Expand DownExpand Up@@ -1219,10 +1224,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);
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp