[Python-checkins] gh-102029: Deprecate passing arguments to `_PyRLock` in `threading` (#102071)

gpshead webhook-mailer at python.org
Thu Aug 17 12:19:11 EDT 2023


https://github.com/python/cpython/commit/80f30cf51bd89411ef1220d714b33667d6a39901
commit: 80f30cf51bd89411ef1220d714b33667d6a39901
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: gpshead <greg at krypto.org>
date: 2023-08-17T09:19:07-07:00
summary:

gh-102029: Deprecate passing arguments to `_PyRLock` in `threading` (#102071)

files:
A Misc/NEWS.d/next/Library/2023-02-20-15-41-59.gh-issue-102029.9ZPG99.rst
M Doc/whatsnew/3.13.rst
M Lib/test/test_threading.py
M Lib/threading.py

diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index 519dee5eb7d6c..13ae6e595dc99 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -357,6 +357,12 @@ Pending Removal in Python 3.15
   They will be removed in Python 3.15.
   (Contributed by Victor Stinner in :gh:`105096`.)
 
+* Passing any arguments to :func:`threading.RLock` is now deprecated.
+  C version allows any numbers of args and kwargs,
+  but they are just ignored. Python version does not allow any arguments.
+  All arguments will be removed from :func:`threading.RLock` in Python 3.15.
+  (Contributed by Nikita Sobolev in :gh:`102029`.)
+
 Pending Removal in Python 3.16
 ------------------------------
 
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index 4a91eef31c4b8..6465a44656584 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -1748,6 +1748,30 @@ class PyRLockTests(lock_tests.RLockTests):
 class CRLockTests(lock_tests.RLockTests):
     locktype = staticmethod(threading._CRLock)
 
+    def test_signature(self):  # gh-102029
+        with warnings.catch_warnings(record=True) as warnings_log:
+            threading.RLock()
+        self.assertEqual(warnings_log, [])
+
+        arg_types = [
+            ((1,), {}),
+            ((), {'a': 1}),
+            ((1, 2), {'a': 1}),
+        ]
+        for args, kwargs in arg_types:
+            with self.subTest(args=args, kwargs=kwargs):
+                with self.assertWarns(DeprecationWarning):
+                    threading.RLock(*args, **kwargs)
+
+        # Subtypes with custom `__init__` are allowed (but, not recommended):
+        class CustomRLock(self.locktype):
+            def __init__(self, a, *, b) -> None:
+                super().__init__()
+
+        with warnings.catch_warnings(record=True) as warnings_log:
+            CustomRLock(1, b=2)
+        self.assertEqual(warnings_log, [])
+
 class EventTests(lock_tests.EventTests):
     eventtype = staticmethod(threading.Event)
 
diff --git a/Lib/threading.py b/Lib/threading.py
index e036cb891e196..f6bbdb0d0c80e 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -4,6 +4,7 @@
 import sys as _sys
 import _thread
 import functools
+import warnings
 
 from time import monotonic as _time
 from _weakrefset import WeakSet
@@ -116,6 +117,12 @@ def RLock(*args, **kwargs):
     acquired it.
 
     """
+    if args or kwargs:
+        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)
diff --git a/Misc/NEWS.d/next/Library/2023-02-20-15-41-59.gh-issue-102029.9ZPG99.rst b/Misc/NEWS.d/next/Library/2023-02-20-15-41-59.gh-issue-102029.9ZPG99.rst
new file mode 100644
index 0000000000000..4d6f05f252452
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-02-20-15-41-59.gh-issue-102029.9ZPG99.rst
@@ -0,0 +1 @@
+Deprecate passing any arguments to :func:`threading.RLock`.



More information about the Python-checkins mailing list