mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Patch #1515343: Fix printing of deprecated string exceptions with a
value in the traceback module.
This commit is contained in:
parent
844f7ddcdc
commit
c13c34c39d
3 changed files with 93 additions and 46 deletions
100
Lib/traceback.py
100
Lib/traceback.py
|
@ -150,51 +150,63 @@ def format_exception_only(etype, value):
|
|||
|
||||
The arguments are the exception type and value such as given by
|
||||
sys.last_type and sys.last_value. The return value is a list of
|
||||
strings, each ending in a newline. Normally, the list contains a
|
||||
single string; however, for SyntaxError exceptions, it contains
|
||||
several lines that (when printed) display detailed information
|
||||
about where the syntax error occurred. The message indicating
|
||||
which exception occurred is the always last string in the list.
|
||||
"""
|
||||
list = []
|
||||
if (type(etype) == types.ClassType
|
||||
or (isinstance(etype, type) and issubclass(etype, BaseException))):
|
||||
stype = etype.__name__
|
||||
else:
|
||||
stype = etype
|
||||
if value is None:
|
||||
list.append(str(stype) + '\n')
|
||||
else:
|
||||
if issubclass(etype, SyntaxError):
|
||||
try:
|
||||
msg, (filename, lineno, offset, line) = value
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
if not filename: filename = "<string>"
|
||||
list.append(' File "%s", line %d\n' %
|
||||
(filename, lineno))
|
||||
if line is not None:
|
||||
i = 0
|
||||
while i < len(line) and line[i].isspace():
|
||||
i = i+1
|
||||
list.append(' %s\n' % line.strip())
|
||||
if offset is not None:
|
||||
s = ' '
|
||||
for c in line[i:offset-1]:
|
||||
if c.isspace():
|
||||
s = s + c
|
||||
else:
|
||||
s = s + ' '
|
||||
list.append('%s^\n' % s)
|
||||
value = msg
|
||||
s = _some_str(value)
|
||||
if s:
|
||||
list.append('%s: %s\n' % (str(stype), s))
|
||||
else:
|
||||
list.append('%s\n' % str(stype))
|
||||
return list
|
||||
strings, each ending in a newline.
|
||||
|
||||
Normally, the list contains a single string; however, for
|
||||
SyntaxError exceptions, it contains several lines that (when
|
||||
printed) display detailed information about where the syntax
|
||||
error occurred.
|
||||
|
||||
The message indicating which exception occurred is always the last
|
||||
string in the list.
|
||||
|
||||
"""
|
||||
|
||||
# An instance should not have a meaningful value parameter, but
|
||||
# sometimes does, particularly for string exceptions, such as
|
||||
# >>> raise string1, string2 # deprecated
|
||||
#
|
||||
# Clear these out first because issubtype(string1, SyntaxError)
|
||||
# would throw another exception and mask the original problem.
|
||||
if (isinstance(etype, BaseException) or
|
||||
isinstance(etype, types.InstanceType) or
|
||||
type(etype) is str):
|
||||
return [_format_final_exc_line(etype, value)]
|
||||
|
||||
stype = etype.__name__
|
||||
|
||||
if not issubclass(etype, SyntaxError):
|
||||
return [_format_final_exc_line(stype, value)]
|
||||
|
||||
# It was a syntax error; show exactly where the problem was found.
|
||||
try:
|
||||
msg, (filename, lineno, offset, badline) = value
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
filename = filename or "<string>"
|
||||
lines = [(' File "%s", line %d\n' % (filename, lineno))]
|
||||
if badline is not None:
|
||||
lines.append(' %s\n' % badline.strip())
|
||||
if offset is not None:
|
||||
caretspace = badline[:offset].lstrip()
|
||||
# non-space whitespace (likes tabs) must be kept for alignment
|
||||
caretspace = ((c.isspace() and c or ' ') for c in caretspace)
|
||||
# only three spaces to account for offset1 == pos 0
|
||||
lines.append(' %s^\n' % ''.join(caretspace))
|
||||
value = msg
|
||||
|
||||
lines.append(_format_final_exc_line(stype, value))
|
||||
return lines
|
||||
|
||||
def _format_final_exc_line(etype, value):
|
||||
"""Return a list of a single line -- normal case for format_exception_only"""
|
||||
if value is None or not str(value):
|
||||
line = "%s\n" % etype
|
||||
else:
|
||||
line = "%s: %s\n" % (etype, _some_str(value))
|
||||
return line
|
||||
|
||||
def _some_str(value):
|
||||
try:
|
||||
return str(value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue