[Pytest-commit] Issue #612: Syntax error on assertion reinterpretation (hpk42/pytest)

Alex Gaynor issues-reply at bitbucket.org
Wed Oct 8 23:32:23 CEST 2014


New issue 612: Syntax error on assertion reinterpretation
https://bitbucket.org/hpk42/pytest/issue/612/syntax-error-on-assertion-reinterpretation

Alex Gaynor:

The function with the `assert` statement is:

```
    def _ec_key_set_public_key_affine_coordinates(self, ctx, x, y):
        """
        This is a port of EC_KEY_set_public_key_affine_coordinates that was
        added in 1.0.1.

        Sets the public key point in the EC_KEY context to the affine x and y
        values.
        """

        bn_x = self._int_to_bn(x)
        bn_y = self._int_to_bn(y)

        set_func, get_func, group = (
            self._ec_key_determine_group_get_set_funcs(ctx)
        )

        point = self._lib.EC_POINT_new(group)
        assert point != self._ffi.NULL
        point = self._ffi.gc(point, self._lib.EC_POINT_free)

        with self._tmp_bn_ctx() as bn_ctx:
            check_x = self._lib.BN_CTX_get(bn_ctx)
            check_y = self._lib.BN_CTX_get(bn_ctx)

            res = set_func(group, point, bn_x, bn_y, bn_ctx)
            assert res == 1

            res = get_func(group, point, check_x, check_y, bn_ctx)
            assert res == 1

            assert (
                self._lib.BN_cmp(bn_x, check_x) == 0 and
                self._lib.BN_cmp(bn_y, check_y) == 0
            )

        res = self._lib.EC_KEY_set_public_key(ctx, point)
        assert res == 1

        res = self._lib.EC_KEY_check_key(ctx)
        assert res == 1

        return ctx
```

The specific assertion that's failing is the:

```
            assert (
                self._lib.BN_cmp(bn_x, check_x) == 0 and
                self._lib.BN_cmp(bn_y, check_y) == 0
            )
```

The source code that `py.test` is trying to compile is:

```
self._lib.BN_cmp(bn_x, check_x) == 0 and
self._lib.BN_cmp(bn_y, check_y) == 0
            )
```

which is clearly a syntax error :-) The exact error message from `py.test` is:

```
__ TestPEMSerialization.test_load_pem_ec_private_key[backend1-ec_private_key_encrypted.pem-123456] __

self = <tests.hazmat.primitives.test_serialization.TestPEMSerialization object at 0x108dd8c90>
key_file = 'ec_private_key_encrypted.pem', password = '123456'
backend = <cryptography.hazmat.backends.openssl.backend.Backend object at 0x1089b5f90>

    @pytest.mark.parametrize(
        ("key_file", "password"),
        [
            ("ec_private_key.pem", None),
            ("ec_private_key_encrypted.pem", b"123456"),
        ]
    )
    @pytest.mark.elliptic
    def test_load_pem_ec_private_key(self, key_file, password, backend):
        _skip_curve_unsupported(backend, ec.SECP256R1())
        key = load_vectors_from_file(
            os.path.join(
                "asymmetric", "PEM_Serialization", key_file),
>           lambda pemfile: load_pem_private_key(
                pemfile.read().encode(), password, backend
            )
        )

tests/hazmat/primitives/test_serialization.py:76:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tests/utils.py:102: in load_vectors_from_file
    return loader(vector_file)
tests/hazmat/primitives/test_serialization.py:77: in <lambda>
    pemfile.read().encode(), password, backend
cryptography/hazmat/primitives/serialization.py:59: in load_pem_private_key
    return parser_type(backend).load_object(pem)
cryptography/hazmat/primitives/serialization.py:133: in load_object
    ).private_key(self._backend)
cryptography/hazmat/primitives/asymmetric/ec.py:279: in private_key
    return backend.load_elliptic_curve_private_numbers(self)
cryptography/hazmat/backends/openssl/backend.py:881: in load_elliptic_curve_private_numbers
    ec_cdata, public.x, public.y)
../../.virtualenvs/cryptography-dev/lib/python2.7/site-packages/_pytest/assertion/reinterpret.py:40: in __init__
    self.msg = reinterpret(source, f, should_fail=True)
../../.virtualenvs/cryptography-dev/lib/python2.7/site-packages/_pytest/assertion/newinterpret.py:46: in interpret
    mod = ast.parse(source)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

source = 'self._lib.BN_cmp(bn_x, check_x) == 0 and\nself._lib.BN_cmp(bn_y, check_y) == 0\n            )'
filename = '<unknown>', mode = 'exec'

    def parse(source, filename='<unknown>', mode='exec'):
        """
        Parse the source into an AST node.
        Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
        """
>       return compile(source, filename, mode, PyCF_ONLY_AST)
E         File "<unknown>", line 1
E           self._lib.BN_cmp(bn_x, check_x) == 0 and
E                                                  ^
E       SyntaxError: invalid syntax

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py:37: SyntaxError
```

I haven't been able to further minimize the failure yet.




More information about the pytest-commit mailing list