Fix usages of py.io.saferepr

This commit is contained in:
Anthony Sottile 2019-01-20 10:45:12 -08:00
parent 095ce2ca7f
commit 0c6ca0da62
8 changed files with 27 additions and 25 deletions

View File

@ -54,5 +54,5 @@ repos:
- id: py-deprecated - id: py-deprecated
name: py library is deprecated name: py library is deprecated
language: pygrep language: pygrep
entry: '\bpy\.(builtin\.|code\.|std\.)' entry: '\bpy\.(builtin\.|code\.|std\.|io\.saferepr)'
types: [python] types: [python]

View File

@ -0,0 +1 @@
Copy saferepr from pylib

View File

@ -36,10 +36,11 @@ platforms = unix, linux, osx, cygwin, win32
zip_safe = no zip_safe = no
packages = packages =
_pytest _pytest
_pytest.assertion
_pytest._code _pytest._code
_pytest.mark _pytest._io
_pytest.assertion
_pytest.config _pytest.config
_pytest.mark
py_modules = pytest py_modules = pytest
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.* python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*

View File

@ -18,6 +18,7 @@ import six
from six import text_type from six import text_type
import _pytest import _pytest
from _pytest._io.saferepr import saferepr
from _pytest.compat import _PY2 from _pytest.compat import _PY2
from _pytest.compat import _PY3 from _pytest.compat import _PY3
from _pytest.compat import PY35 from _pytest.compat import PY35
@ -144,7 +145,7 @@ class Frame(object):
def repr(self, object): def repr(self, object):
""" return a 'safe' (non-recursive, one-line) string repr for 'object' """ return a 'safe' (non-recursive, one-line) string repr for 'object'
""" """
return py.io.saferepr(object) return saferepr(object)
def is_true(self, object): def is_true(self, object):
return object return object
@ -423,7 +424,7 @@ class ExceptionInfo(object):
if exprinfo is None and isinstance(tup[1], AssertionError): if exprinfo is None and isinstance(tup[1], AssertionError):
exprinfo = getattr(tup[1], "msg", None) exprinfo = getattr(tup[1], "msg", None)
if exprinfo is None: if exprinfo is None:
exprinfo = py.io.saferepr(tup[1]) exprinfo = saferepr(tup[1])
if exprinfo and exprinfo.startswith(cls._assert_start_repr): if exprinfo and exprinfo.startswith(cls._assert_start_repr):
_striptext = "AssertionError: " _striptext = "AssertionError: "
@ -620,7 +621,7 @@ class FormattedExcinfo(object):
return source return source
def _saferepr(self, obj): def _saferepr(self, obj):
return py.io.saferepr(obj) return saferepr(obj)
def repr_args(self, entry): def repr_args(self, entry):
if self.funcargs: if self.funcargs:

View File

@ -19,6 +19,7 @@ import atomicwrites
import py import py
import six import six
from _pytest._io.saferepr import saferepr
from _pytest.assertion import util from _pytest.assertion import util
from _pytest.compat import spec_from_file_location from _pytest.compat import spec_from_file_location
from _pytest.pathlib import fnmatch_ex from _pytest.pathlib import fnmatch_ex
@ -484,7 +485,7 @@ def _saferepr(obj):
JSON reprs. JSON reprs.
""" """
r = py.io.saferepr(obj) r = saferepr(obj)
# only occurs in python2.x, repr must return text in python3+ # only occurs in python2.x, repr must return text in python3+
if isinstance(r, bytes): if isinstance(r, bytes):
# Represent unprintable bytes as `\x##` # Represent unprintable bytes as `\x##`
@ -503,7 +504,7 @@ def _format_assertmsg(obj):
For strings this simply replaces newlines with '\n~' so that For strings this simply replaces newlines with '\n~' so that
util.format_explanation() will preserve them instead of escaping util.format_explanation() will preserve them instead of escaping
newlines. For other objects py.io.saferepr() is used first. newlines. For other objects saferepr() is used first.
""" """
# reprlib appears to have a bug which means that if a string # reprlib appears to have a bug which means that if a string
@ -512,7 +513,7 @@ def _format_assertmsg(obj):
# However in either case we want to preserve the newline. # However in either case we want to preserve the newline.
replaces = [(u"\n", u"\n~"), (u"%", u"%%")] replaces = [(u"\n", u"\n~"), (u"%", u"%%")]
if not isinstance(obj, six.string_types): if not isinstance(obj, six.string_types):
obj = py.io.saferepr(obj) obj = saferepr(obj)
replaces.append((u"\\n", u"\n~")) replaces.append((u"\\n", u"\n~"))
if isinstance(obj, bytes): if isinstance(obj, bytes):
@ -753,7 +754,7 @@ class AssertionRewriter(ast.NodeVisitor):
return ast.Name(name, ast.Load()) return ast.Name(name, ast.Load())
def display(self, expr): def display(self, expr):
"""Call py.io.saferepr on the expression.""" """Call saferepr on the expression."""
return self.helper("saferepr", expr) return self.helper("saferepr", expr)
def helper(self, name, *args): def helper(self, name, *args):

View File

