Skip to content

Commit 8b3e7d8

Browse files
authored
[mypyc] Fix librt.threading.Lock runtime type checks (#21876)
The definition of `CPyLock_Check` was missing.
1 parent d1173b8 commit 8b3e7d8

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

mypyc/lib-rt/threading/librt_threading_api.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef LIBRT_THREADING_API_H
22
#define LIBRT_THREADING_API_H
33

4+
#include <stdbool.h>
5+
46
#include "librt_threading.h"
57

68
int
@@ -17,4 +19,8 @@ extern void *LibRTThreading_API[LIBRT_THREADING_API_LEN];
1719
#define LibRTThreading_Lock_locked_internal (*(char (*)(PyObject *self)) LibRTThreading_API[6])
1820
#define LibRTThreading_Lock_acquire_blocking_internal (*(char (*)(PyObject *self, char blocking)) LibRTThreading_API[7])
1921

22+
static inline bool CPyLock_Check(PyObject *obj) {
23+
return Py_TYPE(obj) == LibRTThreading_Lock_type_internal();
24+
}
25+
2026
#endif // LIBRT_THREADING_API_H

mypyc/test-data/run-threading.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class BadBool:
99
def __bool__(self) -> bool:
1010
raise RuntimeError("bad bool")
1111

12+
class MutableLockHolder:
13+
def __init__(self) -> None:
14+
self.lock = Lock()
15+
1216
def test_lock_basic() -> None:
1317
lock = Lock()
1418
assert not lock.locked()
@@ -24,6 +28,22 @@ def test_lock_context_manager() -> None:
2428
assert lock.locked()
2529
assert not lock.locked()
2630

31+
def test_mutable_lock_attribute() -> None:
32+
mutable_holder = MutableLockHolder()
33+
assert not mutable_holder.lock.locked()
34+
with mutable_holder.lock:
35+
assert mutable_holder.lock.locked()
36+
assert not mutable_holder.lock.locked()
37+
38+
dynamic_holder: Any = mutable_holder
39+
replacement = Lock()
40+
dynamic_holder.lock = replacement
41+
assert mutable_holder.lock is replacement
42+
with mutable_holder.lock:
43+
assert replacement.locked()
44+
with assertRaises(TypeError):
45+
dynamic_holder.lock = object()
46+
2747
def test_lock_non_blocking() -> None:
2848
lock = Lock()
2949
assert lock.acquire()

0 commit comments

Comments
 (0)