Skip to content

Commit

Permalink
Fix #7126 - saferepr for bytes params
Browse files Browse the repository at this point in the history
bytes parametrize parameters cause error when --setup-show is used
and Python is called with -bb flag
  • Loading branch information
lancelote committed May 9, 2020
1 parent 741a8b8 commit 903e2ab
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ Ondřej Súkup
Oscar Benjamin
Patrick Hayes
Pauli Virtanen
Pavel Karateev
Paweł Adamczak
Pedro Algarvio
Philipp Loose
Expand Down
3 changes: 3 additions & 0 deletions changelog/7126.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Use ``saferepr`` to format bytes ``parametrize`` parameters for ``--setup-show``
output to prevent errors when Python is called with ``-bb`` to catch bytearray with
unicode comparison.
7 changes: 6 additions & 1 deletion src/_pytest/setuponly.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from _pytest._io.saferepr import saferepr


def pytest_addoption(parser):
Expand Down Expand Up @@ -66,7 +67,11 @@ def _show_fixture_action(fixturedef, msg):
tw.write(" (fixtures used: {})".format(", ".join(deps)))

if hasattr(fixturedef, "cached_param"):
tw.write("[{}]".format(fixturedef.cached_param))
if isinstance(fixturedef.cached_param, bytes):
param = saferepr(fixturedef.cached_param, maxsize=42)
else:
param = fixturedef.cached_param
tw.write("[{}]".format(param))

tw.flush()

Expand Down
18 changes: 18 additions & 0 deletions testing/test_setuponly.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import pytest
from _pytest.config import ExitCode

Expand Down Expand Up @@ -292,3 +294,19 @@ def test_arg(arg):
]
)
assert result.ret == ExitCode.INTERRUPTED


def test_parametrize_no_comparing_bytearray_error(testdir):
test_file = testdir.makepyfile(
"""
import pytest
@pytest.mark.parametrize('data', [b'Hello World'])
def test_data(data):
pass
"""
)
result = testdir.run(
sys.executable, "-bb", "-m", "pytest", "--setup-show", str(test_file)
)
assert result.ret == 0

0 comments on commit 903e2ab

Please sign in to comment.