3.3.0 parametrized node IDs are not ACSII escaped :: breaks PYTEST_CURRENT_TEST in Python 3 · Issue #2957 · pytest-dev/pytest (original) (raw)

Problem

With 3.3.0, it appears that there were some conflicting changes in the formatting of parametrized node IDs which results in errors in Python 3 if the parametrized values contain null bytes.

I noticed this behavior because setting the PYTEST_CURRENT_TEST environment variable now fails in Python 3 in this scenario. Digging deeper, it appears that the immediate reason for this error is this commit that indicates that ascii escaping is no longer necessary because it is sanitized during the parametrization process.

However, looking at the values that it attempts to write to the environment variable and the formatted names for the parametrized tests, it would appear that the parametrized node ID is not actually being ASCII sanitized.

Details

OS

Darwin 186590df9307.ant.amazon.com 16.7.0 Darwin Kernel Version 16.7.0: Wed Oct 4 00:17:00 PDT 2017; root:xnu-3789.71.6~1/RELEASE_X86_64 x86_64

Isolated test code

@pytest.mark.parametrize('plaintext_source, b64_plaintext_with_whitespace, read_bytes', (
    (b'\x00\x00\x00', b'AAAA', 3),
))
def test_base64io_decode_parametrized_null_bytes(plaintext_source, b64_plaintext_with_whitespace, read_bytes):
    with Base64IO(io.BytesIO(b64_plaintext_with_whitespace)) as decoder:
        test = decoder.read(read_bytes)

    assert test == plaintext_source[:read_bytes]

Test/pip list with

pytest 3.2.5

186590df9307:aws-encryption-sdk-cli bullocm$ tox -re py36 test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes -- -v
GLOB sdist-make: /Users/bullocm/git/aws-encryption-sdk-cli/setup.py
py36 recreate: /Users/bullocm/git/aws-encryption-sdk-cli/.tox/py36
py36 installdeps: mock, pytest==3.2.5, pytest-catchlog, pytest-cov, pytest-mock, coverage
py36 inst: /Users/bullocm/git/aws-encryption-sdk-cli/.tox/dist/aws-encryption-sdk-cli-1.1.2.zip
py36 installed: asn1crypto==0.23.0,attrs==17.3.0,aws-encryption-sdk==1.3.2,aws-encryption-sdk-cli==1.1.2,boto3==1.4.8,botocore==1.8.2,cffi==1.11.2,coverage==4.4.2,cryptography==2.1.3,docutils==0.14,idna==2.6,jmespath==0.9.3,mock==2.0.0,pbr==3.1.1,py==1.5.2,pycparser==2.18,pytest==3.2.5,pytest-catchlog==1.2.2,pytest-cov==2.5.1,pytest-mock==1.6.3,python-dateutil==2.6.1,s3transfer==0.1.11,six==1.11.0,typing==3.6.2,wrapt==1.10.11
py36 runtests: PYTHONHASHSEED='309851949'
py36 runtests: commands[0] | coverage run -m pytest --cov aws_encryption_sdk_cli test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes -v
============================================================================================ test session starts =============================================================================================
platform darwin -- Python 3.6.2, pytest-3.2.5, py-1.5.2, pluggy-0.4.0 -- /Users/bullocm/git/aws-encryption-sdk-cli/.tox/py36/bin/python3.6
cachedir: .cache
rootdir: /Users/bullocm/git/aws-encryption-sdk-cli, inifile:
plugins: mock-1.6.3, cov-2.5.1, catchlog-1.2.2
collected 1 item                                                                                                                                                                                              

test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes[\x00\x00\x00-AAAA-3] PASSED

---------- coverage: platform darwin, python 3.6.2-final-0 -----------
Name                                                                                          Stmts   Miss Branch BrPart  Cover   Missing
-----------------------------------------------------------------------------------------------------------------------------------------
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/__init__.py                        118     89     56      0    17%   37-39, 54-59, 69-71, 84-89, 102-108, 125-147, 158-213, 232-246, 255-290
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/exceptions.py                        4      0      0      0   100%
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/__init__.py                 1      0      0      0   100%
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/arg_parsing.py            182    148     56      0    14%   35-37, 51-53, 64-72, 83-86, 93-107, 125-128, 137-140, 154-358, 370-382, 393-397, 408-411, 427-445, 458-475, 489-524, 535-563
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/encoding.py               119     55     42      8    46%   28-30, 67, 94-95, 98, 115-119, 131, 146, 163-180, 188-189, 201-218, 230, 233, 236, 248, 268, 283, 295-300, 305-308, 313, 66->67, 93->94, 97->98, 229->230, 232->233, 235->236, 238->244, 247->248
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/identifiers.py             22      2      2      0    92%   18-20
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/io_handling.py            158    117     44      0    21%   34-36, 48-50, 59-61, 69-71, 80-91, 104-106, 121-127, 138-139, 180-187, 201-238, 251-269, 278-304, 314-348, 359-374
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/logging_utils.py           83     57     10      0    28%   21-23, 48-50, 60-67, 76-81, 91-99, 108-114, 123-128, 137-138, 150-151, 161, 173-181, 191-208
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/master_key_parsing.py      74     51     28      0    23%   31-33, 43-63, 73-75, 87-111, 133-139, 153-155, 166-177, 188-196
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/metadata.py                87     54     32      0    29%   29-31, 60, 71-91, 96, 101-105, 110-111, 116-124, 129, 137-141, 152, 165-186, 200-205
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/mypy_types.py              20      3      2      1    82%   46-49, 43->46
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/key_providers.py                    31     31      4      0     0%   13-69
-----------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                                           899    607    276      9    26%


