[Python-Dev] Can Python implementations reject semantically invalid expressions?
Stefan Behnel
stefan_ml at behnel.de
Sat Jul 3 08:38:37 CEST 2010
Steven D'Aprano, 03.07.2010 06:35:
> On Sat, 3 Jul 2010 11:39:07 am Greg Ewing wrote:
>> Stefan Behnel wrote:
>>> So, would it still be Python if it folded
>>>
>>> 1 + "1"
>>>
>>> into
>>>
>>> raise TypeError()
>>>
>>> at compile time?
>>
>> It would have to be
>>
>> raise TypeError("Exactly the message that would have been
>> produced at run time")
>
> Python doesn't make any guarantees about the message that exceptions
> display, so I don't think you need to match the message, just the
> exception. Anyone testing for specific exception messages is living in
> a state of sin and shouldn't complain when their code stops working.
Yep, the Cython project keeps running into this all over the place. When we
reimplement CPython functionality for performance reasons, we make sure we
match the behaviour exactly, which usually means that we try to match the
exceptions and their messages as well. Since we use doctests for our test
suite, this means that we need to special case some Python versions in the
test suite to make sure we test our code as good as possible without
letting the tests break across CPython versions. This can be really tricky
at times.
The general trend seems to be that modified exception messages become more
exact and telling over time, so we tend to follow Python 3.x in our own
choice of error messages.
Stefan
More information about the Python-Dev
mailing list