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

Commitced49a1

Browse files
miss-islingtonYvesDupkumaraditya303
authored
[3.14]gh-134323: Fix the newthreading.RLock.locked method (GH-134368) (#134510)
gh-134323: Fix the new `threading.RLock.locked` method (GH-134368)(cherry picked from commit3effede)Co-authored-by: Duprat <yduprat@gmail.com>Co-authored-by: Kumar Aditya <kumaraditya@python.org>
1 parent7e73918 commitced49a1

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+
deftest_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+
defacquire():
376+
result.append(rlock.locked())
377+
rlock.acquire()
378+
result.append(rlock.locked())
379+
380+
withBunch(acquire,1):
381+
pass
382+
self.assertTrue(rlock.locked())
383+
self.assertFalse(result[0])
384+
self.assertTrue(result[1])
385+
368386
deftest_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
exceptKeyError:
166166
pass
167167
return"<%s %s.%s object owner=%r count=%d at %s>"% (
168-
"locked"ifself._block.locked()else"unlocked",
168+
"locked"ifself.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
deflocked(self):
246246
"""Return whether this object is locked."""
247-
returnself._count>0
247+
returnself._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
return0;
10121012
}
10131013

1014+
staticint
1015+
rlock_locked_impl(rlockobject*self)
1016+
{
1017+
returnPyMutex_IsLocked(&self->lock.mutex);
1018+
}
10141019

10151020
staticvoid
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-
intis_locked=_PyRecursiveMutex_IsLockedByCurrentThread(&self->lock);
1108+
intis_locked=rlock_locked_impl(self);
11041109
returnPyBool_FromLong(is_locked);
11051110
}
11061111

@@ -1202,10 +1207,11 @@ rlock_repr(PyObject *op)
12021207
{
12031208
rlockobject*self=rlockobject_CAST(op);
12041209
PyThread_ident_towner=self->lock.thread;
1210+
intlocked=rlock_locked_impl(self);
12051211
size_tcount=self->lock.level+1;
12061212
returnPyUnicode_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)

[8]ページ先頭

©2009-2025 Movatter.jp