From c31e1a379700873267dfb520f74f0cb62713e736 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 13:36:33 +0100 Subject: [PATCH 01/11] turn mark into a package --- _pytest/{mark.py => mark/__init__.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename _pytest/{mark.py => mark/__init__.py} (99%) diff --git a/_pytest/mark.py b/_pytest/mark/__init__.py similarity index 99% rename from _pytest/mark.py rename to _pytest/mark/__init__.py index 3cac9dc91..d2d601e9e 100644 --- a/_pytest/mark.py +++ b/_pytest/mark/__init__.py @@ -10,8 +10,8 @@ from operator import attrgetter from six.moves import map from _pytest.config import UsageError -from .deprecated import MARK_PARAMETERSET_UNPACKING -from .compat import NOTSET, getfslineno +from ..deprecated import MARK_PARAMETERSET_UNPACKING +from ..compat import NOTSET, getfslineno EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark" From cf40c0743c565ed25bc14753e2350e010b39025a Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 14:05:10 +0100 Subject: [PATCH 02/11] move mark evaluator into mark package --- _pytest/mark/evaluate.py | 126 +++++++++++++++++++++++++++++++++++++++ _pytest/skipping.py | 125 +------------------------------------- 2 files changed, 128 insertions(+), 123 deletions(-) create mode 100644 _pytest/mark/evaluate.py diff --git a/_pytest/mark/evaluate.py b/_pytest/mark/evaluate.py new file mode 100644 index 000000000..27eaa78b3 --- /dev/null +++ b/_pytest/mark/evaluate.py @@ -0,0 +1,126 @@ +import os +import six +import sys +import traceback + +from . import MarkDecorator, MarkInfo +from ..outcomes import fail, TEST_OUTCOME + + +def cached_eval(config, expr, d): + if not hasattr(config, '_evalcache'): + config._evalcache = {} + try: + return config._evalcache[expr] + except KeyError: + import _pytest._code + exprcode = _pytest._code.compile(expr, mode="eval") + config._evalcache[expr] = x = eval(exprcode, d) + return x + + +class MarkEvaluator(object): + def __init__(self, item, name): + self.item = item + self._marks = None + self._mark = None + self._mark_name = name + + def __bool__(self): + self._marks = self._get_marks() + return bool(self._marks) + __nonzero__ = __bool__ + + def wasvalid(self): + return not hasattr(self, 'exc') + + def _get_marks(self): + + keyword = self.item.keywords.get(self._mark_name) + if isinstance(keyword, MarkDecorator): + return [keyword.mark] + elif isinstance(keyword, MarkInfo): + return [x.combined for x in keyword] + else: + return [] + + def invalidraise(self, exc): + raises = self.get('raises') + if not raises: + return + return not isinstance(exc, raises) + + def istrue(self): + try: + return self._istrue() + except TEST_OUTCOME: + self.exc = sys.exc_info() + if isinstance(self.exc[1], SyntaxError): + msg = [" " * (self.exc[1].offset + 4) + "^", ] + msg.append("SyntaxError: invalid syntax") + else: + msg = traceback.format_exception_only(*self.exc[:2]) + fail("Error evaluating %r expression\n" + " %s\n" + "%s" + % (self._mark_name, self.expr, "\n".join(msg)), + pytrace=False) + + def _getglobals(self): + d = {'os': os, 'sys': sys, 'config': self.item.config} + if hasattr(self.item, 'obj'): + d.update(self.item.obj.__globals__) + return d + + def _istrue(self): + if hasattr(self, 'result'): + return self.result + self._marks = self._get_marks() + + if self._marks: + self.result = False + for mark in self._marks: + self._mark = mark + if 'condition' in mark.kwargs: + args = (mark.kwargs['condition'],) + else: + args = mark.args + + for expr in args: + self.expr = expr + if isinstance(expr, six.string_types): + d = self._getglobals() + result = cached_eval(self.item.config, expr, d) + else: + if "reason" not in mark.kwargs: + # XXX better be checked at collection time + msg = "you need to specify reason=STRING " \ + "when using booleans as conditions." + fail(msg) + result = bool(expr) + if result: + self.result = True + self.reason = mark.kwargs.get('reason', None) + self.expr = expr + return self.result + + if not args: + self.result = True + self.reason = mark.kwargs.get('reason', None) + return self.result + return False + + def get(self, attr, default=None): + if self._mark is None: + return default + return self._mark.kwargs.get(attr, default) + + def getexplanation(self): + expl = getattr(self, 'reason', None) or self.get('reason', None) + if not expl: + if not hasattr(self, 'expr'): + return "" + else: + return "condition: " + str(self.expr) + return expl + diff --git a/_pytest/skipping.py b/_pytest/skipping.py index 98fc51c7f..588b2a4a0 100644 --- a/_pytest/skipping.py +++ b/_pytest/skipping.py @@ -1,14 +1,11 @@ """ support for skip/xfail functions and markers. """ from __future__ import absolute_import, division, print_function -import os -import six -import sys -import traceback from _pytest.config import hookimpl from _pytest.mark import MarkInfo, MarkDecorator -from _pytest.outcomes import fail, skip, xfail, TEST_OUTCOME +from _pytest.mark.evaluate import MarkEvaluator +from _pytest.outcomes import fail, skip, xfail def pytest_addoption(parser): @@ -60,112 +57,6 @@ def pytest_configure(config): ) -class MarkEvaluator(object): - def __init__(self, item, name): - self.item = item - self._marks = None - self._mark = None - self._mark_name = name - - def __bool__(self): - self._marks = self._get_marks() - return bool(self._marks) - __nonzero__ = __bool__ - - def wasvalid(self): - return not hasattr(self, 'exc') - - def _get_marks(self): - - keyword = self.item.keywords.get(self._mark_name) - if isinstance(keyword, MarkDecorator): - return [keyword.mark] - elif isinstance(keyword, MarkInfo): - return [x.combined for x in keyword] - else: - return [] - - def invalidraise(self, exc): - raises = self.get('raises') - if not raises: - return - return not isinstance(exc, raises) - - def istrue(self): - try: - return self._istrue() - except TEST_OUTCOME: - self.exc = sys.exc_info() - if isinstance(self.exc[1], SyntaxError): - msg = [" " * (self.exc[1].offset + 4) + "^", ] - msg.append("SyntaxError: invalid syntax") - else: - msg = traceback.format_exception_only(*self.exc[:2]) - fail("Error evaluating %r expression\n" - " %s\n" - "%s" - % (self._mark_name, self.expr, "\n".join(msg)), - pytrace=False) - - def _getglobals(self): - d = {'os': os, 'sys': sys, 'config': self.item.config} - if hasattr(self.item, 'obj'): - d.update(self.item.obj.__globals__) - return d - - def _istrue(self): - if hasattr(self, 'result'): - return self.result - self._marks = self._get_marks() - - if self._marks: - self.result = False - for mark in self._marks: - self._mark = mark - if 'condition' in mark.kwargs: - args = (mark.kwargs['condition'],) - else: - args = mark.args - - for expr in args: - self.expr = expr - if isinstance(expr, six.string_types): - d = self._getglobals() - result = cached_eval(self.item.config, expr, d) - else: - if "reason" not in mark.kwargs: - # XXX better be checked at collection time - msg = "you need to specify reason=STRING " \ - "when using booleans as conditions." - fail(msg) - result = bool(expr) - if result: - self.result = True - self.reason = mark.kwargs.get('reason', None) - self.expr = expr - return self.result - - if not args: - self.result = True - self.reason = mark.kwargs.get('reason', None) - return self.result - return False - - def get(self, attr, default=None): - if self._mark is None: - return default - return self._mark.kwargs.get(attr, default) - - def getexplanation(self): - expl = getattr(self, 'reason', None) or self.get('reason', None) - if not expl: - if not hasattr(self, 'expr'): - return "" - else: - return "condition: " + str(self.expr) - return expl - - @hookimpl(tryfirst=True) def pytest_runtest_setup(item): # Check if skip or skipif are specified as pytest marks @@ -341,18 +232,6 @@ def show_xpassed(terminalreporter, lines): lines.append("XPASS %s %s" % (pos, reason)) -def cached_eval(config, expr, d): - if not hasattr(config, '_evalcache'): - config._evalcache = {} - try: - return config._evalcache[expr] - except KeyError: - import _pytest._code - exprcode = _pytest._code.compile(expr, mode="eval") - config._evalcache[expr] = x = eval(exprcode, d) - return x - - def folded_skips(skipped): d = {} for event in skipped: From 25a3e9296adb3b29483895ec94f2a88616819207 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 14:18:49 +0100 Subject: [PATCH 03/11] reduce the complexity of skipping terminal summary --- _pytest/skipping.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/_pytest/skipping.py b/_pytest/skipping.py index 588b2a4a0..1a4187c1b 100644 --- a/_pytest/skipping.py +++ b/_pytest/skipping.py @@ -185,18 +185,8 @@ def pytest_terminal_summary(terminalreporter): lines = [] for char in tr.reportchars: - if char == "x": - show_xfailed(terminalreporter, lines) - elif char == "X": - show_xpassed(terminalreporter, lines) - elif char in "fF": - show_simple(terminalreporter, lines, 'failed', "FAIL %s") - elif char in "sS": - show_skipped(terminalreporter, lines) - elif char == "E": - show_simple(terminalreporter, lines, 'error', "ERROR %s") - elif char == 'p': - show_simple(terminalreporter, lines, 'passed', "PASSED %s") + action = REPORTCHAR_ACTIONS.get(char, lambda tr, lines: None) + action(terminalreporter, lines) if lines: tr._tw.sep("=", "short test summary info") @@ -274,3 +264,22 @@ def show_skipped(terminalreporter, lines): lines.append( "SKIP [%d] %s: %s" % (num, fspath, reason)) + + +def shower(stat, format): + def show_(terminalreporter, lines): + return show_simple(terminalreporter, lines, stat, format) + return show_ + + +REPORTCHAR_ACTIONS = { + 'x': show_xfailed, + 'X': show_xpassed, + 'f': shower('failed', "FAIL %s"), + 'F': shower('failed', "FAIL %s"), + 's': show_skipped, + 'S': show_skipped, + 'p': shower('passed', "PASSED %s"), + 'E': shower('error', "ERROR %s") + +} From de2de00de90271049424f819092e052ca88a10ac Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 14:19:31 +0100 Subject: [PATCH 04/11] update setup.py for the mark package --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e2013f3fc..78b3ebc5e 100644 --- a/setup.py +++ b/setup.py @@ -101,7 +101,7 @@ def main(): python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', install_requires=install_requires, extras_require=extras_require, - packages=['_pytest', '_pytest.assertion', '_pytest._code'], + packages=['_pytest', '_pytest.assertion', '_pytest._code', '_pytest.mark'], py_modules=['pytest'], zip_safe=False, ) From cef0423b2704e8d03eeef918751b79387dee69d8 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 14:26:22 +0100 Subject: [PATCH 05/11] move the keyword/mark matching to the "legacy" module --- _pytest/mark/__init__.py | 85 +----------------------------------- _pytest/mark/legacy.py | 93 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 84 deletions(-) create mode 100644 _pytest/mark/legacy.py diff --git a/_pytest/mark/__init__.py b/_pytest/mark/__init__.py index d2d601e9e..54d0e6447 100644 --- a/_pytest/mark/__init__.py +++ b/_pytest/mark/__init__.py @@ -2,7 +2,6 @@ from __future__ import absolute_import, division, print_function import inspect -import keyword import warnings import attr from collections import namedtuple @@ -174,6 +173,7 @@ pytest_cmdline_main.tryfirst = True def pytest_collection_modifyitems(items, config): + from .legacy import matchkeyword, matchmark keywordexpr = config.option.keyword.lstrip() matchexpr = config.option.markexpr if not keywordexpr and not matchexpr: @@ -207,89 +207,6 @@ def pytest_collection_modifyitems(items, config): items[:] = remaining -@attr.s -class MarkMapping(object): - """Provides a local mapping for markers where item access - resolves to True if the marker is present. """ - - own_mark_names = attr.ib() - - @classmethod - def from_keywords(cls, keywords): - mark_names = set() - for key, value in keywords.items(): - if isinstance(value, MarkInfo) or isinstance(value, MarkDecorator): - mark_names.add(key) - return cls(mark_names) - - def __getitem__(self, name): - return name in self.own_mark_names - - -class KeywordMapping(object): - """Provides a local mapping for keywords. - Given a list of names, map any substring of one of these names to True. - """ - - def __init__(self, names): - self._names = names - - def __getitem__(self, subname): - for name in self._names: - if subname in name: - return True - return False - - -python_keywords_allowed_list = ["or", "and", "not"] - - -def matchmark(colitem, markexpr): - """Tries to match on any marker names, attached to the given colitem.""" - return eval(markexpr, {}, MarkMapping.from_keywords(colitem.keywords)) - - -def matchkeyword(colitem, keywordexpr): - """Tries to match given keyword expression to given collector item. - - Will match on the name of colitem, including the names of its parents. - Only matches names of items which are either a :class:`Class` or a - :class:`Function`. - Additionally, matches on names in the 'extra_keyword_matches' set of - any item, as well as names directly assigned to test functions. - """ - mapped_names = set() - - # Add the names of the current item and any parent items - import pytest - for item in colitem.listchain(): - if not isinstance(item, pytest.Instance): - mapped_names.add(item.name) - - # Add the names added as extra keywords to current or parent items - for name in colitem.listextrakeywords(): - mapped_names.add(name) - - # Add the names attached to the current function through direct assignment - if hasattr(colitem, 'function'): - for name in colitem.function.__dict__: - mapped_names.add(name) - - mapping = KeywordMapping(mapped_names) - if " " not in keywordexpr: - # special case to allow for simple "-k pass" and "-k 1.3" - return mapping[keywordexpr] - elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]: - return not mapping[keywordexpr[4:]] - for kwd in keywordexpr.split(): - if keyword.iskeyword(kwd) and kwd not in python_keywords_allowed_list: - raise UsageError("Python keyword '{}' not accepted in expressions passed to '-k'".format(kwd)) - try: - return eval(keywordexpr, {}, mapping) - except SyntaxError: - raise UsageError("Wrong expression passed to '-k': {}".format(keywordexpr)) - - def pytest_configure(config): config._old_mark_config = MARK_GEN._config if config.option.strict: diff --git a/_pytest/mark/legacy.py b/_pytest/mark/legacy.py new file mode 100644 index 000000000..8c9e86d10 --- /dev/null +++ b/_pytest/mark/legacy.py @@ -0,0 +1,93 @@ +""" +this is a place where we put datastructures used by legacy apis +we hope ot remove +""" +import attr +import keyword + +from . import MarkInfo, MarkDecorator + +from _pytest.config import UsageError + + +@attr.s +class MarkMapping(object): + """Provides a local mapping for markers where item access + resolves to True if the marker is present. """ + + own_mark_names = attr.ib() + + @classmethod + def from_keywords(cls, keywords): + mark_names = set() + for key, value in keywords.items(): + if isinstance(value, MarkInfo) or isinstance(value, MarkDecorator): + mark_names.add(key) + return cls(mark_names) + + def __getitem__(self, name): + return name in self.own_mark_names + + +class KeywordMapping(object): + """Provides a local mapping for keywords. + Given a list of names, map any substring of one of these names to True. + """ + + def __init__(self, names): + self._names = names + + def __getitem__(self, subname): + for name in self._names: + if subname in name: + return True + return False + + +python_keywords_allowed_list = ["or", "and", "not"] + + +def matchmark(colitem, markexpr): + """Tries to match on any marker names, attached to the given colitem.""" + return eval(markexpr, {}, MarkMapping.from_keywords(colitem.keywords)) + + +def matchkeyword(colitem, keywordexpr): + """Tries to match given keyword expression to given collector item. + + Will match on the name of colitem, including the names of its parents. + Only matches names of items which are either a :class:`Class` or a + :class:`Function`. + Additionally, matches on names in the 'extra_keyword_matches' set of + any item, as well as names directly assigned to test functions. + """ + mapped_names = set() + + # Add the names of the current item and any parent items + import pytest + for item in colitem.listchain(): + if not isinstance(item, pytest.Instance): + mapped_names.add(item.name) + + # Add the names added as extra keywords to current or parent items + for name in colitem.listextrakeywords(): + mapped_names.add(name) + + # Add the names attached to the current function through direct assignment + if hasattr(colitem, 'function'): + for name in colitem.function.__dict__: + mapped_names.add(name) + + mapping = KeywordMapping(mapped_names) + if " " not in keywordexpr: + # special case to allow for simple "-k pass" and "-k 1.3" + return mapping[keywordexpr] + elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]: + return not mapping[keywordexpr[4:]] + for kwd in keywordexpr.split(): + if keyword.iskeyword(kwd) and kwd not in python_keywords_allowed_list: + raise UsageError("Python keyword '{}' not accepted in expressions passed to '-k'".format(kwd)) + try: + return eval(keywordexpr, {}, mapping) + except SyntaxError: + raise UsageError("Wrong expression passed to '-k': {}".format(keywordexpr)) From be2e3a973e34373533ffbea6d7d1f6554cdb6f5f Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 14:30:32 +0100 Subject: [PATCH 06/11] remove complexity from match_keywords --- _pytest/mark/legacy.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/_pytest/mark/legacy.py b/_pytest/mark/legacy.py index 8c9e86d10..ec45f12af 100644 --- a/_pytest/mark/legacy.py +++ b/_pytest/mark/legacy.py @@ -37,6 +37,27 @@ class KeywordMapping(object): def __init__(self, names): self._names = names + @classmethod + def from_item(cls, item): + mapped_names = set() + + # Add the names of the current item and any parent items + import pytest + for item in item.listchain(): + if not isinstance(item, pytest.Instance): + mapped_names.add(item.name) + + # Add the names added as extra keywords to current or parent items + for name in item.listextrakeywords(): + mapped_names.add(name) + + # Add the names attached to the current function through direct assignment + if hasattr(item, 'function'): + for name in item.function.__dict__: + mapped_names.add(name) + + return cls(mapped_names) + def __getitem__(self, subname): for name in self._names: if subname in name: @@ -61,24 +82,7 @@ def matchkeyword(colitem, keywordexpr): Additionally, matches on names in the 'extra_keyword_matches' set of any item, as well as names directly assigned to test functions. """ - mapped_names = set() - - # Add the names of the current item and any parent items - import pytest - for item in colitem.listchain(): - if not isinstance(item, pytest.Instance): - mapped_names.add(item.name) - - # Add the names added as extra keywords to current or parent items - for name in colitem.listextrakeywords(): - mapped_names.add(name) - - # Add the names attached to the current function through direct assignment - if hasattr(colitem, 'function'): - for name in colitem.function.__dict__: - mapped_names.add(name) - - mapping = KeywordMapping(mapped_names) + mapping = KeywordMapping.from_item(colitem) if " " not in keywordexpr: # special case to allow for simple "-k pass" and "-k 1.3" return mapping[keywordexpr] From c8d24739ed3ddfa491ca40f9966440f4391f2ad0 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 14:43:01 +0100 Subject: [PATCH 07/11] move current mark datastructures to own module --- _pytest/mark/__init__.py | 335 +------------------------------------ _pytest/mark/structures.py | 330 ++++++++++++++++++++++++++++++++++++ _pytest/python.py | 2 +- 3 files changed, 336 insertions(+), 331 deletions(-) create mode 100644 _pytest/mark/structures.py diff --git a/_pytest/mark/__init__.py b/_pytest/mark/__init__.py index 54d0e6447..cf26c2683 100644 --- a/_pytest/mark/__init__.py +++ b/_pytest/mark/__init__.py @@ -1,112 +1,11 @@ """ generic mechanism for marking and selecting python functions. """ from __future__ import absolute_import, division, print_function - -import inspect -import warnings -import attr -from collections import namedtuple -from operator import attrgetter -from six.moves import map - from _pytest.config import UsageError -from ..deprecated import MARK_PARAMETERSET_UNPACKING -from ..compat import NOTSET, getfslineno - -EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark" - - -def alias(name, warning=None): - getter = attrgetter(name) - - def warned(self): - warnings.warn(warning, stacklevel=2) - return getter(self) - - return property(getter if warning is None else warned, doc='alias for ' + name) - - -class ParameterSet(namedtuple('ParameterSet', 'values, marks, id')): - @classmethod - def param(cls, *values, **kw): - marks = kw.pop('marks', ()) - if isinstance(marks, MarkDecorator): - marks = marks, - else: - assert isinstance(marks, (tuple, list, set)) - - def param_extract_id(id=None): - return id - - id = param_extract_id(**kw) - return cls(values, marks, id) - - @classmethod - def extract_from(cls, parameterset, legacy_force_tuple=False): - """ - :param parameterset: - a legacy style parameterset that may or may not be a tuple, - and may or may not be wrapped into a mess of mark objects - - :param legacy_force_tuple: - enforce tuple wrapping so single argument tuple values - don't get decomposed and break tests - - """ - - if isinstance(parameterset, cls): - return parameterset - if not isinstance(parameterset, MarkDecorator) and legacy_force_tuple: - return cls.param(parameterset) - - newmarks = [] - argval = parameterset - while isinstance(argval, MarkDecorator): - newmarks.append(MarkDecorator(Mark( - argval.markname, argval.args[:-1], argval.kwargs))) - argval = argval.args[-1] - assert not isinstance(argval, ParameterSet) - if legacy_force_tuple: - argval = argval, - - if newmarks: - warnings.warn(MARK_PARAMETERSET_UNPACKING) - - return cls(argval, marks=newmarks, id=None) - - @classmethod - def _for_parametrize(cls, argnames, argvalues, function, config): - if not isinstance(argnames, (tuple, list)): - argnames = [x.strip() for x in argnames.split(",") if x.strip()] - force_tuple = len(argnames) == 1 - else: - force_tuple = False - parameters = [ - ParameterSet.extract_from(x, legacy_force_tuple=force_tuple) - for x in argvalues] - del argvalues - - if not parameters: - mark = get_empty_parameterset_mark(config, argnames, function) - parameters.append(ParameterSet( - values=(NOTSET,) * len(argnames), - marks=[mark], - id=None, - )) - return argnames, parameters - - -def get_empty_parameterset_mark(config, argnames, function): - requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION) - if requested_mark in ('', None, 'skip'): - mark = MARK_GEN.skip - elif requested_mark == 'xfail': - mark = MARK_GEN.xfail(run=False) - else: - raise LookupError(requested_mark) - fs, lineno = getfslineno(function) - reason = "got empty parameter set %r, function %s at %s:%d" % ( - argnames, function.__name__, fs, lineno) - return mark(reason=reason) +from .structures import ( + ParameterSet, EMPTY_PARAMETERSET_OPTION, MARK_GEN, + Mark, MarkInfo, MarkDecorator, +) +__all__ = ['Mark', 'MarkInfo', 'MarkDecorator'] class MarkerError(Exception): @@ -222,227 +121,3 @@ def pytest_configure(config): def pytest_unconfigure(config): MARK_GEN._config = getattr(config, '_old_mark_config', None) - - -class MarkGenerator(object): - """ Factory for :class:`MarkDecorator` objects - exposed as - a ``pytest.mark`` singleton instance. Example:: - - import pytest - @pytest.mark.slowtest - def test_function(): - pass - - will set a 'slowtest' :class:`MarkInfo` object - on the ``test_function`` object. """ - _config = None - - def __getattr__(self, name): - if name[0] == "_": - raise AttributeError("Marker name must NOT start with underscore") - if self._config is not None: - self._check(name) - return MarkDecorator(Mark(name, (), {})) - - def _check(self, name): - try: - if name in self._markers: - return - except AttributeError: - pass - self._markers = values = set() - for line in self._config.getini("markers"): - marker = line.split(":", 1)[0] - marker = marker.rstrip() - x = marker.split("(", 1)[0] - values.add(x) - if name not in self._markers: - raise AttributeError("%r not a registered marker" % (name,)) - - -def istestfunc(func): - return hasattr(func, "__call__") and \ - getattr(func, "__name__", "") != "" - - -@attr.s(frozen=True) -class Mark(object): - name = attr.ib() - args = attr.ib() - kwargs = attr.ib() - - def combined_with(self, other): - assert self.name == other.name - return Mark( - self.name, self.args + other.args, - dict(self.kwargs, **other.kwargs)) - - -@attr.s -class MarkDecorator(object): - """ A decorator for test functions and test classes. When applied - it will create :class:`MarkInfo` objects which may be - :ref:`retrieved by hooks as item keywords `. - MarkDecorator instances are often created like this:: - - mark1 = pytest.mark.NAME # simple MarkDecorator - mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator - - and can then be applied as decorators to test functions:: - - @mark2 - def test_function(): - pass - - When a MarkDecorator instance is called it does the following: - 1. If called with a single class as its only positional argument and no - additional keyword arguments, it attaches itself to the class so it - gets applied automatically to all test cases found in that class. - 2. If called with a single function as its only positional argument and - no additional keyword arguments, it attaches a MarkInfo object to the - function, containing all the arguments already stored internally in - the MarkDecorator. - 3. When called in any other case, it performs a 'fake construction' call, - i.e. it returns a new MarkDecorator instance with the original - MarkDecorator's content updated with the arguments passed to this - call. - - Note: The rules above prevent MarkDecorator objects from storing only a - single function or class reference as their positional argument with no - additional keyword or positional arguments. - - """ - - mark = attr.ib(validator=attr.validators.instance_of(Mark)) - - name = alias('mark.name') - args = alias('mark.args') - kwargs = alias('mark.kwargs') - - @property - def markname(self): - return self.name # for backward-compat (2.4.1 had this attr) - - def __eq__(self, other): - return self.mark == other.mark if isinstance(other, MarkDecorator) else False - - def __repr__(self): - return "" % (self.mark,) - - def with_args(self, *args, **kwargs): - """ return a MarkDecorator with extra arguments added - - unlike call this can be used even if the sole argument is a callable/class - - :return: MarkDecorator - """ - - mark = Mark(self.name, args, kwargs) - return self.__class__(self.mark.combined_with(mark)) - - def __call__(self, *args, **kwargs): - """ if passed a single callable argument: decorate it with mark info. - otherwise add *args/**kwargs in-place to mark information. """ - if args and not kwargs: - func = args[0] - is_class = inspect.isclass(func) - if len(args) == 1 and (istestfunc(func) or is_class): - if is_class: - store_mark(func, self.mark) - else: - store_legacy_markinfo(func, self.mark) - store_mark(func, self.mark) - return func - return self.with_args(*args, **kwargs) - - -def get_unpacked_marks(obj): - """ - obtain the unpacked marks that are stored on a object - """ - mark_list = getattr(obj, 'pytestmark', []) - - if not isinstance(mark_list, list): - mark_list = [mark_list] - return [ - getattr(mark, 'mark', mark) # unpack MarkDecorator - for mark in mark_list - ] - - -def store_mark(obj, mark): - """store a Mark on a object - this is used to implement the Mark declarations/decorators correctly - """ - assert isinstance(mark, Mark), mark - # always reassign name to avoid updating pytestmark - # in a reference that was only borrowed - obj.pytestmark = get_unpacked_marks(obj) + [mark] - - -def store_legacy_markinfo(func, mark): - """create the legacy MarkInfo objects and put them onto the function - """ - if not isinstance(mark, Mark): - raise TypeError("got {mark!r} instead of a Mark".format(mark=mark)) - holder = getattr(func, mark.name, None) - if holder is None: - holder = MarkInfo(mark) - setattr(func, mark.name, holder) - else: - holder.add_mark(mark) - - -class MarkInfo(object): - """ Marking object created by :class:`MarkDecorator` instances. """ - - def __init__(self, mark): - assert isinstance(mark, Mark), repr(mark) - self.combined = mark - self._marks = [mark] - - name = alias('combined.name') - args = alias('combined.args') - kwargs = alias('combined.kwargs') - - def __repr__(self): - return "".format(self.combined) - - def add_mark(self, mark): - """ add a MarkInfo with the given args and kwargs. """ - self._marks.append(mark) - self.combined = self.combined.combined_with(mark) - - def __iter__(self): - """ yield MarkInfo objects each relating to a marking-call. """ - return map(MarkInfo, self._marks) - - -MARK_GEN = MarkGenerator() - - -def _marked(func, mark): - """ Returns True if :func: is already marked with :mark:, False otherwise. - This can happen if marker is applied to class and the test file is - invoked more than once. - """ - try: - func_mark = getattr(func, mark.name) - except AttributeError: - return False - return mark.args == func_mark.args and mark.kwargs == func_mark.kwargs - - -def transfer_markers(funcobj, cls, mod): - """ - this function transfers class level markers and module level markers - into function level markinfo objects - - this is the main reason why marks are so broken - the resolution will involve phasing out function level MarkInfo objects - - """ - for obj in (cls, mod): - for mark in get_unpacked_marks(obj): - if not _marked(funcobj, mark): - store_legacy_markinfo(funcobj, mark) diff --git a/_pytest/mark/structures.py b/_pytest/mark/structures.py new file mode 100644 index 000000000..5550dc546 --- /dev/null +++ b/_pytest/mark/structures.py @@ -0,0 +1,330 @@ +from collections import namedtuple +import warnings +from operator import attrgetter +import inspect + +import attr +from ..deprecated import MARK_PARAMETERSET_UNPACKING +from ..compat import NOTSET, getfslineno +from six.moves import map + + +EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark" + + +def alias(name, warning=None): + getter = attrgetter(name) + + def warned(self): + warnings.warn(warning, stacklevel=2) + return getter(self) + + return property(getter if warning is None else warned, doc='alias for ' + name) + + +def istestfunc(func): + return hasattr(func, "__call__") and \ + getattr(func, "__name__", "") != "" + + +def get_empty_parameterset_mark(config, argnames, function): + requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION) + if requested_mark in ('', None, 'skip'): + mark = MARK_GEN.skip + elif requested_mark == 'xfail': + mark = MARK_GEN.xfail(run=False) + else: + raise LookupError(requested_mark) + fs, lineno = getfslineno(function) + reason = "got empty parameter set %r, function %s at %s:%d" % ( + argnames, function.__name__, fs, lineno) + return mark(reason=reason) + + +class ParameterSet(namedtuple('ParameterSet', 'values, marks, id')): + @classmethod + def param(cls, *values, **kw): + marks = kw.pop('marks', ()) + if isinstance(marks, MarkDecorator): + marks = marks, + else: + assert isinstance(marks, (tuple, list, set)) + + def param_extract_id(id=None): + return id + + id = param_extract_id(**kw) + return cls(values, marks, id) + + @classmethod + def extract_from(cls, parameterset, legacy_force_tuple=False): + """ + :param parameterset: + a legacy style parameterset that may or may not be a tuple, + and may or may not be wrapped into a mess of mark objects + + :param legacy_force_tuple: + enforce tuple wrapping so single argument tuple values + don't get decomposed and break tests + + """ + + if isinstance(parameterset, cls): + return parameterset + if not isinstance(parameterset, MarkDecorator) and legacy_force_tuple: + return cls.param(parameterset) + + newmarks = [] + argval = parameterset + while isinstance(argval, MarkDecorator): + newmarks.append(MarkDecorator(Mark( + argval.markname, argval.args[:-1], argval.kwargs))) + argval = argval.args[-1] + assert not isinstance(argval, ParameterSet) + if legacy_force_tuple: + argval = argval, + + if newmarks: + warnings.warn(MARK_PARAMETERSET_UNPACKING) + + return cls(argval, marks=newmarks, id=None) + + @classmethod + def _for_parametrize(cls, argnames, argvalues, function, config): + if not isinstance(argnames, (tuple, list)): + argnames = [x.strip() for x in argnames.split(",") if x.strip()] + force_tuple = len(argnames) == 1 + else: + force_tuple = False + parameters = [ + ParameterSet.extract_from(x, legacy_force_tuple=force_tuple) + for x in argvalues] + del argvalues + + if not parameters: + mark = get_empty_parameterset_mark(config, argnames, function) + parameters.append(ParameterSet( + values=(NOTSET,) * len(argnames), + marks=[mark], + id=None, + )) + return argnames, parameters + + +@attr.s(frozen=True) +class Mark(object): + name = attr.ib() + args = attr.ib() + kwargs = attr.ib() + + def combined_with(self, other): + assert self.name == other.name + return Mark( + self.name, self.args + other.args, + dict(self.kwargs, **other.kwargs)) + + +@attr.s +class MarkDecorator(object): + """ A decorator for test functions and test classes. When applied + it will create :class:`MarkInfo` objects which may be + :ref:`retrieved by hooks as item keywords `. + MarkDecorator instances are often created like this:: + + mark1 = pytest.mark.NAME # simple MarkDecorator + mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator + + and can then be applied as decorators to test functions:: + + @mark2 + def test_function(): + pass + + When a MarkDecorator instance is called it does the following: + 1. If called with a single class as its only positional argument and no + additional keyword arguments, it attaches itself to the class so it + gets applied automatically to all test cases found in that class. + 2. If called with a single function as its only positional argument and + no additional keyword arguments, it attaches a MarkInfo object to the + function, containing all the arguments already stored internally in + the MarkDecorator. + 3. When called in any other case, it performs a 'fake construction' call, + i.e. it returns a new MarkDecorator instance with the original + MarkDecorator's content updated with the arguments passed to this + call. + + Note: The rules above prevent MarkDecorator objects from storing only a + single function or class reference as their positional argument with no + additional keyword or positional arguments. + + """ + + mark = attr.ib(validator=attr.validators.instance_of(Mark)) + + name = alias('mark.name') + args = alias('mark.args') + kwargs = alias('mark.kwargs') + + @property + def markname(self): + return self.name # for backward-compat (2.4.1 had this attr) + + def __eq__(self, other): + return self.mark == other.mark if isinstance(other, MarkDecorator) else False + + def __repr__(self): + return "" % (self.mark,) + + def with_args(self, *args, **kwargs): + """ return a MarkDecorator with extra arguments added + + unlike call this can be used even if the sole argument is a callable/class + + :return: MarkDecorator + """ + + mark = Mark(self.name, args, kwargs) + return self.__class__(self.mark.combined_with(mark)) + + def __call__(self, *args, **kwargs): + """ if passed a single callable argument: decorate it with mark info. + otherwise add *args/**kwargs in-place to mark information. """ + if args and not kwargs: + func = args[0] + is_class = inspect.isclass(func) + if len(args) == 1 and (istestfunc(func) or is_class): + if is_class: + store_mark(func, self.mark) + else: + store_legacy_markinfo(func, self.mark) + store_mark(func, self.mark) + return func + return self.with_args(*args, **kwargs) + + +def get_unpacked_marks(obj): + """ + obtain the unpacked marks that are stored on a object + """ + mark_list = getattr(obj, 'pytestmark', []) + + if not isinstance(mark_list, list): + mark_list = [mark_list] + return [ + getattr(mark, 'mark', mark) # unpack MarkDecorator + for mark in mark_list + ] + + +def store_mark(obj, mark): + """store a Mark on a object + this is used to implement the Mark declarations/decorators correctly + """ + assert isinstance(mark, Mark), mark + # always reassign name to avoid updating pytestmark + # in a reference that was only borrowed + obj.pytestmark = get_unpacked_marks(obj) + [mark] + + +def store_legacy_markinfo(func, mark): + """create the legacy MarkInfo objects and put them onto the function + """ + if not isinstance(mark, Mark): + raise TypeError("got {mark!r} instead of a Mark".format(mark=mark)) + holder = getattr(func, mark.name, None) + if holder is None: + holder = MarkInfo(mark) + setattr(func, mark.name, holder) + else: + holder.add_mark(mark) + + +def transfer_markers(funcobj, cls, mod): + """ + this function transfers class level markers and module level markers + into function level markinfo objects + + this is the main reason why marks are so broken + the resolution will involve phasing out function level MarkInfo objects + + """ + for obj in (cls, mod): + for mark in get_unpacked_marks(obj): + if not _marked(funcobj, mark): + store_legacy_markinfo(funcobj, mark) + + +def _marked(func, mark): + """ Returns True if :func: is already marked with :mark:, False otherwise. + This can happen if marker is applied to class and the test file is + invoked more than once. + """ + try: + func_mark = getattr(func, mark.name) + except AttributeError: + return False + return mark.args == func_mark.args and mark.kwargs == func_mark.kwargs + + +class MarkInfo(object): + """ Marking object created by :class:`MarkDecorator` instances. """ + + def __init__(self, mark): + assert isinstance(mark, Mark), repr(mark) + self.combined = mark + self._marks = [mark] + + name = alias('combined.name') + args = alias('combined.args') + kwargs = alias('combined.kwargs') + + def __repr__(self): + return "".format(self.combined) + + def add_mark(self, mark): + """ add a MarkInfo with the given args and kwargs. """ + self._marks.append(mark) + self.combined = self.combined.combined_with(mark) + + def __iter__(self): + """ yield MarkInfo objects each relating to a marking-call. """ + return map(MarkInfo, self._marks) + + +class MarkGenerator(object): + """ Factory for :class:`MarkDecorator` objects - exposed as + a ``pytest.mark`` singleton instance. Example:: + + import pytest + @pytest.mark.slowtest + def test_function(): + pass + + will set a 'slowtest' :class:`MarkInfo` object + on the ``test_function`` object. """ + _config = None + + def __getattr__(self, name): + if name[0] == "_": + raise AttributeError("Marker name must NOT start with underscore") + if self._config is not None: + self._check(name) + return MarkDecorator(Mark(name, (), {})) + + def _check(self, name): + try: + if name in self._markers: + return + except AttributeError: + pass + self._markers = values = set() + for line in self._config.getini("markers"): + marker = line.split(":", 1)[0] + marker = marker.rstrip() + x = marker.split("(", 1)[0] + values.add(x) + if name not in self._markers: + raise AttributeError("%r not a registered marker" % (name,)) + + +MARK_GEN = MarkGenerator() diff --git a/_pytest/python.py b/_pytest/python.py index fb7bac8b8..9f633812f 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -28,7 +28,7 @@ from _pytest.compat import ( safe_str, getlocation, enum, ) from _pytest.outcomes import fail -from _pytest.mark import transfer_markers +from _pytest.mark.legacy import transfer_markers # relative paths that we use to filter traceback entries from appearing to the user; From 935dd3aaa5368f3a61f2e4996a09deed6c524406 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 15:11:55 +0100 Subject: [PATCH 08/11] simplify complexyity in mark plugin modifyitems --- _pytest/mark/__init__.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/_pytest/mark/__init__.py b/_pytest/mark/__init__.py index cf26c2683..0856cf926 100644 --- a/_pytest/mark/__init__.py +++ b/_pytest/mark/__init__.py @@ -5,6 +5,8 @@ from .structures import ( ParameterSet, EMPTY_PARAMETERSET_OPTION, MARK_GEN, Mark, MarkInfo, MarkDecorator, ) +from .legacy import matchkeyword, matchmark + __all__ = ['Mark', 'MarkInfo', 'MarkDecorator'] @@ -71,15 +73,8 @@ def pytest_cmdline_main(config): pytest_cmdline_main.tryfirst = True -def pytest_collection_modifyitems(items, config): - from .legacy import matchkeyword, matchmark +def deselect_by_keyword(items, config): keywordexpr = config.option.keyword.lstrip() - matchexpr = config.option.markexpr - if not keywordexpr and not matchexpr: - return - # pytest used to allow "-" for negating - # but today we just allow "-" at the beginning, use "not" instead - # we probably remove "-" altogether soon if keywordexpr.startswith("-"): keywordexpr = "not " + keywordexpr[1:] selectuntil = False @@ -95,10 +90,6 @@ def pytest_collection_modifyitems(items, config): else: if selectuntil: keywordexpr = None - if matchexpr: - if not matchmark(colitem, matchexpr): - deselected.append(colitem) - continue remaining.append(colitem) if deselected: @@ -106,6 +97,29 @@ def pytest_collection_modifyitems(items, config): items[:] = remaining +def deselect_by_mark(items, config): + matchexpr = config.option.markexpr + if not matchexpr: + return + + remaining = [] + deselected = [] + for item in items: + if matchmark(item, matchexpr): + remaining.append(item) + else: + deselected.append(item) + + if deselected: + config.hook.pytest_deselected(items=deselected) + items[:] = remaining + + +def pytest_collection_modifyitems(items, config): + deselect_by_keyword(items, config) + deselect_by_mark(items, config) + + def pytest_configure(config): config._old_mark_config = MARK_GEN._config if config.option.strict: From 2cd69cf632801ac975b55881d7cb3d3c17a09b0a Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 22 Feb 2018 15:13:01 +0100 Subject: [PATCH 09/11] sort out import misstake --- _pytest/mark/__init__.py | 8 ++++++-- _pytest/python.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/_pytest/mark/__init__.py b/_pytest/mark/__init__.py index 0856cf926..f22db5820 100644 --- a/_pytest/mark/__init__.py +++ b/_pytest/mark/__init__.py @@ -3,11 +3,15 @@ from __future__ import absolute_import, division, print_function from _pytest.config import UsageError from .structures import ( ParameterSet, EMPTY_PARAMETERSET_OPTION, MARK_GEN, - Mark, MarkInfo, MarkDecorator, + Mark, MarkInfo, MarkDecorator, MarkGenerator, + transfer_markers, get_empty_parameterset_mark ) from .legacy import matchkeyword, matchmark -__all__ = ['Mark', 'MarkInfo', 'MarkDecorator'] +__all__ = [ + 'Mark', 'MarkInfo', 'MarkDecorator', 'MarkGenerator', + 'transfer_markers', 'get_empty_parameterset_mark' +] class MarkerError(Exception): diff --git a/_pytest/python.py b/_pytest/python.py index 9f633812f..cdcfed49b 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -28,7 +28,7 @@ from _pytest.compat import ( safe_str, getlocation, enum, ) from _pytest.outcomes import fail -from _pytest.mark.legacy import transfer_markers +from _pytest.mark.structures import transfer_markers # relative paths that we use to filter traceback entries from appearing to the user; From 60358b6db8ef4ff082090103d095d9886e199c9f Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 22 Feb 2018 18:49:20 -0300 Subject: [PATCH 10/11] Fix linting --- _pytest/mark/evaluate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/_pytest/mark/evaluate.py b/_pytest/mark/evaluate.py index 27eaa78b3..f84d7455d 100644 --- a/_pytest/mark/evaluate.py +++ b/_pytest/mark/evaluate.py @@ -123,4 +123,3 @@ class MarkEvaluator(object): else: return "condition: " + str(self.expr) return expl - From 9959164c9ac919d3be56776509b569accc50289a Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 22 Feb 2018 18:55:25 -0300 Subject: [PATCH 11/11] Add CHANGELOG entry for #3250 --- changelog/3250.trivial.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/3250.trivial.rst diff --git a/changelog/3250.trivial.rst b/changelog/3250.trivial.rst new file mode 100644 index 000000000..a80bac513 --- /dev/null +++ b/changelog/3250.trivial.rst @@ -0,0 +1 @@ +Internal ``mark.py`` module has been turned into a package.