Fix `TypeError` when the assertion message is `bytes` in python 3.
This commit is contained in:
parent
f25683354e
commit
452e5c1cf0
|
@ -0,0 +1 @@
|
||||||
|
Fix ``TypeError`` when the assertion message is ``bytes`` in python 3.
|
|
@ -425,20 +425,18 @@ def _format_assertmsg(obj):
|
||||||
# contains a newline it gets escaped, however if an object has a
|
# contains a newline it gets escaped, however if an object has a
|
||||||
# .__repr__() which contains newlines it does not get escaped.
|
# .__repr__() which contains newlines it does not get escaped.
|
||||||
# However in either case we want to preserve the newline.
|
# However in either case we want to preserve the newline.
|
||||||
if isinstance(obj, six.text_type) or isinstance(obj, six.binary_type):
|
replaces = [(u"\n", u"\n~"), (u"%", u"%%")]
|
||||||
s = obj
|
if not isinstance(obj, six.string_types):
|
||||||
is_repr = False
|
obj = py.io.saferepr(obj)
|
||||||
else:
|
replaces.append((u"\\n", u"\n~"))
|
||||||
s = py.io.saferepr(obj)
|
|
||||||
is_repr = True
|
if isinstance(obj, bytes):
|
||||||
if isinstance(s, six.text_type):
|
replaces = [(r1.encode(), r2.encode()) for r1, r2 in replaces]
|
||||||
t = six.text_type
|
|
||||||
else:
|
for r1, r2 in replaces:
|
||||||
t = six.binary_type
|
obj = obj.replace(r1, r2)
|
||||||
s = s.replace(t("\n"), t("\n~")).replace(t("%"), t("%%"))
|
|
||||||
if is_repr:
|
return obj
|
||||||
s = s.replace(t("\\n"), t("\n~"))
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
def _should_repr_global_name(obj):
|
def _should_repr_global_name(obj):
|
||||||
|
|
|
@ -246,6 +246,15 @@ class TestAssertionRewrite(object):
|
||||||
["*AssertionError: To be escaped: %", "*assert 1 == 2"]
|
["*AssertionError: To be escaped: %", "*assert 1 == 2"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
sys.version_info < (3,), reason="bytes is a string type in python 2"
|
||||||
|
)
|
||||||
|
def test_assertion_messages_bytes(self, testdir):
|
||||||
|
testdir.makepyfile("def test_bytes_assertion():\n assert False, b'ohai!'\n")
|
||||||
|
result = testdir.runpytest()
|
||||||
|
assert result.ret == 1
|
||||||
|
result.stdout.fnmatch_lines(["*AssertionError: b'ohai!'", "*assert False"])
|
||||||
|
|
||||||
def test_boolop(self):
|
def test_boolop(self):
|
||||||
def f():
|
def f():
|
||||||
f = g = False
|
f = g = False
|
||||||
|
|
Loading…
Reference in New Issue