Fix UnicodeEncodeError when string comparison with unicode has failed.#1870
Conversation
5e37be7 to
6cbcc35
Compare
| try: | ||
| exprinfo = str(tup[1]) | ||
| except UnicodeEncodeError: | ||
| exprinfo = unicode(tup[1]) |
There was a problem hiding this comment.
Looking at tup[1], it always seems to contain an unicode object, even when there are byte strings in the assertion:
-> exprinfo = str(tup[1])
(Pdb) tup[1]
AssertionError(u"assert 'a' == 'b'\n - a\n + b",)So I think the proper fix would be to always use unicode() and do the check with u'assert ' below.
There was a problem hiding this comment.
Actually, tup[1] is an AssertionError instance containing a unicode string. I think the proper fix is actually using py._builtin._totext:
diff --git a/_pytest/_code/code.py b/_pytest/_code/code.py
index 040e4fc..9a6d326 100644
--- a/_pytest/_code/code.py
+++ b/_pytest/_code/code.py
@@ -354,7 +354,7 @@ class ExceptionInfo(object):
if exprinfo is None and isinstance(tup[1], AssertionError):
exprinfo = getattr(tup[1], 'msg', None)
if exprinfo is None:
- exprinfo = str(tup[1])
+ exprinfo = py._builtin._totext(tup[1])
if exprinfo and exprinfo.startswith('assert '):
self._striptext = 'AssertionError: '
self._excinfo = tuppy._builtin._totext is just unicode in Python 2 and str in Python 3. With the above change the test passes to me.
| * Add ``buffer`` attribute to stdin stub class ``pytest.capture.DontReadFromInput`` | ||
| Thanks `@joguSD`_ for the PR. | ||
|
|
||
| * Fix UnicodeEncodeError when string comparison with unicode is failed. (`#1864`_) |
There was a problem hiding this comment.
Nitpick: "has failed" or "fails" 😁
Also: Use double backticks around UnicodeEncodeError so it appears in a monospaced font (see buffer on the item above this one).
6cbcc35 to
86b8801
Compare
|
Thanks for your kind review! I've updated this PR.
And,
I think this fix is unnecessary because below code runs well: |
I agree. 😁 Thanks for the update, I will merge as soon as CI passes. |
|
Oh, just a tip: on GitHub if you add a string like "Fixes #XXX" or "Resolves #XXX" to the commit message it will automatically close the issue |
|
Thanks again for the PR! |
Thanks for submitting a PR, your contribution is really appreciated!
Here's a quick checklist that should be present in PRs:
master; for new features, targetfeatures;AUTHORS;CHANGELOG.rstCHANGELOG, so please add a thank note to yourself ("Thanks @user for the PR") and a link to your GitHub profile. It may sound weird thanking yourself, but otherwise a maintainer would have to do it manually before or after merging instead of just using GitHub's merge button. This makes it easier on the maintainers to merge PRs.UnicodeEncodeError is occurred when comparing two strings with any unicode character. This PR fixes this error.
Relative issue: #1864.
Error reproduction: