Skip to content

feat: new mypyc primitive for weakref.ref #19099

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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6b28f17
feat: new primitive for weakref.ref
BobTheBuidler May 23, 2025
8d41c26
fix(test): fix test ref
BobTheBuidler May 23, 2025
97f9186
fix(test): only test with callback
BobTheBuidler May 23, 2025
81eadfb
Update run-weakref.test
BobTheBuidler May 23, 2025
50b9597
Merge branch 'master' into patch-3
BobTheBuidler May 30, 2025
f780828
Merge branch 'master' into patch-3
BobTheBuidler May 30, 2025
8ca882c
Merge branch 'master' into patch-3
BobTheBuidler May 31, 2025
67bcf4b
Merge branch 'master' into patch-3
BobTheBuidler Jun 2, 2025
b05d220
Merge branch 'master' into patch-3
BobTheBuidler Jun 2, 2025
1ae55e4
feat: add primitive for weakref.ref with 1 arg
BobTheBuidler Jun 2, 2025
216df5e
Update irbuild-weakref.test
BobTheBuidler Jun 2, 2025
180c98c
Update run-weakref.test
BobTheBuidler Jun 2, 2025
e0b20f5
Update irbuild-weakref.test
BobTheBuidler Jun 2, 2025
c0f9bd2
Update irbuild-weakref.test
BobTheBuidler Jun 2, 2025
975d91a
Merge branch 'master' into patch-3
BobTheBuidler Jun 3, 2025
83cb39f
Update weakref_ops.py
BobTheBuidler Jun 3, 2025
9e1375d
Update weakref_ops.py
BobTheBuidler Jun 3, 2025
6ec984e
Merge branch 'master' into patch-3
BobTheBuidler Jun 3, 2025
a15838c
Merge branch 'master' into patch-3
BobTheBuidler Jun 3, 2025
1da9046
Merge branch 'master' into patch-3
BobTheBuidler Jun 4, 2025
ec5d4c6
Merge branch 'master' into patch-3
BobTheBuidler Jun 5, 2025
806a27d
Merge branch 'master' into patch-3
BobTheBuidler Jun 5, 2025
34e2f74
Merge branch 'master' into patch-3
BobTheBuidler Jun 7, 2025
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
22 changes: 22 additions & 0 deletions mypyc/primitives/weakref_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from mypyc.ir.ops import ERR_MAGIC
from mypyc.ir.rtypes import object_rprimitive, pointer_rprimitive
from mypyc.primitives.registry import function_op

# Weakref operations

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

new_ref__with_callback_op = function_op(
name="weakref.ReferenceType",
arg_types=[object_rprimitive, object_rprimitive],
return_type=object_rprimitive,
c_function_name="PyWeakref_NewRef",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also support calls with a single argument, since this is probably quite common?

You'd need to add another function_op with arg_types=[object_rprimitive], and probably something like extra_int_constants=[(0, pointer_rprimitive)], to add an implicit NULL argument.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all makes sense to me, the pointer_rprimitive would become the NULL (i think?) but what does the 0 do in your above example?

Copy link
Author

@BobTheBuidler BobTheBuidler Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though I don't understand it, it seems to be working. Is it an index? as in 'for extra int constant in slot 0, use pointer_rprimitive'?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 is the constant value of the argument (zero i.e. NULL).

error_kind=ERR_MAGIC,
)
51 changes: 51 additions & 0 deletions mypyc/test-data/irbuild-weakref.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[case testWeakrefRef]
import weakref
from typing import Any, Callable
def f(x: object) -> object:
return weakref.ref(x)

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

[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 :: object
L0:
r0 = PyWeakref_NewRef(x, cb)
return r0

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

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

[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 :: object
L0:
r0 = PyWeakref_NewRef(x, cb)
return r0
26 changes: 26 additions & 0 deletions mypyc/test-data/run-weakref.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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():
obj = Object()
r = ref(obj)
assert r() is obj
obj = None
assert r() is None, r()

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