-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9eadb7e
feat: new primitive for weakref.ref
BobTheBuidler c16342b
Merge branch 'master' into patch-3
BobTheBuidler 2eb743c
Merge branch 'master' into patch-3
BobTheBuidler f19b55b
Update run-weakref.test
BobTheBuidler 9ebe2e7
Merge branch 'master' into patch-3
BobTheBuidler c2f68f1
Update run-weakref.test
BobTheBuidler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
error_kind=ERR_MAGIC, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Test cases for weakrefs (compile and run) | ||
|
||
[case testWeakrefRef] | ||
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() | ||
|
||
[file driver.py] | ||
from native import test_weakref_ref, test_weakref_ref_with_callback | ||
|
||
test_weakref_ref() | ||
test_weakref_ref_with_callback() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from collections.abc import Callable | ||
from typing import Any, Generic, TypeVar | ||
from typing_extensions import Self | ||
|
||
_T = TypeVar("_T") | ||
|
||
class ReferenceType(Generic[_T]): # "weakref" | ||
__callback__: Callable[[Self], Any] | ||
def __new__(cls, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> Self: ... | ||
|
||
ref = ReferenceType |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.