Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

WIP: new mypyc primitive for weakref.ref.__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
Loading
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: new primitive for weakref.__call__
  • Loading branch information
BobTheBuidler committed May 23, 2025
commit 3b71a7f71d1ede3ff858285c68d9cce43fa02ed0
10 changes: 9 additions & 1 deletion 10 mypyc/primitives/weakref_ops.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from mypyc.ir.ops import ERR_MAGIC
from mypyc.ir.rtypes import object_rprimitive
from mypyc.primitives.registry import function_op
from mypyc.primitives.registry import ERR_NEG_INT, function_op

# Weakref operations

Expand All @@ -11,3 +11,11 @@
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,
)
14 changes: 8 additions & 6 deletions 14 mypyc/test-data/irbuild-weakref.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
import weakref
from typing import Any, Callable
def f(x: object, cb: Callable[[object], Any]) -> object:
return weakref.ref(x, cb)
return weakref.ref(x, cb)()

[out]
def f(x, cb):
x, cb, r0 :: object
x, cb, r0, r1 :: object
L0:
r0 = PyWeakref_NewRef(x, cb)
return r0
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)
return ref(x, cb)()

[out]
def f(x, cb):
x, cb, r0 :: object
x, cb, r0, r1 :: object
L0:
r0 = PyWeakref_NewRef(x, cb)
return r0
r1 = PyWeakref_GetRef(r0)
return r1
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.