========================================================================================== 1 passed in 0.60 seconds ==========================================================================================
__________________________________________________________________________________________________ summary ___________________________________________________________________________________________________
  py36: commands succeeded
  congratulations :)
186590df9307:aws-encryption-sdk-cli bullocm$ source .tox/py36/bin/activate
(py36) 186590df9307:aws-encryption-sdk-cli bullocm$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
asn1crypto (0.23.0)
attrs (17.3.0)
aws-encryption-sdk (1.3.2)
aws-encryption-sdk-cli (1.1.2)
boto3 (1.4.8)
botocore (1.8.2)
cffi (1.11.2)
coverage (4.4.2)
cryptography (2.1.3)
docutils (0.14)
idna (2.6)
jmespath (0.9.3)
mock (2.0.0)
pbr (3.1.1)
pip (9.0.1)
py (1.5.2)
pycparser (2.18)
pytest (3.2.5)
pytest-catchlog (1.2.2)
pytest-cov (2.5.1)
pytest-mock (1.6.3)
python-dateutil (2.6.1)
s3transfer (0.1.11)
setuptools (38.2.3)
six (1.11.0)
typing (3.6.2)
wheel (0.30.0)
wrapt (1.10.11)

pytest 3.3.0

186590df9307:aws-encryption-sdk-cli bullocm$ tox -re py36 test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes -- -v
GLOB sdist-make: /Users/bullocm/git/aws-encryption-sdk-cli/setup.py
py36 create: /Users/bullocm/git/aws-encryption-sdk-cli/.tox/py36
py36 installdeps: mock, pytest==3.3.0, pytest-cov, pytest-mock, coverage
py36 inst: /Users/bullocm/git/aws-encryption-sdk-cli/.tox/dist/aws-encryption-sdk-cli-1.1.2.zip
py36 installed: asn1crypto==0.23.0,attrs==17.3.0,aws-encryption-sdk==1.3.2,aws-encryption-sdk-cli==1.1.2,boto3==1.4.8,botocore==1.8.2,cffi==1.11.2,coverage==4.4.2,cryptography==2.1.3,docutils==0.14,idna==2.6,jmespath==0.9.3,mock==2.0.0,pbr==3.1.1,pluggy==0.6.0,py==1.5.2,pycparser==2.18,pytest==3.3.0,pytest-cov==2.5.1,pytest-mock==1.6.3,python-dateutil==2.6.1,s3transfer==0.1.11,six==1.11.0,typing==3.6.2,wrapt==1.10.11
py36 runtests: PYTHONHASHSEED='2217619473'
py36 runtests: commands[0] | coverage run -m pytest --cov aws_encryption_sdk_cli test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes -v
============================================================================================ test session starts =============================================================================================
platform darwin -- Python 3.6.2, pytest-3.3.0, py-1.5.2, pluggy-0.6.0 -- /Users/bullocm/git/aws-encryption-sdk-cli/.tox/py36/bin/python3.6
cachedir: .cache
rootdir: /Users/bullocm/git/aws-encryption-sdk-cli, inifile:
plugins: mock-1.6.3, cov-2.5.1
collected 1 item                                                                                                                                                                                             

test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes[-AAAA-3] ERROR                                                                                                             [100%]
test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes[-AAAA-3] ERROR                                                                                                             [200%]

---------- coverage: platform darwin, python 3.6.2-final-0 -----------
Name                                                                                          Stmts   Miss Branch BrPart  Cover   Missing
-----------------------------------------------------------------------------------------------------------------------------------------
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/__init__.py                        118     89     56      0    17%   37-39, 54-59, 69-71, 84-89, 102-108, 125-147, 158-213, 232-246, 255-290
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/exceptions.py                        4      0      0      0   100%
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/__init__.py                 1      0      0      0   100%
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/arg_parsing.py            182    148     56      0    14%   35-37, 51-53, 64-72, 83-86, 93-107, 125-128, 137-140, 154-358, 370-382, 393-397, 408-411, 427-445, 458-475, 489-524, 535-563
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/encoding.py               119     87     42      0    20%   28-30, 65-72, 77, 82, 93-98, 113-121, 131, 141, 146, 163-180, 188-189, 201-218, 229-261, 268, 283, 295-300, 305-308, 313
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/identifiers.py             22      2      2      0    92%   18-20
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/io_handling.py            158    117     44      0    21%   34-36, 48-50, 59-61, 69-71, 80-91, 104-106, 121-127, 138-139, 180-187, 201-238, 251-269, 278-304, 314-348, 359-374
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/logging_utils.py           83     57     10      0    28%   21-23, 48-50, 60-67, 76-81, 91-99, 108-114, 123-128, 137-138, 150-151, 161, 173-181, 191-208
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/master_key_parsing.py      74     51     28      0    23%   31-33, 43-63, 73-75, 87-111, 133-139, 153-155, 166-177, 188-196
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/metadata.py                87     54     32      0    29%   29-31, 60, 71-91, 96, 101-105, 110-111, 116-124, 129, 137-141, 152, 165-186, 200-205
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/internal/mypy_types.py              20      3      2      1    82%   46-49, 43->46
.tox/py36/lib/python3.6/site-packages/aws_encryption_sdk_cli/key_providers.py                    31     31      4      0     0%   13-69
-----------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                                           899    639    276      1    23%


=================================================================================================== ERRORS ===================================================================================================
_________________________________________________________________ ERROR at setup of test_base64io_decode_parametrized_null_bytes[-AAAA-3] _________________________________________________________________

self = environ({'PATH': '/Users/bullocm/git/aws-encryption-sdk-cli/.tox/py36/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin...ncryption_sdk_cli', 'COV_CORE_CONFIG': '', 'COV_CORE_DATAFILE': '/Users/bullocm/git/aws-encryption-sdk-cli/.coverage'})
key = b'PYTEST_CURRENT_TEST', value = b'test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes[\x00\x00\x00-AAAA-3] (setup)'

    def __setitem__(self, key, value):
        key = self.encodekey(key)
        value = self.encodevalue(value)
>       self.putenv(key, value)
E       ValueError: embedded null byte

.tox/py36/lib/python3.6/os.py:675: ValueError
_______________________________________________________________ ERROR at teardown of test_base64io_decode_parametrized_null_bytes[-AAAA-3] ________________________________________________________________

self = environ({'PATH': '/Users/bullocm/git/aws-encryption-sdk-cli/.tox/py36/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin...ncryption_sdk_cli', 'COV_CORE_CONFIG': '', 'COV_CORE_DATAFILE': '/Users/bullocm/git/aws-encryption-sdk-cli/.coverage'})
key = b'PYTEST_CURRENT_TEST', value = b'test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes[\x00\x00\x00-AAAA-3] (teardown)'

    def __setitem__(self, key, value):
        key = self.encodekey(key)
        value = self.encodevalue(value)
>       self.putenv(key, value)
E       ValueError: embedded null byte

.tox/py36/lib/python3.6/os.py:675: ValueError
========================================================================================== 2 error in 0.71 seconds ===========================================================================================
ERROR: InvocationError: '/Users/bullocm/git/aws-encryption-sdk-cli/.tox/py36/bin/coverage run -m pytest --cov aws_encryption_sdk_cli test/unit/test_encoding.py::test_base64io_decode_parametrized_null_bytes -v'
__________________________________________________________________________________________________ summary ___________________________________________________________________________________________________
ERROR:   py36: commands failed
186590df9307:aws-encryption-sdk-cli bullocm$ source .tox/py36/bin/activate
(py36) 186590df9307:aws-encryption-sdk-cli bullocm$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
asn1crypto (0.23.0)
attrs (17.3.0)
aws-encryption-sdk (1.3.2)
aws-encryption-sdk-cli (1.1.2)
boto3 (1.4.8)
botocore (1.8.2)
cffi (1.11.2)
coverage (4.4.2)
cryptography (2.1.3)
docutils (0.14)
idna (2.6)
jmespath (0.9.3)
mock (2.0.0)
pbr (3.1.1)
pip (9.0.1)
pluggy (0.6.0)
py (1.5.2)
pycparser (2.18)
pytest (3.3.0)
pytest-cov (2.5.1)
pytest-mock (1.6.3)
python-dateutil (2.6.1)
s3transfer (0.1.11)
setuptools (38.2.3)
six (1.11.0)
typing (3.6.2)
wheel (0.30.0)
wrapt (1.10.11)