Fix truncated locals in verbose mode

This commit is contained in:
Tadek Teleżyński 2018-07-14 16:05:50 +02:00 committed by Bruno Oliveira
parent f6ceedd15b
commit d2fe619120
5 changed files with 28 additions and 1 deletions

View File

@ -192,6 +192,7 @@ Stefan Zimmermann
Stefano Taschini Stefano Taschini
Steffen Allner Steffen Allner
Stephan Obermann Stephan Obermann
Tadek Teleżyński
Tarcisio Fischer Tarcisio Fischer
Tareq Alayan Tareq Alayan
Ted Xiao Ted Xiao

1
changelog/980.bugfix.rst Normal file
View File

@ -0,0 +1 @@
Fix truncated locals output in verbose mode.

View File

@ -1,5 +1,6 @@
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
import inspect import inspect
import pprint
import sys import sys
import traceback import traceback
from inspect import CO_VARARGS, CO_VARKEYWORDS from inspect import CO_VARARGS, CO_VARKEYWORDS
@ -448,6 +449,7 @@ class ExceptionInfo(object):
abspath=False, abspath=False,
tbfilter=True, tbfilter=True,
funcargs=False, funcargs=False,
truncate_locals=True,
): ):
""" return str()able representation of this exception info. """ return str()able representation of this exception info.
showlocals: show locals per traceback entry showlocals: show locals per traceback entry
@ -472,6 +474,7 @@ class ExceptionInfo(object):
abspath=abspath, abspath=abspath,
tbfilter=tbfilter, tbfilter=tbfilter,
funcargs=funcargs, funcargs=funcargs,
truncate_locals=truncate_locals,
) )
return fmt.repr_excinfo(self) return fmt.repr_excinfo(self)
@ -511,6 +514,7 @@ class FormattedExcinfo(object):
abspath = attr.ib(default=True) abspath = attr.ib(default=True)
tbfilter = attr.ib(default=True) tbfilter = attr.ib(default=True)
funcargs = attr.ib(default=False) funcargs = attr.ib(default=False)
truncate_locals = attr.ib(default=True)
astcache = attr.ib(default=attr.Factory(dict), init=False, repr=False) astcache = attr.ib(default=attr.Factory(dict), init=False, repr=False)
def _getindent(self, source): def _getindent(self, source):
@ -593,7 +597,10 @@ class FormattedExcinfo(object):
# This formatting could all be handled by the # This formatting could all be handled by the
# _repr() function, which is only reprlib.Repr in # _repr() function, which is only reprlib.Repr in
# disguise, so is very configurable. # disguise, so is very configurable.
if self.truncate_locals:
str_repr = self._saferepr(value) str_repr = self._saferepr(value)
else:
str_repr = pprint.pformat(value)
# if len(str_repr) < 70 or not isinstance(value, # if len(str_repr) < 70 or not isinstance(value,
# (list, tuple, dict)): # (list, tuple, dict)):
lines.append("%-10s = %s" % (name, str_repr)) lines.append("%-10s = %s" % (name, str_repr))

View File

@ -287,6 +287,11 @@ class Node(object):
else: else:
style = "long" style = "long"
if self.config.option.verbose > 1:
truncate_locals = False
else:
truncate_locals = True
try: try:
os.getcwd() os.getcwd()
abspath = False abspath = False
@ -299,6 +304,7 @@ class Node(object):
showlocals=self.config.option.showlocals, showlocals=self.config.option.showlocals,
style=style, style=style,
tbfilter=tbfilter, tbfilter=tbfilter,
truncate_locals=truncate_locals,
) )
repr_failure = _repr_failure_py repr_failure = _repr_failure_py

View File

@ -579,6 +579,18 @@ raise ValueError()
assert reprlocals.lines[2] == "y = 5" assert reprlocals.lines[2] == "y = 5"
assert reprlocals.lines[3] == "z = 7" assert reprlocals.lines[3] == "z = 7"
def test_repr_local_truncated(self):
loc = {"l": [i for i in range(10)]}
p = FormattedExcinfo(showlocals=True)
truncated_reprlocals = p.repr_locals(loc)
assert truncated_reprlocals.lines
assert truncated_reprlocals.lines[0] == "l = [0, 1, 2, 3, 4, 5, ...]"
q = FormattedExcinfo(showlocals=True, truncate_locals=False)
full_reprlocals = q.repr_locals(loc)
assert full_reprlocals.lines
assert full_reprlocals.lines[0] == "l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
def test_repr_tracebackentry_lines(self, importasmod): def test_repr_tracebackentry_lines(self, importasmod):
mod = importasmod( mod = importasmod(
""" """