Skip to content

Commit 2823f03

Browse files
committed
Patch by Toby Dickenson: don't die when an error occurs during string
conversion in an exception, but instead display <unprintable %s object> where %s is the type name.
1 parent 986659f commit 2823f03

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Lib/traceback.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,15 @@ def format_exception_only(etype, value):
166166
s = s + ' '
167167
list.append('%s^\n' % s)
168168
value = msg
169-
list.append('%s: %s\n' % (str(stype), str(value)))
169+
list.append('%s: %s\n' % (str(stype), _some_str(value)))
170170
return list
171171

172+
def _some_str(value):
173+
try:
174+
return str(value)
175+
except:
176+
return '<unprintable %s object>' % type(value).__name__
177+
172178

173179
def print_exc(limit=None, file=None):
174180
"""This is a shorthand for 'print_exception(sys.exc_type,

0 commit comments

Comments
 (0)