GeezerVison is showing github.com — feat: new mypyc primitive for weakref.ref (#19099) · python/mypy@07d4a1b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: mypyc/primitives/registry.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -371,4 +371,5 @@ def load_address_op(name: str, type: RType, src: str) -> LoadAddressDescription: | 371 | 371 | import mypyc.primitives.list_ops | 372 | 372 | import mypyc.primitives.misc_ops | 373 | 373 | import mypyc.primitives.str_ops | 374 | | -import mypyc.primitives.tuple_ops # noqa: F401 | | 374 | +import mypyc.primitives.tuple_ops | | 375 | +import mypyc.primitives.weakref_ops # noqa: F401 |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from mypyc.ir.ops import ERR_MAGIC | | 2 | +from mypyc.ir.rtypes import object_rprimitive, pointer_rprimitive | | 3 | +from mypyc.primitives.registry import function_op | | 4 | + | | 5 | +# Weakref operations | | 6 | + | | 7 | +new_ref_op = function_op( | | 8 | + name="weakref.ReferenceType", | | 9 | + arg_types=[object_rprimitive], | | 10 | + return_type=object_rprimitive, | | 11 | + c_function_name="PyWeakref_NewRef", | | 12 | + extra_int_constants=[(0, pointer_rprimitive)], | | 13 | + error_kind=ERR_MAGIC, | | 14 | +) | | 15 | + | | 16 | +new_ref__with_callback_op = function_op( | | 17 | + name="weakref.ReferenceType", | | 18 | + arg_types=[object_rprimitive, object_rprimitive], | | 19 | + return_type=object_rprimitive, | | 20 | + c_function_name="PyWeakref_NewRef", | | 21 | + error_kind=ERR_MAGIC, | | 22 | +) |
Collapse file Copy file name to clipboard +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +[case testWeakrefRef] | | 2 | +import weakref | | 3 | +from typing import Any, Callable | | 4 | +def f(x: object) -> object: | | 5 | + return weakref.ref(x) | | 6 | + | | 7 | +[out] | | 8 | +def f(x): | | 9 | + x, r0 :: object | | 10 | +L0: | | 11 | + r0 = PyWeakref_NewRef(x, 0) | | 12 | + return r0 | | 13 | + | | 14 | +[case testWeakrefRefCallback] | | 15 | +import weakref | | 16 | +from typing import Any, Callable | | 17 | +def f(x: object, cb: Callable[[object], Any]) -> object: | | 18 | + return weakref.ref(x, cb) | | 19 | + | | 20 | +[out] | | 21 | +def f(x, cb): | | 22 | + x, cb, r0 :: object | | 23 | +L0: | | 24 | + r0 = PyWeakref_NewRef(x, cb) | | 25 | + return r0 | | 26 | + | | 27 | +[case testFromWeakrefRef] | | 28 | +from typing import Any, Callable | | 29 | +from weakref import ref | | 30 | +def f(x: object) -> object: | | 31 | + return ref(x) | | 32 | + | | 33 | +[out] | | 34 | +def f(x): | | 35 | + x, r0 :: object | | 36 | +L0: | | 37 | + r0 = PyWeakref_NewRef(x, 0) | | 38 | + return r0 | | 39 | + | | 40 | +[case testFromWeakrefRefCallback] | | 41 | +from typing import Any, Callable | | 42 | +from weakref import ref | | 43 | +def f(x: object, cb: Callable[[object], Any]) -> object: | | 44 | + return ref(x, cb) | | 45 | + | | 46 | +[out] | | 47 | +def f(x, cb): | | 48 | + x, cb, r0 :: object | | 49 | +L0: | | 50 | + r0 = PyWeakref_NewRef(x, cb) | | 51 | + return r0 |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Test cases for weakrefs (compile and run) | | 2 | + | | 3 | +[case testWeakrefRef] | | 4 | +from weakref import ref | | 5 | +from mypy_extensions import mypyc_attr | | 6 | + | | 7 | +@mypyc_attr(native_class=False) | | 8 | +class Object: | | 9 | + """some random weakreffable object""" | | 10 | + pass | | 11 | + | | 12 | +def test_weakref_ref(): | | 13 | + obj = Object() | | 14 | + r = ref(obj) | | 15 | + assert r() is obj | | 16 | + obj = None | | 17 | + assert r() is None, r() | | 18 | + | | 19 | +def test_weakref_ref_with_callback(): | | 20 | + obj = Object() | | 21 | + r = ref(obj, lambda x: x) | | 22 | + assert r() is obj | | 23 | + obj = None | | 24 | + assert r() is None, r() | | 25 | + | | 26 | +[file driver.py] | | 27 | +from native import test_weakref_ref, test_weakref_ref_with_callback | | 28 | + | | 29 | +test_weakref_ref() | | 30 | +test_weakref_ref_with_callback() |
Collapse file Copy file name to clipboardExpand all lines: mypyc/test/test_irbuild.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 53 | 53 | "irbuild-constant-fold.test", | 54 | 54 | "irbuild-glue-methods.test", | 55 | 55 | "irbuild-math.test", | | 56 | + "irbuild-weakref.test", | 56 | 57 | ] | 57 | 58 |
| 58 | 59 | if sys.version_info >= (3, 10): | |
Collapse file Copy file name to clipboardExpand all lines: mypyc/test/test_run.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 70 | 70 | "run-singledispatch.test", | 71 | 71 | "run-attrs.test", | 72 | 72 | "run-signatures.test", | | 73 | + "run-weakref.test", | 73 | 74 | "run-python37.test", | 74 | 75 | "run-python38.test", | 75 | 76 | ] | |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from collections.abc import Callable | | 2 | +from typing import Any, Generic, TypeVar | | 3 | +from typing_extensions import Self | | 4 | + | | 5 | +_T = TypeVar("_T") | | 6 | + | | 7 | +class ReferenceType(Generic[_T]): # "weakref" | | 8 | + __callback__: Callable[[Self], Any] | | 9 | + def __new__(cls, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> Self: ... | | 10 | + | | 11 | +ref = ReferenceType |
|