Skip to content

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 4 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
5 changes: 5 additions & 0 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ def __hash__(self) -> int:
# Python range object.
range_rprimitive: Final = RPrimitive("builtins.range", is_unboxed=False, is_refcounted=True)

# Python weak reference object
weakref_rprimitive: Final = RPrimitive(
"weakref.ReferenceType", is_unboxed=False, is_refcounted=True
)


def is_tagged(rtype: RType) -> bool:
return rtype is int_rprimitive or rtype is short_int_rprimitive
Expand Down
16 changes: 12 additions & 4 deletions mypyc/primitives/weakref_ops.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from mypyc.ir.ops import ERR_MAGIC
from mypyc.ir.rtypes import object_rprimitive, pointer_rprimitive
from mypyc.primitives.registry import function_op
from mypyc.ir.rtypes import object_rprimitive, pointer_rprimitive, weakref_rprimitive
from mypyc.primitives.registry import ERR_NEG_INT, function_op, method_op

# Weakref operations

new_ref_op = function_op(
name="weakref.ReferenceType",
arg_types=[object_rprimitive],
return_type=object_rprimitive,
return_type=weakref_rprimitive,
c_function_name="PyWeakref_NewRef",
extra_int_constants=[(0, pointer_rprimitive)],
error_kind=ERR_MAGIC,
Expand All @@ -16,7 +16,15 @@
new_ref__with_callback_op = function_op(
name="weakref.ReferenceType",
arg_types=[object_rprimitive, object_rprimitive],
return_type=object_rprimitive,
return_type=weakref_rprimitive,
c_function_name="PyWeakref_NewRef",
error_kind=ERR_MAGIC,
)

deref_op = method_op(
name="__call__",
arg_types=[weakref_rprimitive],
return_type=object_rprimitive,
c_function_name="PyWeakref_GetRef",
error_kind=ERR_NEG_INT,
)
36 changes: 24 additions & 12 deletions mypyc/test-data/irbuild-weakref.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,62 @@
import weakref
from typing import Any, Callable
def f(x: object) -> object:
return weakref.ref(x)
return weakref.ref(x)()

[out]
def f(x):
x, r0 :: object
x :: object
r0 :: weakref.ReferenceType
r1 :: object
L0:
r0 = PyWeakref_NewRef(x, 0)
return r0
r1 = PyWeakref_GetRef(r0)
return r1

[case testWeakrefRefCallback]
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 :: object
r0 :: weakref.ReferenceType
r1 :: object
L0:
r0 = PyWeakref_NewRef(x, cb)
return r0
r1 = PyWeakref_GetRef(r0)
return r1

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

[out]
def f(x):
x, r0 :: object
x :: object
r0 :: weakref.ReferenceType
r1 :: object
L0:
r0 = PyWeakref_NewRef(x, 0)
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 :: object
r0 :: weakref.ReferenceType
r1 :: object
L0:
r0 = PyWeakref_NewRef(x, cb)
return r0
r1 = PyWeakref_GetRef(r0)
return r1
1 change: 1 addition & 0 deletions test-data/unit/lib-stub/weakref.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ _T = TypeVar("_T")
class ReferenceType(Generic[_T]): # "weakref"
__callback__: Callable[[Self], Any]
def __new__(cls, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> Self: ...
def __call__(self) -> _T: ...

ref = ReferenceType
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy