Backport functionality from python 2.7 to support python2.4