From 903e2ab6eef5a99625f4ef58115c9455b018feda Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sat, 9 May 2020 13:57:17 +0300 Subject: [PATCH 1/8] Fix #7126 - saferepr for bytes params bytes parametrize parameters cause error when --setup-show is used and Python is called with -bb flag --- AUTHORS | 1 + changelog/7126.bugfix.rst | 3 +++ src/_pytest/setuponly.py | 7 ++++++- testing/test_setuponly.py | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelog/7126.bugfix.rst diff --git a/AUTHORS b/AUTHORS index b59ebc2a2..68f156862 100644 --- a/AUTHORS +++ b/AUTHORS @@ -216,6 +216,7 @@ Ondřej Súkup Oscar Benjamin Patrick Hayes Pauli Virtanen +Pavel Karateev Paweł Adamczak Pedro Algarvio Philipp Loose diff --git a/changelog/7126.bugfix.rst b/changelog/7126.bugfix.rst new file mode 100644 index 000000000..6817ee211 --- /dev/null +++ b/changelog/7126.bugfix.rst @@ -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. diff --git a/src/_pytest/setuponly.py b/src/_pytest/setuponly.py index c9cc589ff..e18208e59 100644 --- a/src/_pytest/setuponly.py +++ b/src/_pytest/setuponly.py @@ -1,4 +1,5 @@ import pytest +from _pytest._io.saferepr import saferepr def pytest_addoption(parser): @@ -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() diff --git a/testing/test_setuponly.py b/testing/test_setuponly.py index e26a33dee..779c17fbd 100644 --- a/testing/test_setuponly.py +++ b/testing/test_setuponly.py @@ -1,3 +1,5 @@ +import sys + import pytest from _pytest.config import ExitCode @@ -292,3 +294,19 @@ def test_setup_show_with_KeyboardInterrupt_in_test(testdir): ] ) 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 From a2280d39ec9ff030abcbaba8e246c269587b9bea Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sat, 9 May 2020 14:14:23 +0300 Subject: [PATCH 2/8] #7126, use past tense in changelog --- changelog/7126.bugfix.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog/7126.bugfix.rst b/changelog/7126.bugfix.rst index 6817ee211..ad3368d77 100644 --- a/changelog/7126.bugfix.rst +++ b/changelog/7126.bugfix.rst @@ -1,3 +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. +Switched to ``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. From feb7a5f0d1589bdc82473537e9017ff832e684f3 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sun, 10 May 2020 12:11:59 +0300 Subject: [PATCH 3/8] Omit internal solution details --- changelog/7126.bugfix.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/changelog/7126.bugfix.rst b/changelog/7126.bugfix.rst index ad3368d77..a547688cb 100644 --- a/changelog/7126.bugfix.rst +++ b/changelog/7126.bugfix.rst @@ -1,3 +1,2 @@ -Switched to ``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. +``--setup-show`` now doesn't raise an error if bytearray is used as ``parametrize`` +parameter when Python is called with ``-bb`` flag. From 6b26f0f890f161f91d81b443468f10de1d5046a2 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sun, 10 May 2020 12:19:52 +0300 Subject: [PATCH 4/8] Rename test method and reference issue --- testing/test_setuponly.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testing/test_setuponly.py b/testing/test_setuponly.py index 779c17fbd..57c95ae06 100644 --- a/testing/test_setuponly.py +++ b/testing/test_setuponly.py @@ -296,7 +296,8 @@ def test_setup_show_with_KeyboardInterrupt_in_test(testdir): assert result.ret == ExitCode.INTERRUPTED -def test_parametrize_no_comparing_bytearray_error(testdir): +def test_show_fixture_action_with_bytearrays(testdir): + # Issue 7126, BytesWarning when using --setup-show with bytes parameter test_file = testdir.makepyfile( """ import pytest From 7b196747ddb55dd484ad5dfa8276d2d65cd737d4 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sun, 10 May 2020 12:47:26 +0300 Subject: [PATCH 5/8] Use saferepr for all types --- src/_pytest/setuponly.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/_pytest/setuponly.py b/src/_pytest/setuponly.py index e18208e59..9e4cd9519 100644 --- a/src/_pytest/setuponly.py +++ b/src/_pytest/setuponly.py @@ -67,11 +67,7 @@ def _show_fixture_action(fixturedef, msg): tw.write(" (fixtures used: {})".format(", ".join(deps))) if hasattr(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.write("[{}]".format(saferepr(fixturedef.cached_param, maxsize=42))) tw.flush() From 184528d0c28f2cde73a0457414c55218fd6a2ddf Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sun, 10 May 2020 13:06:36 +0300 Subject: [PATCH 6/8] Fix tests to expected repr output --- testing/test_setuponly.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/testing/test_setuponly.py b/testing/test_setuponly.py index 57c95ae06..9b0bb909b 100644 --- a/testing/test_setuponly.py +++ b/testing/test_setuponly.py @@ -148,10 +148,10 @@ def test_show_fixtures_with_parameters(testdir, mode): result.stdout.fnmatch_lines( [ - "SETUP S arg_same?foo?", - "TEARDOWN S arg_same?foo?", - "SETUP S arg_same?bar?", - "TEARDOWN S arg_same?bar?", + "SETUP S arg_same?'foo'?", + "TEARDOWN S arg_same?'foo'?", + "SETUP S arg_same?'bar'?", + "TEARDOWN S arg_same?'bar'?", ] ) @@ -181,7 +181,7 @@ def test_show_fixtures_with_parameter_ids(testdir, mode): assert result.ret == 0 result.stdout.fnmatch_lines( - ["SETUP S arg_same?spam?", "SETUP S arg_same?ham?"] + ["SETUP S arg_same?'spam'?", "SETUP S arg_same?'ham'?"] ) @@ -200,7 +200,9 @@ def test_show_fixtures_with_parameter_ids_function(testdir, mode): result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines(["*SETUP F foobar?FOO?", "*SETUP F foobar?BAR?"]) + result.stdout.fnmatch_lines( + ["*SETUP F foobar?'FOO'?", "*SETUP F foobar?'BAR'?"] + ) def test_dynamic_fixture_request(testdir): From 17857b67df4c4cf1ba7def1d896426e583e4120c Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sun, 10 May 2020 16:58:22 +0300 Subject: [PATCH 7/8] Better changelog wording Co-authored-by: Ran Benita --- changelog/7126.bugfix.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog/7126.bugfix.rst b/changelog/7126.bugfix.rst index a547688cb..1a85e8997 100644 --- a/changelog/7126.bugfix.rst +++ b/changelog/7126.bugfix.rst @@ -1,2 +1,2 @@ -``--setup-show`` now doesn't raise an error if bytearray is used as ``parametrize`` -parameter when Python is called with ``-bb`` flag. +``--setup-show`` now doesn't raise an error when a bytes value is used as a ``parametrize`` +parameter when Python is called with the ``-bb`` flag. From 8bd3f1a72bf794e919f0fe2d06b426729f53b092 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Sun, 10 May 2020 16:59:20 +0300 Subject: [PATCH 8/8] Better test method name Co-authored-by: Ran Benita --- testing/test_setuponly.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_setuponly.py b/testing/test_setuponly.py index 9b0bb909b..221c32a31 100644 --- a/testing/test_setuponly.py +++ b/testing/test_setuponly.py @@ -298,7 +298,7 @@ def test_setup_show_with_KeyboardInterrupt_in_test(testdir): assert result.ret == ExitCode.INTERRUPTED -def test_show_fixture_action_with_bytearrays(testdir): +def test_show_fixture_action_with_bytes(testdir): # Issue 7126, BytesWarning when using --setup-show with bytes parameter test_file = testdir.makepyfile( """