Much of this is formatting to make pylint happy but it also fixes some real bugs.
Python 2.6 changed its internal implementation which makes it difficult to override in a way that is backwards compatible. 508953