Only allow to raise and catch exceptions deriving from BaseException #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi Matt,
I made some changes regarding exception handling. Previously you could raise anything at all and catch "real objects" (Object ObjectInfo) of any class. You can see that in the tests I added.
The behavior now matches the Python interpreter more closely and only allows raising and catching exceptions deriving from BaseException.
When digging around in the code I found some more things that seem to be not handled completely right. I think we agree that achieving perfectly identical behavior would make the code more complex and is not the goal of this project, but I will mention them briefly (for reference):
Evaluation (and check of correctness) of except clauses should happen only when an exception
was raised and could be caught by them.
E.g. this works fine in Python 3.5 (but not when changing the order of the two except clauses):
This means
ExceptionHandler
should contain anExpression
instead of aClassInfo
.Also, it is legal for this expression to be a tuple of multiple valid exception classes
(e.g.
except (SystemError, TypeError):
).Parsing:
xor_expr: and_expr ('^' and_expr)
should bexor_expr: (xor_expr '^') and_expr
(the same for and, or etc.). Currently1 ^ 2 ^ 3
is a parsing error.Parsing: In the language reference, there exists a separate
target
type, mainly for assignment.you can not assign to an
expression_list
, only to atarget_list
, which makes code like1 + 2 = "test"
a parsing error instead of having to handle it later.Optional function parameters should come in the end. Code like
def f(a = 1, b): print(a, b)
should not be allowed. Callingf(3)
causesPrelude.!!: index too large
.I also found a nice way to create an endless loop in the interpeter: Redefining exception types and causing an internal exception e.g.:
This is because internal exceptions are raised by the name of the exception.
raiseInternal
then looks up the corresponding class to create an exception object. If that fails, it raises an exception (and now the same thing happens again and again). :DRedefining existing classes is nothing you would normally do, though, so I see no need to fix it.
Matthias