Skip to content

Commit

Permalink
Merge pull request pytest-dev#6243 from blueyed/move-_pformat_dispatch
Browse files Browse the repository at this point in the history
minor: move internal _pformat_dispatch function
  • Loading branch information
blueyed committed Nov 20, 2019
2 parents 5b3867f + c0b1a39 commit f1224a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
21 changes: 21 additions & 0 deletions src/_pytest/_io/saferepr.py
Expand Up @@ -80,3 +80,24 @@ def saferepr(obj: Any, maxsize: int = 240) -> str:
around the Repr/reprlib functionality of the standard 2.6 lib.
"""
return SafeRepr(maxsize).repr(obj)


class AlwaysDispatchingPrettyPrinter(pprint.PrettyPrinter):
"""PrettyPrinter that always dispatches (regardless of width)."""

def _format(self, object, stream, indent, allowance, context, level):
p = self._dispatch.get(type(object).__repr__, None)

objid = id(object)
if objid in context or p is None:
return super()._format(object, stream, indent, allowance, context, level)

context[objid] = 1
p(self, object, stream, indent, allowance, context, level + 1)
del context[objid]


def _pformat_dispatch(object, indent=1, width=80, depth=None, *, compact=False):
return AlwaysDispatchingPrettyPrinter(
indent=1, width=80, depth=None, compact=False
).pformat(object)
22 changes: 1 addition & 21 deletions src/_pytest/assertion/util.py
Expand Up @@ -13,6 +13,7 @@

import _pytest._code
from _pytest import outcomes
from _pytest._io.saferepr import _pformat_dispatch
from _pytest._io.saferepr import safeformat
from _pytest._io.saferepr import saferepr
from _pytest.compat import ATTRS_EQ_FIELD
Expand All @@ -28,27 +29,6 @@
_assertion_pass = None # type: Optional[Callable[[int, str, str], None]]


class AlwaysDispatchingPrettyPrinter(pprint.PrettyPrinter):
"""PrettyPrinter that always dispatches (regardless of width)."""

def _format(self, object, stream, indent, allowance, context, level):
p = self._dispatch.get(type(object).__repr__, None)

objid = id(object)
if objid in context or p is None:
return super()._format(object, stream, indent, allowance, context, level)

context[objid] = 1
p(self, object, stream, indent, allowance, context, level + 1)
del context[objid]


def _pformat_dispatch(object, indent=1, width=80, depth=None, *, compact=False):
return AlwaysDispatchingPrettyPrinter(
indent=1, width=80, depth=None, compact=False
).pformat(object)


def format_explanation(explanation: str) -> str:
"""This formats an explanation
Expand Down

0 comments on commit f1224a0

Please sign in to comment.