Pulled in some formatting fixes and code-style improvements from the patch
in #7027, plus made some addition style fixes to better align with the
trac coding guidelines. The changes that break 0.10 compatibility have
been left out for now.