skipping: use plain compile() instead of _pytest._code.compile()

eval() is used for evaluating string conditions in skipif/xfail e.g.

    @pytest.mark.skipif("1 == 0")

This is the only code that uses `_pytest._code.compile()`, so removing
its last use enables us to remove it entirely.

In this case it doesn't add much. Plain compile() gives a good enough
error message.

For regular exceptions, the message is the same.

For SyntaxError exceptions, e.g. "1 ==", the previous code adds a little
bit of useful context:

```
invalid syntax (skipping.py:108>, line 1)

The above exception was the direct cause of the following exception:
1 ==
     ^
(code was compiled probably from here: <0-codegen /pytest/src/_pytest/skipping.py:108>) (line 1)

During handling of the above exception, another exception occurred:
Error evaluating 'skipif' condition
    1 ==
         ^
SyntaxError: invalid syntax
```

The new code loses it:

```
unexpected EOF while parsing (<skipif condition>, line 1)

During handling of the above exception, another exception occurred:
Error evaluating 'skipif' condition
    1 ==
        ^
SyntaxError: invalid syntax
```

Since the old message is a minor improvement to an unlikely error
condition in a deprecated feature, I think it is not worth all the code
that it requires.
This commit is contained in:
Ran Benita 2020-07-01 20:20:12 +03:00
parent 4a27d7d973
commit 9640c9c9eb
1 changed files with 2 additions and 2 deletions

View File

@ -9,7 +9,6 @@ from typing import Tuple
import attr
import _pytest._code
from _pytest.compat import TYPE_CHECKING
from _pytest.config import Config
from _pytest.config import hookimpl
@ -105,7 +104,8 @@ def evaluate_condition(item: Item, mark: Mark, condition: object) -> Tuple[bool,
if hasattr(item, "obj"):
globals_.update(item.obj.__globals__) # type: ignore[attr-defined]
try:
condition_code = _pytest._code.compile(condition, mode="eval")
filename = "<{} condition>".format(mark.name)
condition_code = compile(condition, filename, "eval")
result = eval(condition_code, globals_)
except SyntaxError as exc:
msglines = [