Backport stuff from Python 2.7 so we can remain compatible with 2.6