XanaduAI / blackbird

Blackbird is a quantum assembly language for continuous-variable quantum computation, that can be used to program Xanadu's quantum photonics hardware and Strawberry Fields simulator.
https://quantum-blackbird.readthedocs.io
Apache License 2.0
70 stars 26 forks source link

blackbird_python: Parse errors raise inappropriate SystemExit #15

Closed jswinarton closed 5 years ago

jswinarton commented 5 years ago

Calls to blackbird.load or blackbird.loads that pass in a malformed Blackbird script raise a SystemExit error. This is a problem because SystemExit is a special error class that causes the Python program to quit, which can have unintended consequences. For example, running blackbird.loads('invalid script') in a python REPL causes the REPL to quit, whereas other exception classes print the traceback and allow the user to continue. This could have other unintentional effects when used with other software.

The ideal solution would be to define a base BlackbirdException class that all blackbird errors inherit from, and then define a BlackbirdParseError that derives from BlackbirdException.

co9olguy commented 5 years ago

Thanks @jswinarton, good catch :+1:

josh146 commented 5 years ago

It should be raising the defined BlackbirdSyntaxError, but I was trying to be a bit too clever and replace the default python traceback, leading to the error handler wrapping the custom exceptions in calls to sys.exit().

See here for the problematic code section:

https://github.com/XanaduAI/blackbird/blob/8d5177d8492bee35f629beeb0309647d13cd7bec/blackbird_python/blackbird/error.py#L71

I'll remove this behavior in a PR.

josh146 commented 5 years ago

Fixed via #16