Applied patch mentioned in #227.

Didn't apply cleanly so had to do it manually. Hopefully it didn't break
much.

Related: http://bugs.python.org/issue17997