-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix pytest.raises handling of unicode exceptions in Python 2 #5479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
013d0e6
86a4eb6
09dee29
a886015
34b4e21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Use safe_str to serialize Exceptions in pytest.raises | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -572,8 +572,9 @@ def match(self, regexp): | |
raised. | ||
""" | ||
__tracebackhide__ = True | ||
if not re.search(regexp, str(self.value)): | ||
assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value) | ||
value = safe_str(self.value) | ||
if not re.search(regexp, value): | ||
assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. option or followup: we might want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done Out of curiosity, is there any reason other than style to prefer one over the other? |
||
return True | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -278,3 +278,7 @@ def __class__(self): | |
with pytest.raises(CrappyClass()): | ||
pass | ||
assert "via __class__" in excinfo.value.args[0] | ||
|
||
def test_u(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
seems to be failing on windows CI There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've applied this patch: diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py
index e621f3ee0..407155373 100644
--- a/src/_pytest/_code/code.py
+++ b/src/_pytest/_code/code.py
@@ -574,7 +574,7 @@ class ExceptionInfo(object):
__tracebackhide__ = True
value = safe_str(self.value)
if not re.search(regexp, value):
- assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, value)
+ assert 0, u"Pattern {!r} not found in {!r}".format(regexp, value)
return True The problem is that even so the check still fails, when it should pass:
This will be true for any system which can't handle that unicode string using the default locale. This solution works for me and the rest of the suite: diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py
index e621f3ee0..e04bbc915 100644
--- a/src/_pytest/_code/code.py
+++ b/src/_pytest/_code/code.py
@@ -14,6 +14,7 @@ from weakref import ref
import attr
import pluggy
import py
+import six
from six import text_type
import _pytest
@@ -572,9 +573,9 @@ class ExceptionInfo(object):
raised.
"""
__tracebackhide__ = True
- value = safe_str(self.value)
+ value = text_type(self.value) if isinstance(regexp, text_type) else str(self.value)
if not re.search(regexp, value):
- assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, value)
+ assert 0, u"Pattern '{!s}' not found in '{!s}'".format(regexp, value)
return True There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. though this probably triggers the same error I posted in the issue when using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, but actually it works:
so I think we can go with that solution. 👍 |
||
with pytest.raises(AssertionError, match=u"\u2603"): | ||
assert False, u"\u2603" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also test a failure of unicode<=>unicode as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but what do you mean by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah that is super unclear, what the heck did I mean 🤔 there's 6(?) combinations I think and maybe need more tests for them:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, thanks. Done. Surprisingly on Python 3 we don't support raising exceptions using bytes ( |
Uh oh!
There was an error while loading. Please reload this page.