diff --git a/AUTHORS b/AUTHORS index c260a9653..49440194e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -192,6 +192,7 @@ Stefan Zimmermann Stefano Taschini Steffen Allner Stephan Obermann +Tadek Teleżyński Tarcisio Fischer Tareq Alayan Ted Xiao diff --git a/changelog/980.bugfix.rst b/changelog/980.bugfix.rst new file mode 100644 index 000000000..e30c38f20 --- /dev/null +++ b/changelog/980.bugfix.rst @@ -0,0 +1 @@ +Fix truncated locals output in verbose mode. diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 0e5e580b6..78644db8a 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -1,5 +1,6 @@ from __future__ import absolute_import, division, print_function import inspect +import pprint import sys import traceback from inspect import CO_VARARGS, CO_VARKEYWORDS @@ -448,6 +449,7 @@ class ExceptionInfo(object): abspath=False, tbfilter=True, funcargs=False, + truncate_locals=True, ): """ return str()able representation of this exception info. showlocals: show locals per traceback entry @@ -472,6 +474,7 @@ class ExceptionInfo(object): abspath=abspath, tbfilter=tbfilter, funcargs=funcargs, + truncate_locals=truncate_locals, ) return fmt.repr_excinfo(self) @@ -511,6 +514,7 @@ class FormattedExcinfo(object): abspath = attr.ib(default=True) tbfilter = attr.ib(default=True) funcargs = attr.ib(default=False) + truncate_locals = attr.ib(default=True) astcache = attr.ib(default=attr.Factory(dict), init=False, repr=False) def _getindent(self, source): @@ -593,7 +597,10 @@ class FormattedExcinfo(object): # This formatting could all be handled by the # _repr() function, which is only reprlib.Repr in # disguise, so is very configurable. - str_repr = self._saferepr(value) + if self.truncate_locals: + str_repr = self._saferepr(value) + else: + str_repr = pprint.pformat(value) # if len(str_repr) < 70 or not isinstance(value, # (list, tuple, dict)): lines.append("%-10s = %s" % (name, str_repr)) diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 85f176e90..49c30e903 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -287,6 +287,11 @@ class Node(object): else: style = "long" + if self.config.option.verbose > 1: + truncate_locals = False + else: + truncate_locals = True + try: os.getcwd() abspath = False @@ -299,6 +304,7 @@ class Node(object): showlocals=self.config.option.showlocals, style=style, tbfilter=tbfilter, + truncate_locals=truncate_locals, ) repr_failure = _repr_failure_py diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 4bac7ba4c..403063ad6 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -579,6 +579,18 @@ raise ValueError() assert reprlocals.lines[2] == "y = 5" 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): mod = importasmod( """