Skip to content

Commit 801c5c9

Browse files
authored
improve identity of threading module's Lock and RLock (#12966)
1 parent eb8af63 commit 801c5c9

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ turtledemo\..+
3131
# TODO: Module members that exist at runtime, but are missing from stubs
3232
# ======================================================================
3333

34-
_thread.RLock
3534
tkinter.Misc.config
3635

3736

stdlib/_thread.pyi

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@ error = RuntimeError
1313

1414
def _count() -> int: ...
1515
@final
16-
class LockType:
16+
class RLock:
1717
def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ...
1818
def release(self) -> None: ...
19-
def locked(self) -> bool: ...
20-
def acquire_lock(self, blocking: bool = True, timeout: float = -1) -> bool: ...
21-
def release_lock(self) -> None: ...
22-
def locked_lock(self) -> bool: ...
23-
def __enter__(self) -> bool: ...
24-
def __exit__(
25-
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
26-
) -> None: ...
19+
__enter__ = acquire
20+
def __exit__(self, t: type[BaseException] | None, v: BaseException | None, tb: TracebackType | None) -> None: ...
2721

2822
if sys.version_info >= (3, 13):
2923
@final
@@ -37,7 +31,33 @@ if sys.version_info >= (3, 13):
3731
def start_joinable_thread(
3832
function: Callable[[], object], handle: _ThreadHandle | None = None, daemon: bool = True
3933
) -> _ThreadHandle: ...
40-
lock = LockType
34+
@final
35+
class lock:
36+
def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ...
37+
def release(self) -> None: ...
38+
def locked(self) -> bool: ...
39+
def acquire_lock(self, blocking: bool = True, timeout: float = -1) -> bool: ...
40+
def release_lock(self) -> None: ...
41+
def locked_lock(self) -> bool: ...
42+
def __enter__(self) -> bool: ...
43+
def __exit__(
44+
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
45+
) -> None: ...
46+
47+
LockType = lock
48+
else:
49+
@final
50+
class LockType:
51+
def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ...
52+
def release(self) -> None: ...
53+
def locked(self) -> bool: ...
54+
def acquire_lock(self, blocking: bool = True, timeout: float = -1) -> bool: ...
55+
def release_lock(self) -> None: ...
56+
def locked_lock(self) -> bool: ...
57+
def __enter__(self) -> bool: ...
58+
def __exit__(
59+
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
60+
) -> None: ...
4161

4262
@overload
4363
def start_new_thread(function: Callable[[Unpack[_Ts]], object], args: tuple[Unpack[_Ts]], /) -> int: ...

stdlib/threading.pyi

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,22 @@ class Thread:
100100
class _DummyThread(Thread):
101101
def __init__(self) -> None: ...
102102

103-
@final
104-
class Lock:
105-
def __enter__(self) -> bool: ...
106-
def __exit__(
107-
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
108-
) -> None: ...
109-
def acquire(self, blocking: bool = ..., timeout: float = ...) -> bool: ...
110-
def release(self) -> None: ...
111-
def locked(self) -> bool: ...
112-
def acquire_lock(self, blocking: bool = ..., timeout: float = ...) -> bool: ... # undocumented
113-
def release_lock(self) -> None: ... # undocumented
114-
def locked_lock(self) -> bool: ... # undocumented
103+
# This is actually the function _thread.allocate_lock for <= 3.12
104+
Lock = _thread.LockType
115105

106+
# Python implementation of RLock.
116107
@final
117108
class _RLock:
109+
_count: int
118110
def acquire(self, blocking: bool = True, timeout: float = -1) -> bool: ...
119111
def release(self) -> None: ...
120112
__enter__ = acquire
121113
def __exit__(self, t: type[BaseException] | None, v: BaseException | None, tb: TracebackType | None) -> None: ...
122114

123-
RLock = _RLock
115+
RLock = _thread.RLock # Actually a function at runtime.
124116

125117
class Condition:
126-
def __init__(self, lock: Lock | _RLock | None = None) -> None: ...
118+
def __init__(self, lock: Lock | _RLock | RLock | None = None) -> None: ...
127119
def __enter__(self) -> bool: ...
128120
def __exit__(
129121
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None

0 commit comments

Comments
 (0)