Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@cocotb.test(expect_error=cocotb.triggers.TriggerException if cocotb.SIM_NAME.startswith(("xmsim", "ncsim")) and cocotb.LANGUAGE in ["vhdl"] else False)
async def test_await_after_function(dut):
"""
Test that awaiting a Trigger works after returning
from @external functions that call @functions that consume
simulation time
"""
clk_gen = cocotb.fork(Clock(dut.clk, 100, units='ns').start())
value = await external(calls_cocotb_function)(dut)
assert value == 2
await Timer(10, units="ns")
await RisingEdge(dut.clk)
def get_value():
yield cocotb.triggers.Timer(1, units='ns')
raise cocotb.result.ReturnValue(42)
async def example():
nonlocal ran
await cocotb.triggers.Timer(1, 'ns')
ran = True
def raise_soon():
yield Timer(1)
yield cocotb.triggers.First(raise_inner())
def test_bad_attr(dut):
yield cocotb.triggers.NullTrigger()
try:
_ = dut.stream_in_data.whoops
except AttributeError as e:
assert 'whoops' in str(e)
else:
assert False, "Expected AttributeError"
A single object that could be right of an :keyword:`await` expression in cocotb.
timeout_time (numbers.Real or decimal.Decimal):
Simulation time duration before timeout occurs.
timeout_unit (str or None, optional):
Units of timeout_time, accepts any units that :class:`~cocotb.triggers.Timer` does.
Returns:
First trigger that completed if timeout did not occur.
Raises:
:exc:`SimTimeoutError`: If timeout occurs.
.. versionadded:: 1.3
"""
timeout_timer = cocotb.triggers.Timer(timeout_time, timeout_unit)
res = await First(timeout_timer, trigger)
if res is timeout_timer:
raise cocotb.result.SimTimeoutError
else:
return res
async def f(*args, **kwargs):
running_co = co(*args, **kwargs)
try:
res = await cocotb.triggers.with_timeout(running_co, self.timeout_time, self.timeout_unit)
except cocotb.result.SimTimeoutError:
running_co.kill()
raise
else:
return res
if isinstance(result, Trigger):
return result
if isinstance(result, cocotb.decorators.RunningTask):
if not result.has_started():
return self._trigger_from_unstarted_coro(result)
else:
return self._trigger_from_started_coro(result)
if inspect.iscoroutine(result):
return self._trigger_from_unstarted_coro(cocotb.decorators.RunningTask(result))
if isinstance(result, list):
return self._trigger_from_list(result)
if isinstance(result, cocotb.triggers.Waitable):
return self._trigger_from_waitable(result)
if sys.version_info >= (3, 6) and inspect.isasyncgen(result):
raise TypeError(
"{} is an async generator, not a coroutine. "
"You likely used the yield keyword instead of await.".format(
result.__qualname__))
raise TypeError(
"Coroutine yielded an object of type {}, which the scheduler can't "
"handle: {!r}\n"
"Did you forget to decorate with @cocotb.coroutine?"
.format(type(result), result)
)
def join(self):
"""Return a trigger that will fire when the wrapped coroutine exits."""
return cocotb.triggers.Join(self)
def _trigger_from_list(self, result: list) -> Trigger:
return self._trigger_from_waitable(cocotb.triggers.First(*result))