Skip to content
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

pytest.fail with non-ascii characters raises an internal pytest error #1439

Merged
merged 2 commits into from
Mar 6, 2016

Conversation

nicoddemus
Copy link
Member

Fix #1178

(oops, sent the branch to pytest's repository by accident, sorry)

@RonnyPfannschmidt
Copy link
Member

Does this one have a test for non Unicode non ASCII?

@nicoddemus
Copy link
Member Author

I think test_pytest_fail_notrace covers it? (right above the test I added)

@RonnyPfannschmidt
Copy link
Member

At first glance it seems to test only ASCII

@nicoddemus
Copy link
Member Author

Hmmm good catch!

def test_pytest_fail_notrace_non_ascii_bytes(testdir):
    testdir.makepyfile(u"""
        # coding: utf-8
        import pytest

        def test_hello():
            pytest.fail('oh oh: ☺', pytrace=False)
    """)
    result = testdir.runpytest()
    if sys.version_info[0] >= 3:
        result.stdout.fnmatch_lines(['*test_hello*', "oh oh: ☺"])
    else:
        result.stdout.fnmatch_lines(['*test_hello*', "oh oh: *"])
    assert 'def test_hello' not in result.stdout.str()

Fails:

    and: u'INTERNALERROR>   File "X:\\pytest\\_pytest\\python.py", line 743, in _repr_failure_py'
    and: u'INTERNALERROR>     return py._builtin._totext(excinfo.value)'
    and: u'INTERNALERROR>   File "X:\\pytest\\_pytest\\runner.py", line 438, in __repr__'
    and: u'INTERNALERROR>     return py._builtin._totext(self.msg)'
    and: u"INTERNALERROR> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 7: ordinal not in range(128)"

Not sure what the appropriate fix for this issue would be then (totext might not be it). Do you have a suggestion? I will think a little more about it meanwhile.

@nicoddemus
Copy link
Member Author

This seems to solve it:

diff --git a/_pytest/runner.py b/_pytest/runner.py
index 4f66aa9..cde94c8 100644
--- a/_pytest/runner.py
+++ b/_pytest/runner.py
@@ -435,7 +435,10 @@ class OutcomeException(Exception):

     def __repr__(self):
         if self.msg:
-            return py._builtin._totext(self.msg)
+            val = self.msg
+            if isinstance(val, bytes):
+                val = py._builtin._totext(val, errors='replace')
+            return val
         return "<%s instance>" %(self.__class__.__name__,)
     __str__ = __repr__

What do you think @RonnyPfannschmidt?

EDIT: updated diff

@RonnyPfannschmidt
Copy link
Member

I'm fine e with anything that works in the demonstrated error cases

I suspect at least another upcoming overhaul

@nicoddemus
Copy link
Member Author

Done

@RonnyPfannschmidt
Copy link
Member

👍

RonnyPfannschmidt added a commit that referenced this pull request Mar 6, 2016
Support pytest.fail with non-ascii characters

Fixes #1178
@RonnyPfannschmidt RonnyPfannschmidt merged commit fd0010e into master Mar 6, 2016
@RonnyPfannschmidt RonnyPfannschmidt deleted the fix-1178 branch March 6, 2016 05:42
bluetech added a commit to bluetech/pytest that referenced this pull request Jul 17, 2019
It seems to have been added in pytest-dev#1439 to fix pytest-dev#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.
bluetech added a commit to bluetech/pytest that referenced this pull request Jul 17, 2019
It seems to have been added in pytest-dev#1439 to fix pytest-dev#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants