Issue 33848: Incomplete format string syntax for Exceptions
The following is valid Python 2:
>>> 'okay {0:s}'.format(Exception('test'))
'okay test'
>>> 'okay {0}'.format(Exception('test'))
'okay test'
The following fails on Python 3.6:
>>> 'okay {0:s}'.format(Exception('test'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to Exception.__format__
While this doesn't fail:
>>> 'okay {0}'.format(Exception('test'))
'okay test'
This is because Exception does not define __format__, so object.__format__ is being called. object.__format__ does not allow any format specifier to be used. You get the same error for any object without its own __format__, when you supply a format spec.
>>> 'okay {0:s}'.format(sys)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to module.__format__
The reason this is enforced is because it allows an Execption.__format__ to be added in the future, without worrying what existing format specifiers are being used. We had a problem adding complex.__format__ because people had supplied format specs when formatting complex numbers, assuming they acted like strings. But now we've prevented this problem from happening in the future.
If you want to provide str formatting options to something like an exception, you should force-convert it to a str first, using !s:
>>> 'okay {0!s:*^10}'.format(Exception('test'))
'okay ***test***'