Skip to content

Commit

Permalink
Don't accept bytes message in pytest.{fail,xfail,skip}
Browse files Browse the repository at this point in the history
It seems to have been added in #1439 to fix #1178.

This was only relevant for Python 2 where it was tempting to use str (==
bytes) literals instead of unicode literals. In Python 3, it is unlikely
that anyone passes bytes to these functions.
  • Loading branch information
bluetech committed Jul 17, 2019
1 parent 0b532fd commit 675e950
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 7 additions & 0 deletions changelog/5615.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
``pytest.fail``, ``pytest.xfail`` and ``pytest.skip`` no longer support bytes for the message argument.

This was supported for Python 2 where it was tempting to use ``"message"``
instead of ``u"message"``.

Python 3 code is unlikely to pass ``bytes`` to these functions. If you do,
please decode it to an ``str`` beforehand.
10 changes: 2 additions & 8 deletions src/_pytest/outcomes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys
from typing import Any
from typing import Optional
from typing import Union

from packaging.version import Version

Expand All @@ -18,19 +17,14 @@ class OutcomeException(BaseException):
contain info about test and collection outcomes.
"""

def __init__(
self, msg: Optional[Union[str, bytes]] = None, pytrace: bool = True
) -> None:
def __init__(self, msg: Optional[str] = None, pytrace: bool = True) -> None:
BaseException.__init__(self, msg)
self.msg = msg
self.pytrace = pytrace

def __repr__(self) -> str:
if self.msg:
val = self.msg
if isinstance(val, bytes):
val = val.decode("UTF-8", errors="replace")
return val
return self.msg
return "<{} instance>".format(self.__class__.__name__)

__str__ = __repr__
Expand Down

0 comments on commit 675e950

Please sign in to comment.