gh-134087: enforce signature of `threading.RLock` by picnixz · Pull Request #134178 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.15.rst
5 changes: 5 additions & 0 deletions Lib/test/lock_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def test_constructor(self):
lock = self.locktype()
del lock

def test_constructor_noargs(self):
self.assertRaises(TypeError, self.locktype, 1)
self.assertRaises(TypeError, self.locktype, x=1)
self.assertRaises(TypeError, self.locktype, 1, x=2)

def test_repr(self):
lock = self.locktype()
self.assertRegex(repr(lock), "<unlocked .* object (.*)?at .*>")
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2137,8 +2137,7 @@ def test_signature(self): # gh-102029
]
for args, kwargs in arg_types:
with self.subTest(args=args, kwargs=kwargs):
with self.assertWarns(DeprecationWarning):
threading.RLock(*args, **kwargs)
self.assertRaises(TypeError, threading.RLock, *args, **kwargs)

# Subtypes with custom `__init__` are allowed (but, not recommended):
class CustomRLock(self.locktype):
Expand All @@ -2156,6 +2155,9 @@ class ConditionAsRLockTests(lock_tests.RLockTests):
# Condition uses an RLock by default and exports its API.
locktype = staticmethod(threading.Condition)

def test_constructor_noargs(self):
self.skipTest("Condition allows positional arguments")

def test_recursion_count(self):
self.skipTest("Condition does not expose _recursion_count()")

Expand Down
13 changes: 3 additions & 10 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def gettrace():

Lock = _LockType

def RLock(*args, **kwargs):
def RLock():
"""Factory function that returns a new reentrant lock.

A reentrant lock must be released by the thread that acquired it. Once a
Expand All @@ -132,16 +132,9 @@ def RLock(*args, **kwargs):
acquired it.

"""
if args or kwargs:
import warnings
warnings.warn(
'Passing arguments to RLock is deprecated and will be removed in 3.15',
DeprecationWarning,
stacklevel=2,
)
if _CRLock is None:
return _PyRLock(*args, **kwargs)
return _CRLock(*args, **kwargs)
return _PyRLock()
return _CRLock()

class _RLock:
"""This class implements reentrant lock objects.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Remove support for arbitrary positional or keyword arguments in the C
implementation of :class:`threading.RLock` objects. This was deprecated
since Python 3.14. Patch by Bénédikt Tran.
82 changes: 43 additions & 39 deletions Modules/_threadmodule.c
50 changes: 48 additions & 2 deletions Modules/clinic/_threadmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading