Skip to content

Commit 6b8490f

Browse files
Update specialize.py
1 parent 6c839a7 commit 6b8490f

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

mypyc/irbuild/specialize.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@
9696
str_encode_utf8_strict,
9797
)
9898
from mypyc.primitives.tuple_ops import new_tuple_set_item_op
99-
from mypyc.primitives.weakref_ops import new_proxy_op
99+
from mypyc.primitives.weakref_ops import (
100+
new_proxy_op,
101+
new_ref_op,
102+
)
100103

101104
# Specializers are attempted before compiling the arguments to the
102105
# function. Specializers can return None to indicate that they failed
@@ -891,29 +894,51 @@ def translate_ord(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
891894
return None
892895

893896

894-
@specialize_function("weakref.proxy")
897+
@specialize_function("weakref.ReferenceType")
895898
def translate_proxy(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
896899
"""
897-
Special case for weakref.proxy(...).
900+
Special case for weakref.ref(...).
898901
899902
If there's only one argument (the object), pass None for the callback.
900903
If there are two arguments, use them as (object, callback).
901904
Otherwise, fallback to a normal Python call by returning None.
902905
"""
903-
line = expr.line
906+
obj_val = builder.accept(expr.args[0])
904907
num_args = len(expr.args)
905-
906-
# If the user supplied something other than 1 or 2 args, just fail
907-
# and let MyPyC do a generic Python call.
908-
if num_args not in (1, 2):
908+
if num_args == 1:
909+
callback_val = builder.none_object()
910+
elif num_args == 2:
911+
callback_val = builder.accept(expr.args[1])
912+
else:
913+
# If the user supplied something other than 1 or 2 args, just fail
914+
# and let MyPyC do a generic Python call.
909915
# NOTE: should we raise a compiler error here?
910916
return None
911917

918+
# Emit a direct C call to PyWeakref_NewProxy
919+
return builder.call_c(new_ref_op, [obj_val, callback_val], expr.line)
920+
921+
922+
@specialize_function("weakref.proxy")
923+
def translate_proxy(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
924+
"""
925+
Special case for weakref.proxy(...).
926+
927+
If there's only one argument (the object), pass None for the callback.
928+
If there are two arguments, use them as (object, callback).
929+
Otherwise, fallback to a normal Python call by returning None.
930+
"""
912931
obj_val = builder.accept(expr.args[0])
932+
num_args = len(expr.args)
913933
if num_args == 1:
914934
callback_val = builder.none_object()
915-
else:
935+
elif num_args == 2:
916936
callback_val = builder.accept(expr.args[1])
937+
else:
938+
# If the user supplied something other than 1 or 2 args, just fail
939+
# and let MyPyC do a generic Python call.
940+
# NOTE: should we raise a compiler error here?
941+
return None
917942

918943
# Emit a direct C call to PyWeakref_NewProxy
919-
return builder.call_c(new_proxy_op, [obj_val, callback_val], line)
944+
return builder.call_c(new_proxy_op, [obj_val, callback_val], expr.line)

0 commit comments

Comments
 (0)