Skip to content

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

Closed
@sergiy-kozak

Description

@sergiy-kozak

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions