Skip to content

Commit

Permalink
WIP: assert: use safeformat for display with -vv
Browse files Browse the repository at this point in the history
TODO:

- [ ] real plugin hook?
- [ ] should handle other places where safeformat is used also, via
      args/options to the hook then likely.

Ref: pytest-dev#3962
Ref: pytest-dev#5933
  • Loading branch information
blueyed committed Oct 21, 2019
1 parent fbb7f66 commit 4496e36
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/_pytest/assertion/__init__.py
Expand Up @@ -141,6 +141,17 @@ def callbinrepr(op, left, right):

util._reprcompare = callbinrepr

from _pytest._io.saferepr import safeformat
from _pytest._io.saferepr import saferepr

def call_assertion_display_hook(obj):
verbose = item.config.getoption("verbose")
if verbose > 1:
return safeformat(obj)
return saferepr(obj).replace("\n", "\\n")

util._reprdisplay = call_assertion_display_hook

if item.ihook.pytest_assertion_pass.get_hookimpls():

def call_assertion_pass_hook(lineno, orig, expl):
Expand All @@ -153,6 +164,7 @@ def call_assertion_pass_hook(lineno, orig, expl):

def pytest_runtest_teardown(item):
util._reprcompare = None
util._reprdisplay = None
util._assertion_pass = None


Expand Down
7 changes: 5 additions & 2 deletions src/_pytest/assertion/rewrite.py
Expand Up @@ -398,6 +398,10 @@ def _call_reprcompare(ops, results, expls, each_obj):
return expl


def _call_reprdisplay(obj):
return util._reprdisplay(obj)


def _call_assertion_pass(lineno, orig, expl):
# type: (int, str, str) -> None
if util._assertion_pass is not None:
Expand Down Expand Up @@ -662,8 +666,7 @@ def assign(self, expr):
return ast.Name(name, ast.Load())

def display(self, expr):
"""Call saferepr on the expression."""
return self.helper("_saferepr", expr)
return self.helper("_call_reprdisplay", expr)

def helper(self, name, *args):
"""Call a helper in this module."""
Expand Down

0 comments on commit 4496e36

Please sign in to comment.