Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

WIP: new mypyc primitive forweakref.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
BobTheBuidler wants to merge12 commits intopython:master
base:master
Choose a base branch
Loading
fromBobTheBuidler:weakref-call
Draft
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
12 commits
Select commitHold shift + click to select a range
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
NextNext commit
feat: new primitive for weakref.ref
  • Loading branch information
@BobTheBuidler
BobTheBuidler committedMay 23, 2025
commit6b28f17a59a7160eeaf73cdc7ffddf979a3083ce
3 changes: 2 additions & 1 deletionmypyc/primitives/registry.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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
13 changes: 13 additions & 0 deletionsmypyc/primitives/weakref_ops.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
from mypyc.ir.ops import ERR_MAGIC
from mypyc.ir.rtypes import object_rprimitive
from mypyc.primitives.registry import function_op

# Weakref operations

new_ref_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,
)
51 changes: 51 additions & 0 deletionsmypyc/test-data/irbuild-weakref.test
View file
Open in desktop
Original file line numberDiff line numberDiff 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, NULL)
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, NULL)
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
22 changes: 22 additions & 0 deletionsmypyc/test-data/run-weakref.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
# Test cases for weakrefs (compile and run)

[case testWeakrefRef]
import asyncio
import pytest # type: ignore [import-not-found]
from weakref import ref

def test_weakref_ref():
# some random weakreffable object
obj = asyncio.new_event_loop()
r = ref(obj)
assert r() is obj
obj = None
assert r() is None, r()

def test_weakref_ref_with_callback():
# some random weakreffable object
obj = asyncio.new_event_loop()
r = ref(obj, lambda x: x)
assert r() is obj
obj = None
assert r() is None, r()
1 change: 1 addition & 0 deletionsmypyc/test/test_irbuild.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsmypyc/test/test_run.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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
Loading

[8]ページ先頭

©2009-2025 Movatter.jp