Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,11 @@ def _saferepr(obj):
JSON reprs.

"""
repr = py.io.saferepr(obj)
if isinstance(repr, six.text_type):
t = six.text_type
r = py.io.saferepr(obj)
if isinstance(r, six.text_type):
return r.replace(u"\n", u"\\n")
else:
t = six.binary_type
return repr.replace(t("\n"), t("\\n"))
return r.replace(b"\n", b"\\n")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that bytes("string literal") in python3+ is a TypeError so I fixed these as well.

As far as I can tell however this situation is impossible as __repr__ raises for non str types in python3.x



from _pytest.assertion.util import format_explanation as _format_explanation # noqa
Expand Down Expand Up @@ -446,10 +445,9 @@ def _should_repr_global_name(obj):
def _format_boolop(explanations, is_or):
explanation = "(" + (is_or and " or " or " and ").join(explanations) + ")"
if isinstance(explanation, six.text_type):
t = six.text_type
return explanation.replace(u"%", u"%%")
else:
t = six.binary_type
return explanation.replace(t("%"), t("%%"))
return explanation.replace(b"%", b"%%")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one however seems maybe possible? I'm not sure how this code gets called 🤷‍♂️



def _call_reprcompare(ops, results, expls, each_obj):
Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ def escape_for_readable_diff(binary_text):
r = r.replace(r"\r", "\r")
return r

if isinstance(left, six.binary_type):
if isinstance(left, bytes):
left = escape_for_readable_diff(left)
if isinstance(right, six.binary_type):
if isinstance(right, bytes):
right = escape_for_readable_diff(right)
if not verbose:
i = 0 # just in case left or right has zero length
Expand Down
6 changes: 3 additions & 3 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import py
import pytest
import contextlib
from six import binary_type, text_type
from six import text_type
from _pytest import capture
from _pytest.capture import CaptureManager
from _pytest.main import EXIT_NOTESTSCOLLECTED
Expand All @@ -24,12 +24,12 @@
def tobytes(obj):
if isinstance(obj, text_type):
obj = obj.encode("UTF-8")
assert isinstance(obj, binary_type)
assert isinstance(obj, bytes)
return obj


def totext(obj):
if isinstance(obj, binary_type):
if isinstance(obj, bytes):
obj = text_type(obj, "UTF-8")
assert isinstance(obj, text_type)
return obj
Expand Down