Why does str(KeyError) add extra quotes?
This is done so that you can detect KeyError('')
properly. From the KeyError_str
function source:
/* If args is a tuple of exactly one item, apply repr to args[0]. This is done so that e.g. the exception raised by {}[''] prints KeyError: '' rather than the confusing KeyError alone. The downside is that if KeyError is raised with an explanatory string, that string will be displayed in quotes. Too bad. If args is anything else, use the default BaseException__str__().*/
And indeed, the traceback
printing code will not print the exception value if str(value)
is an empty string.