Skip to content

Commit 6d4b14d

Browse files
Merge pull request #1438 from Bachmann1234/issue-1437
Make a good faith effort to display a bytestring when one is provided…
2 parents fd0010e + 8ce32b0 commit 6d4b14d

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Marc Schlaich
5959
Mark Abramowitz
6060
Markus Unterwaditzer
6161
Martijn Faassen
62+
Matt Bachmann
6263
Michael Aquilina
6364
Michael Birtwell
6465
Michael Droettboom

CHANGELOG.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
* Fix (`#469`_): junit parses report.nodeid incorrectly, when params IDs
1313
contain ``::``. Thanks `@tomviner`_ for the PR (`#1431`_).
1414

15-
*
1615

1716
* Fix (`#578 <https://github.com/pytest-dev/pytest/issues/578>`_): SyntaxErrors
1817
containing non-ascii lines at the point of failure generated an internal
1918
py.test error.
2019
Thanks `@asottile`_ for the report and `@nicoddemus`_ for the PR.
2120

22-
*
21+
* Fix (`#1437`_): When passing in a bytestring regex pattern to parameterize
22+
attempt to decode it as utf-8 ignoring errors.
2323

2424
*
2525

26+
27+
.. _#1437: https://github.com/pytest-dev/pytest/issues/1437
2628
.. _#469: https://github.com/pytest-dev/pytest/issues/469
2729
.. _#1431: https://github.com/pytest-dev/pytest/pull/1431
2830

_pytest/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ def _idval(val, argname, idx, idfn):
11151115
elif isinstance(val, (float, int, str, bool, NoneType)):
11161116
return str(val)
11171117
elif isinstance(val, REGEX_TYPE):
1118-
return val.pattern
1118+
return _escape_bytes(val.pattern) if isinstance(val.pattern, bytes) else val.pattern
11191119
elif enum is not None and isinstance(val, enum.Enum):
11201120
return str(val)
11211121
elif isclass(val) and hasattr(val, '__name__'):

testing/acceptance_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,20 @@ def test_plugins_given_as_strings(self, tmpdir, monkeypatch):
392392
monkeypatch.setitem(sys.modules, 'myplugin', mod)
393393
assert pytest.main(args=[str(tmpdir)], plugins=['myplugin']) == 0
394394

395+
def test_parameterized_with_bytes_regex(self, testdir):
396+
p = testdir.makepyfile("""
397+
import re
398+
import pytest
399+
@pytest.mark.parametrize('r', [re.compile(b'foo')])
400+
def test_stuff(r):
401+
pass
402+
"""
403+
)
404+
res = testdir.runpytest(p)
405+
res.stdout.fnmatch_lines([
406+
'*1 passed*'
407+
])
408+
395409

396410
class TestInvocationVariants:
397411
def test_earlyinit(self, testdir):

testing/python/metafunc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ def test_idmaker_autoname(self):
170170
result = idmaker((py.builtin._totext("a"), "b"), [({}, b'\xc3\xb4')])
171171
assert result == ['a0-\\xc3\\xb4']
172172

173+
def test_idmaker_with_bytes_regex(self):
174+
from _pytest.python import idmaker
175+
result = idmaker(("a"), [(re.compile(b'foo'), 1.0)])
176+
assert result == ["foo"]
177+
173178
def test_idmaker_native_strings(self):
174179
from _pytest.python import idmaker
175180
totext = py.builtin._totext

0 commit comments

Comments
 (0)