Skip to content

Weakref call #19145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypyc/primitives/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,5 @@ def load_address_op(name: str, type: RType, src: str) -> LoadAddressDescription:
import mypyc.primitives.list_ops
import mypyc.primitives.misc_ops
import mypyc.primitives.str_ops
import mypyc.primitives.tuple_ops # noqa: F401
import mypyc.primitives.tuple_ops
import mypyc.primitives.weakref_ops # noqa: F401
21 changes: 21 additions & 0 deletions mypyc/primitives/weakref_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from mypyc.ir.ops import ERR_MAGIC
from mypyc.ir.rtypes import object_rprimitive
from mypyc.primitives.registry import ERR_NEG_INT, function_op

# Weakref operations

new_ref_op = function_op(
name="weakref.ReferenceType",
arg_types=[object_rprimitive, object_rprimitive],
return_type=object_rprimitive,
c_function_name="PyWeakref_NewRef",
error_kind=ERR_MAGIC,
)

deref_op = function_op(
name="weakref.ReferenceType.__call__",
arg_types=[object_rprimitive],
return_type=object_rprimitive,
c_function_name="PyWeakref_GetRef",
error_kind=ERR_NEG_INT,
)
27 changes: 27 additions & 0 deletions mypyc/test-data/irbuild-weakref.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[case testWeakrefRefCallback]
import weakref
from typing import Any, Callable
def f(x: object, cb: Callable[[object], Any]) -> object:
return weakref.ref(x, cb)()

[out]
def f(x, cb):
x, cb, r0, r1 :: object
L0:
r0 = PyWeakref_NewRef(x, cb)
r1 = PyWeakref_GetRef(r0)
return r1

[case testFromWeakrefRefCallback]
from typing import Any, Callable
from weakref import ref
def f(x: object, cb: Callable[[object], Any]) -> object:
return ref(x, cb)()

[out]
def f(x, cb):
x, cb, r0, r1 :: object
L0:
r0 = PyWeakref_NewRef(x, cb)
r1 = PyWeakref_GetRef(r0)
return r1
19 changes: 19 additions & 0 deletions mypyc/test-data/run-weakref.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Test cases for weakrefs (compile and run)

[case testWeakrefRef]
import asyncio
import pytest # type: ignore [import-not-found]
from weakref import ref
from mypy_extensions import mypyc_attr

@mypyc_attr(native_class=False)
class Object:
"""some random weakreffable object"""
pass

def test_weakref_ref_with_callback():
obj = Object()
r = ref(obj, lambda x: x)
assert r() is obj
obj = None
assert r() is None, r()
1 change: 1 addition & 0 deletions mypyc/test/test_irbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"irbuild-constant-fold.test",
"irbuild-glue-methods.test",
"irbuild-math.test",
"irbuild-weakref.test",
]

if sys.version_info >= (3, 10):
Expand Down
1 change: 1 addition & 0 deletions mypyc/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"run-dunders-special.test",
"run-singledispatch.test",
"run-attrs.test",
"run-weakref.test",
"run-python37.test",
"run-python38.test",
]
Expand Down
Loading