Implement passing of kwarg
This commit is contained in:
parent
79931365b7
commit
ec2787aaf6
@ -114,26 +114,13 @@ def extern(function):
|
|||||||
|
|
||||||
|
|
||||||
def rpc(arg=None, flags={}):
|
def rpc(arg=None, flags={}):
|
||||||
"""Decorates a function to be executed on the host interpreter with kwargs support."""
|
"""Decorates a function or method to be executed on the host interpreter."""
|
||||||
def decorator(function):
|
|
||||||
@wraps(function)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
# Get function signature
|
|
||||||
sig = inspect.signature(function)
|
|
||||||
|
|
||||||
# Validate kwargs against signature
|
|
||||||
bound_args = sig.bind(*args, **kwargs)
|
|
||||||
bound_args.apply_defaults()
|
|
||||||
|
|
||||||
# Call RPC with both args and kwargs
|
|
||||||
return _do_rpc(function.__name__,
|
|
||||||
bound_args.args,
|
|
||||||
bound_args.kwargs)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
if arg is None:
|
if arg is None:
|
||||||
return decorator
|
def inner_decorator(function):
|
||||||
return decorator(arg)
|
return rpc(function, flags)
|
||||||
|
return inner_decorator
|
||||||
|
register_function(arg)
|
||||||
|
return arg
|
||||||
|
|
||||||
def kernel(function_or_method):
|
def kernel(function_or_method):
|
||||||
"""Decorates a function or method to be executed on the core device."""
|
"""Decorates a function or method to be executed on the core device."""
|
||||||
|
41
nac3artiq/demo/rpc_kwargs_test.py
Normal file
41
nac3artiq/demo/rpc_kwargs_test.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
from min_artiq import *
|
||||||
|
from numpy import int32
|
||||||
|
|
||||||
|
@rpc
|
||||||
|
def sum_3(a: int32, b: int32 = 10, c: int32 = 20) -> int32:
|
||||||
|
"""
|
||||||
|
An RPC function to test NAC3's handling of positional/keyword arguments.
|
||||||
|
"""
|
||||||
|
return int32(a + b + c)
|
||||||
|
|
||||||
|
@nac3
|
||||||
|
class RpcKwargTest:
|
||||||
|
core: KernelInvariant[Core]
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.core = Core()
|
||||||
|
|
||||||
|
@kernel
|
||||||
|
def run(self):
|
||||||
|
#1) All positional => a=1, b=2, c=3 -> total=6
|
||||||
|
s1 = sum_3(1, 2, 3)
|
||||||
|
if s1 != 6:
|
||||||
|
raise ValueError("sum_3(1,2,3) gave the wrong result.")
|
||||||
|
|
||||||
|
#2) Use the default b=10, c=20 => a=5 => total=35
|
||||||
|
s2 = sum_3(5)
|
||||||
|
if s2 != 35:
|
||||||
|
raise ValueError("sum_3(5) gave the wrong result.")
|
||||||
|
|
||||||
|
#3) a=1 (positional), b=100 (keyword), omit c => c=20 => total=121
|
||||||
|
s3 = sum_3(1, b=100)
|
||||||
|
if s3 != 121:
|
||||||
|
raise ValueError("sum_3(1, b=100) gave the wrong result.")
|
||||||
|
|
||||||
|
#4) a=2, c=300 => b=10 (default) => total=312
|
||||||
|
s4 = sum_3(a=2, c=300)
|
||||||
|
if s4 != 312:
|
||||||
|
raise ValueError("sum_3(a=2, c=300) gave the wrong result.")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
RpcKwargTest().run()
|
Loading…
Reference in New Issue
Block a user