-
-
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
Open
BobTheBuidler
wants to merge
23
commits into
python:master
Choose a base branch
from
BobTheBuidler:patch-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−1
Open
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 8d41c26
fix(test): fix test ref
BobTheBuidler 97f9186
fix(test): only test with callback
BobTheBuidler 81eadfb
Update run-weakref.test
BobTheBuidler 50b9597
Merge branch 'master' into patch-3
BobTheBuidler f780828
Merge branch 'master' into patch-3
BobTheBuidler 8ca882c
Merge branch 'master' into patch-3
BobTheBuidler 67bcf4b
Merge branch 'master' into patch-3
BobTheBuidler b05d220
Merge branch 'master' into patch-3
BobTheBuidler 1ae55e4
feat: add primitive for weakref.ref with 1 arg
BobTheBuidler 216df5e
Update irbuild-weakref.test
BobTheBuidler 180c98c
Update run-weakref.test
BobTheBuidler e0b20f5
Update irbuild-weakref.test
BobTheBuidler c0f9bd2
Update irbuild-weakref.test
BobTheBuidler 975d91a
Merge branch 'master' into patch-3
BobTheBuidler 83cb39f
Update weakref_ops.py
BobTheBuidler 9e1375d
Update weakref_ops.py
BobTheBuidler 6ec984e
Merge branch 'master' into patch-3
BobTheBuidler a15838c
Merge branch 'master' into patch-3
BobTheBuidler 1da9046
Merge branch 'master' into patch-3
BobTheBuidler ec5d4c6
Merge branch 'master' into patch-3
BobTheBuidler 806a27d
Merge branch 'master' into patch-3
BobTheBuidler 34e2f74
Merge branch 'master' into patch-3
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,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() |
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
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.
There was a problem hiding this comment.
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
witharg_types=[object_rprimitive],
and probably something likeextra_int_constants=[(0, pointer_rprimitive)],
to add an implicit NULL argument.There was a problem hiding this comment.
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?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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'?
There was a problem hiding this comment.
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).