Store mark's evalcache in config's store instead of attribute

Part of moving away from ad-hoc attributes to using the config's store.
This commit is contained in:
Ran Benita 2020-03-05 23:25:04 +02:00
parent b1d7a187f2
commit f011bc642c
1 changed files with 12 additions and 5 deletions

View File

@ -2,21 +2,28 @@ import os
import platform
import sys
import traceback
from typing import Any
from typing import Dict
from ..outcomes import fail
from ..outcomes import TEST_OUTCOME
from _pytest.config import Config
from _pytest.store import StoreKey
def cached_eval(config, expr, d):
if not hasattr(config, "_evalcache"):
config._evalcache = {}
evalcache_key = StoreKey[Dict[str, Any]]()
def cached_eval(config: Config, expr: str, d: Dict[str, object]) -> Any:
default = {} # type: Dict[str, object]
evalcache = config._store.setdefault(evalcache_key, default)
try:
return config._evalcache[expr]
return evalcache[expr]
except KeyError:
import _pytest._code
exprcode = _pytest._code.compile(expr, mode="eval")
config._evalcache[expr] = x = eval(exprcode, d)
evalcache[expr] = x = eval(exprcode, d)
return x