Python anger

by havoc

Fighting with Python. Has anyone else noticed that it’s really hard to
figure out how to use the standard library? There aren’t type
declarations, the API conventions are all over the map, and
the docs are often pretty uninformative.

But the thing that really has me upset is this.
By default, the standard Python “print” statement will 1) convert to
ASCII and 2) throw a fatal exception if the conversion fails. Both 1)
and 2) are broken beyond words. On top of that, apparently the default
encoding will change from ASCII to locale encoding if stdout isatty(),
and not if you say redirect to a file; or something bizarre like
that. No! No! No! Cluebat!

To fix it you have to do this:

sys.stdout = codecs.getwriter("utf-8")(sys.__stdout__)

Which of course is impossible to figure out from the docs, though
Google turned it up.

Before stumbling on this I spent a lot of time trying to convert
_parse_response() in to handle invalid UTF-8 in the
stream. The API docs sound promising (with
ignore/replace/xmlreplacecharrefs/etc. error handlers), but heck if I
can find the right magic.

(This post was originally found at

My Twitter account is @havocp.
Interested in becoming a better software developer? Sign up for my email list and I'll let you know when I write something new.
Email *