@ -5,11 +5,11 @@ from __future__ import print_function
import pprint import pprint
import py
import six import six
import _pytest._code import _pytest._code
from ..compat import Sequence from ..compat import Sequence
from _pytest._io.saferepr import saferepr
# The _reprcompare attribute on the util module is used by the new assertion # The _reprcompare attribute on the util module is used by the new assertion
# interpretation code and assertion rewriter to detect this plugin was # interpretation code and assertion rewriter to detect this plugin was
@ -105,8 +105,8 @@ except NameError:
def assertrepr_compare(config, op, left, right): def assertrepr_compare(config, op, left, right):
"""Return specialised explanations for some operators/operands""" """Return specialised explanations for some operators/operands"""
width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op
left_repr = py.io.saferepr(left, maxsize=int(width // 2)) left_repr = saferepr(left, maxsize=int(width // 2))
right_repr = py.io.saferepr(right, maxsize=width - len(left_repr)) right_repr = saferepr(right, maxsize=width - len(left_repr))
summary = u"%s %s %s" % (ecu(left_repr), op, ecu(right_repr)) summary = u"%s %s %s" % (ecu(left_repr), op, ecu(right_repr))
@ -282,12 +282,12 @@ def _compare_eq_sequence(left, right, verbose=False):
if len(left) > len(right): if len(left) > len(right):
explanation += [ explanation += [
u"Left contains more items, first extra item: %s" u"Left contains more items, first extra item: %s"
% py.io.saferepr(left[len(right)]) % saferepr(left[len(right)])
] ]
elif len(left) < len(right): elif len(left) < len(right):
explanation += [ explanation += [
u"Right contains more items, first extra item: %s" u"Right contains more items, first extra item: %s"
% py.io.saferepr(right[len(left)]) % saferepr(right[len(left)])
] ]
return explanation return explanation
@ -299,11 +299,11 @@ def _compare_eq_set(left, right, verbose=False):
if diff_left: if diff_left:
explanation.append(u"Extra items in the left set:") explanation.append(u"Extra items in the left set:")
for item in diff_left: for item in diff_left:
explanation.append(py.io.saferepr(item)) explanation.append(saferepr(item))
if diff_right: if diff_right:
explanation.append(u"Extra items in the right set:") explanation.append(u"Extra items in the right set:")
for item in diff_right: for item in diff_right:
explanation.append(py.io.saferepr(item)) explanation.append(saferepr(item))
return explanation return explanation
@ -320,9 +320,7 @@ def _compare_eq_dict(left, right, verbose=False):
if diff: if diff:
explanation += [u"Differing items:"] explanation += [u"Differing items:"]
for k in diff: for k in diff:
explanation += [ explanation += [saferepr({k: left[k]}) + " != " + saferepr({k: right[k]})]
py.io.saferepr({k: left[k]}) + " != " + py.io.saferepr({k: right[k]})
]
extra_left = set(left) - set(right) extra_left = set(left) - set(right)
if extra_left: if extra_left:
explanation.append(u"Left contains more items:") explanation.append(u"Left contains more items:")
@ -376,7 +374,7 @@ def _notin_text(term, text, verbose=False):
tail = text[index + len(term) :] tail = text[index + len(term) :]
correct_text = head + tail correct_text = head + tail
diff = _diff_text(correct_text, text, verbose) diff = _diff_text(correct_text, text, verbose)
newdiff = [u"%s is contained here:" % py.io.saferepr(term, maxsize=42)] newdiff = [u"%s is contained here:" % saferepr(term, maxsize=42)]
for line in diff: for line in diff:
if line.startswith(u"Skipping"): if line.startswith(u"Skipping"):
continue continue

View File

@ -17,6 +17,7 @@ import six
from six import text_type from six import text_type
import _pytest import _pytest
from _pytest._io.saferepr import saferepr
from _pytest.outcomes import fail from _pytest.outcomes import fail
from _pytest.outcomes import TEST_OUTCOME from _pytest.outcomes import TEST_OUTCOME
@ -294,7 +295,7 @@ def get_real_func(obj):
else: else:
raise ValueError( raise ValueError(
("could not find real function of {start}\nstopped at {current}").format( ("could not find real function of {start}\nstopped at {current}").format(
start=py.io.saferepr(start_obj), current=py.io.saferepr(obj) start=saferepr(start_obj), current=saferepr(obj)
) )
) )
if isinstance(obj, functools.partial): if isinstance(obj, functools.partial):

View File

@ -20,6 +20,7 @@ import six
import pytest import pytest
from _pytest._code import Source from _pytest._code import Source
from _pytest._io.saferepr import saferepr
from _pytest.assertion.rewrite import AssertionRewritingHook from _pytest.assertion.rewrite import AssertionRewritingHook
from _pytest.capture import MultiCapture from _pytest.capture import MultiCapture
from _pytest.capture import SysCapture from _pytest.capture import SysCapture
@ -1225,9 +1226,7 @@ def getdecoded(out):
try: try:
return out.decode("utf-8") return out.decode("utf-8")
except UnicodeDecodeError: except UnicodeDecodeError:
return "INTERNAL not-utf8-decodeable, truncated string:\n%s" % ( return "INTERNAL not-utf8-decodeable, truncated string:\n%s" % (saferepr(out),)
py.io.saferepr(out),
)
class LineComp(object): class LineComp(object):