From 9640c9c9eb2f1ccdfea67dbfe541bdf3b0b8a128 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 1 Jul 2020 20:20:12 +0300 Subject: [PATCH] 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 (, 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. --- src/_pytest/skipping.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/skipping.py b/src/_pytest/skipping.py index 7bd975e5a..a72bdaabf 100644 --- a/src/_pytest/skipping.py +++ b/src/_pytest/skipping.py @@ -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 = [