Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw TypeError instead of terminating the simulation #985

Merged
merged 2 commits into from
Jul 2, 2019

Conversation

eric-wieser
Copy link
Member

Previously, doing all of these things would cause the simulation to terminate:

  • yield bad_obj
  • cocotb.fork(bad_obj)
  • Invoking a function decorated with @cocotb.coroutine that does not produce a generator (typically: contains no yields)

Instead, these operations now throw an exception at the point where they occur, meaning the user gets a proper stack trace, and can still perform cleanup in a try / finally.

This extends NullTrigger to allow its result to be overriden, which is used here to throw an exception at a yield point.

"""Example of trying to fork a coroutine that isn't a coroutine"""
yield Timer(500)
cocotb.fork(function_not_a_coroutine())
yield Timer(500)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was uninteresting, the error happened before fork was even called.

Moved it to below, where it is interesting

@@ -709,18 +707,12 @@ def schedule(self, coroutine, trigger=None):

try:
result = self._trigger_from_any(result)
except TypeError:
msg = ("Coroutine %s yielded something the scheduler can't handle"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to include the name of the coroutine in the message any more, it appears in the stack trace, along with the line that performed the bad yield.

@eric-wieser eric-wieser changed the title Thow TypeError instead of terminating the simulation Throw TypeError instead of terminating the simulation Jul 1, 2019
Copy link
Member

@imphil imphil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a couple of nits in the error messages to make them even more useful to the user. This is a great improvement to usability, thanks a lot for this @eric-wieser!

cocotb/decorators.py Show resolved Hide resolved
cocotb/scheduler.py Outdated Show resolved Hide resolved
cocotb/scheduler.py Outdated Show resolved Hide resolved
cocotb/scheduler.py Outdated Show resolved Hide resolved
Previously, doing all of these things would cause the simulation to terminate:
* `yield bad_obj`
* `cocotb.fork(bad_obj)`
* Invoking a function decorated with `@cocotb.coroutine` that does not produce a generator (typically: contains no yields)
* A trigger failing to prime

Instead, these operations now throw an exception at the point where they occur, meaning the user gets a proper stack trace, and can still perform cleanup in a try / finally.

This extends `NullTrigger` to allow its result to be overriden, which is used here to throw an exception at a yield point.
@eric-wieser
Copy link
Member Author

Updated with a second commit that improves the affected error messages.

buff = StringIO(buff_bytes.getvalue().decode("UTF-8"))
result.stderr.write(buff.getvalue())
raise result
return RunningCoroutine(self._func(*args, **kwargs), self)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming in a future PR: Making the same simplification to test.__call__

@eric-wieser
Copy link
Member Author

You tricked me, I need to update the tests now too

@imphil imphil merged commit bccadbf into cocotb:master Jul 2, 2019
@imphil
Copy link
Member

imphil commented Jul 2, 2019

Thanks for going the extra mile on this!

@imphil imphil added this to the 1.2 milestone Jul 2, 2019
@eric-wieser eric-wieser deleted the bad-yield-as-exception branch July 3, 2019 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants