Skip to content

pytest 2.8.1: wrong error type is caught in _pytest/python.py in def _idval(val, argname, idx, idfn) #1099

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

Closed
sergiy-kozak opened this issue Oct 1, 2015 · 6 comments
Assignees

Comments

@sergiy-kozak
Copy link

In function _idval(val, argname, idx, idfn) of mentioned source file next code is present:

elif _PY2 and isinstance(val, unicode):
    # special case for python 2: if a unicode string is
    # convertible to ascii, return it as an str() object instead
    try:
        return str(val)
    except UnicodeDecodeError:
        # fallthrough
        pass
return str(argname)+str(idx)

In case val variable is unicode value, that contains characters with ordinal > 128, str(val) fails with UnicodeEncodeError, which one would like to catch and fall back to autogenerated parameter id value, like argname1, argname2 etc. The current _idval code causes to the collection time exception, for example UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-5: ordinal not in range(128) due to wrong error type being caught. To fix the problem, replace line

    except UnicodeDecodeError:

with

    except UnicodeEncodeError:

Environment:
platform linux2 -- Python 2.7.9, pytest-2.8.1, py-1.4.30, pluggy-0.3.1 -- /home/myuser/.virtualenvs/test-env/bin/python

@sergiy-kozak
Copy link
Author

Similar problem exists in the following function implementation (in _pytest/python.py):

    def _escape_bytes(val):
        """
        In py2 bytes and str are the same, so return it unchanged if it
        is a full ascii string, otherwise escape it into its binary form.
        """
        try:
            return val.encode('ascii')
        except UnicodeDecodeError:
            return val.encode('string-escape')

which may be fixed by replacing except UnicodeDecodeError: with except UnicodeEncodeError:

@The-Compiler
Copy link
Member

It seems at least the first case was fixed in #1092

@nicoddemus
Copy link
Member

@sergiy-kozak could you try with latest master please? You can install it directly into a virtual environment:

pip install git+https://github.com/pytest-dev/pytest.git

@sergiy-kozak
Copy link
Author

I'll give it a try and report here.

@sergiy-kozak
Copy link
Author

I tested with the latest dev pytest code. I confirm the fix in #1092 is sufficiently solving the issue, hence #1099 can be closed as the duplicate. Thanks everyone for replying on this issue.

@nicoddemus
Copy link
Member

Thanks! 😄

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

No branches or pull requests

3 participants