Skip to content

Commit c14c3a4

Browse files
committed
feat: new primitives for weakref.proxy
1 parent 9eadb7e commit c14c3a4

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

mypyc/primitives/weakref_ops.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@
2020
c_function_name="PyWeakref_NewRef",
2121
error_kind=ERR_MAGIC,
2222
)
23+
24+
new_proxy_op = function_op(
25+
name="_weakref.proxy",
26+
arg_types=[object_rprimitive],
27+
return_type=object_rprimitive,
28+
c_function_name="PyWeakref_NewProxy",
29+
extra_int_constants=[(0, pointer_rprimitive)],
30+
error_kind=ERR_MAGIC,
31+
)
32+
33+
new_proxy_with_callback_op = function_op(
34+
name="_weakref.proxy",
35+
arg_types=[object_rprimitive, object_rprimitive],
36+
return_type=object_rprimitive,
37+
c_function_name="PyWeakref_NewProxy",
38+
error_kind=ERR_MAGIC,
39+
)

mypyc/test-data/run-weakref.test

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
[case testWeakrefRef]
44
import pytest # type: ignore [import-not-found]
5-
from weakref import ref
5+
from weakref import proxy, ref
66
from mypy_extensions import mypyc_attr
77

88
@mypyc_attr(native_class=False)
99
class Object:
1010
"""some random weakreffable object"""
11-
pass
11+
def some_meth(self) -> int:
12+
return 1
1213

1314
def test_weakref_ref():
1415
obj = Object()
@@ -23,3 +24,19 @@ def test_weakref_ref_with_callback():
2324
assert r() is obj
2425
obj = None
2526
assert r() is None, r()
27+
28+
def test_weakref_proxy():
29+
obj = Object()
30+
p = proxy(obj)
31+
assert p.some_meth() == 1
32+
obj = None
33+
with pytest.raises(ReferenceError):
34+
p.some_meth()
35+
36+
def test_weakref_proxy_with_callback():
37+
obj = Object()
38+
p = proxy(obj, lambda x: x)
39+
assert p.some_meth() == 1
40+
obj = None
41+
with pytest.raises(ReferenceError):
42+
p.some_meth()

0 commit comments

Comments
 (0)