Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
bargs = sig.bind(*args, **kwargs)
except TypeError as exc:
raise ParametersError(repr(exc)) from exc
else:
arguments = bargs.arguments
marker = object()
for name, param in sig.parameters.items():
if param.annotation is param.empty:
continue
val = arguments.get(name, marker)
if val is marker:
continue # Skip default value
try:
arguments[name] = param.annotation(val)
except (TypeError, ValueError) as exc:
raise ParametersError(
'Invalid value for argument {!r}: {!r}'
.format(name, exc)) from exc
if sig.return_annotation is not sig.empty:
return bargs.args, bargs.kwargs, sig.return_annotation
return bargs.args, bargs.kwargs, None
async def RPCContext(addr, timeout=None):
preserved_exceptions = (
NotFoundError,
ParametersError,
asyncio.TimeoutError,
asyncio.CancelledError,
asyncio.InvalidStateError,
)
global agent_peers
peer = agent_peers.get(addr, None)
if peer is None:
peer = await aiozmq.rpc.connect_rpc(
connect=addr, error_table={
'concurrent.futures._base.TimeoutError': asyncio.TimeoutError,
})
peer.transport.setsockopt(zmq.LINGER, 1000)
agent_peers[addr] = peer
try:
with _timeout(timeout):
yield peer
def check_args(self, func, args, kwargs):
"""Utility function for validating function arguments
Returns validated (args, kwargs, return annotation) tuple
"""
try:
sig = inspect.signature(func)
bargs = sig.bind(*args, **kwargs)
except TypeError as exc:
raise ParametersError(repr(exc)) from exc
else:
arguments = bargs.arguments
marker = object()
for name, param in sig.parameters.items():
if param.annotation is param.empty:
continue
val = arguments.get(name, marker)
if val is marker:
continue # Skip default value
try:
arguments[name] = param.annotation(val)
except (TypeError, ValueError) as exc:
raise ParametersError(
'Invalid value for argument {!r}: {!r}'
.format(name, exc)) from exc
if sig.return_annotation is not sig.empty:
async def RPCContext(addr, timeout=10):
preserved_exceptions = (
NotFoundError,
ParametersError,
asyncio.TimeoutError,
asyncio.CancelledError,
asyncio.InvalidStateError,
)
server = None
try:
server = await aiozmq.rpc.connect_rpc(
connect=addr, error_table={
'concurrent.futures._base.TimeoutError': asyncio.TimeoutError,
})
server.transport.setsockopt(zmq.LINGER, 50)
with _timeout(timeout):
yield server
except Exception:
exc_type, exc, tb = sys.exc_info()
if issubclass(exc_type, GenericError):
def msg_received(self, data):
bname, bargs, bkwargs = data
args = self.packer.unpackb(bargs)
kwargs = self.packer.unpackb(bkwargs)
try:
name = bname.decode('utf-8')
func = self.dispatch(name)
args, kwargs, ret_ann = self.check_args(func, args, kwargs)
except (NotFoundError, ParametersError) as exc:
fut = asyncio.Future(loop=self.loop)
fut.set_exception(exc)
else:
if asyncio.iscoroutinefunction(func):
fut = self.add_pending(func(*args, **kwargs))
else:
fut = asyncio.Future(loop=self.loop)
try:
fut.set_result(func(*args, **kwargs))
except Exception as exc:
fut.set_exception(exc)
fut.add_done_callback(partial(self.process_call_result,
name=name, args=args, kwargs=kwargs))