Don't accept bytes message in pytest.{fail,xfail,skip}

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.
This commit is contained in:
Ran Benita 2019-07-17 10:33:10 +03:00
parent 0b532fda76
commit 675e9507d8
2 changed files with 9 additions and 8 deletions

View File

@ -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.

View File

@ -5,7 +5,6 @@ as well as functions creating them
import sys import sys
from typing import Any from typing import Any
from typing import Optional from typing import Optional
from typing import Union
from packaging.version import Version from packaging.version import Version
@ -18,19 +17,14 @@ class OutcomeException(BaseException):
contain info about test and collection outcomes. contain info about test and collection outcomes.
""" """
def __init__( def __init__(self, msg: Optional[str] = None, pytrace: bool = True) -> None:
self, msg: Optional[Union[str, bytes]] = None, pytrace: bool = True
) -> None:
BaseException.__init__(self, msg) BaseException.__init__(self, msg)
self.msg = msg self.msg = msg
self.pytrace = pytrace self.pytrace = pytrace
def __repr__(self) -> str: def __repr__(self) -> str:
if self.msg: if self.msg:
val = self.msg return self.msg
if isinstance(val, bytes):
val = val.decode("UTF-8", errors="replace")
return val
return "<{} instance>".format(self.__class__.__name__) return "<{} instance>".format(self.__class__.__name__)
__str__ = __repr__ __str__ = __repr__