Stop writing Python 3.5 incompatible code :-)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jan 14 03:09:25 EST 2016


On Thursday 14 January 2016 17:27, Frank Millman wrote:

> So my test was -
> 
> except ValueError as e:
>     if str(e).startswith('need'):
>         # 0 rows returned
>     else:
>         # > 1 rows returned
> 
> This has worked for years, but with 3.5 it stopped working. It took me a
> while to figure out why. Lo and behold, the error message has changed!

Error message strings have *never* been part of the function API. They are 
always subject to change without notice. Somebody might localise them to the 
user's native language, or merely change them on a whim, and they can do 
that in maintenance releases too, not just minor or major releases.



> Now, if 0 rows are returned, the message is 'not enough values to unpack'.
> 
> Luckily the other message has not changed,

Note to self: push through a minor change to the error message. Perhaps make 
it "too many targets for the number of items"?


> so now my test is -
> 
> except ValueError as e:
>     if str(e).startswith('too many'):
>         # > 1 rows returned
>     else:
>         # 0 rows returned
> 
> Now it works with both versions.

For now.


-- 
Steve




More information about the Python-list mailing list