Move the _pformat() method to be a classmethod

To avoid creating nested functions when a class one
will suffice just switch to using a class method for
the nested _pformat() function instead.

This also makes sure that the cause of a exception is
not none before attempting to format it (and its
children) and also passes along a lines list that is
appended/extended instead of each function call creating
its own version which is later merged.

Change-Id: Ie84b004c115e488c87f9829bfcf4d8f66ebb660f
Merge "Move the _pformat() method to be a classmethod"