Skip to content

Commit c884b55

Browse files
Make the order of options matter.
1 parent 4f41c75 commit c884b55

6 files changed

Lines changed: 42 additions & 78 deletions

File tree

Lib/test/libregrtest/cmdline.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ def __init__(self, **kwargs) -> None:
162162
self.header = False
163163
self.failfast = False
164164
self.match_tests = []
165-
self.accept_labels = None
166-
self.ignore_labels = None
165+
self.match_labels = []
167166
self.pgo = False
168167
self.pgo_extended = False
169168
self.worker_json = None
@@ -273,10 +272,10 @@ def _create_parser():
273272
dest='match_tests', action=FilterAction, const=False,
274273
help='ignore test cases and methods with glob pattern PAT')
275274
group.add_argument('--label', metavar='NAME',
276-
dest='accept_labels', action='append',
275+
dest='match_labels', action=FilterAction, const=True,
277276
help='match test cases and methods with label NAME')
278277
group.add_argument('--no-label', metavar='NAME',
279-
dest='ignore_labels', action='append',
278+
dest='match_labels', action=FilterAction, const=False,
280279
help='ignore test cases and methods with label NAME')
281280
group.add_argument('--matchfile', metavar='FILENAME',
282281
dest='match_tests',

Lib/test/libregrtest/filter.py

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,44 @@
77
# By default, don't filter tests
88
_test_matchers = ()
99
_test_patterns = ()
10-
_match_test_func2 = None
10+
_match_labels = ()
1111

1212

13-
def match_test1(test):
13+
def match_test(test):
1414
# Function used by support.run_unittest() and regrtest --list-cases
15+
return match_test_id(test) and match_test_label(test)
16+
17+
def match_test_id(test):
1518
result = False
1619
for matcher, result in reversed(_test_matchers):
1720
if matcher(test.id()):
1821
return result
1922
return not result
2023

21-
def match_test(test):
22-
# Function used by support.run_unittest() and regrtest --list-cases
23-
return (match_test1(test) and
24-
(_match_test_func2 is None or _match_test_func2(test)))
24+
def match_test_label(test):
25+
result = False
26+
for label, result in reversed(_match_labels):
27+
if _has_label(test, label):
28+
return result
29+
return not result
30+
31+
def _has_label(test, label):
32+
attrname = f'_label_{label}'
33+
if hasattr(test, attrname):
34+
return True
35+
testMethod = getattr(test, test._testMethodName)
36+
while testMethod is not None:
37+
if hasattr(testMethod, attrname):
38+
return True
39+
testMethod = getattr(testMethod, '__wrapped__', None)
40+
try:
41+
module = sys.modules[test.__class__.__module__]
42+
if hasattr(module, attrname):
43+
return True
44+
except KeyError:
45+
pass
46+
return False
47+
2548

2649
def _is_full_match_test(pattern):
2750
# If a pattern contains at least one dot, it's considered
@@ -33,7 +56,7 @@ def _is_full_match_test(pattern):
3356
return ('.' in pattern) and (not re.search(r'[?*\[\]]', pattern))
3457

3558

36-
def set_match_tests(patterns):
59+
def set_match_tests(patterns=None, match_labels=None):
3760
global _test_matchers, _test_patterns
3861

3962
if not patterns:
@@ -50,51 +73,6 @@ def set_match_tests(patterns):
5073
_test_patterns = patterns
5174

5275

53-
def _check_obj_labels(obj, labels):
54-
for label in labels:
55-
if hasattr(obj, f'_label_{label}'):
56-
return True
57-
return False
58-
59-
def _check_test_labels(test, labels):
60-
if _check_obj_labels(test, labels):
61-
return True
62-
testMethod = getattr(test, test._testMethodName)
63-
while testMethod is not None:
64-
if _check_obj_labels(testMethod, labels):
65-
return True
66-
testMethod = getattr(testMethod, '__wrapped__', None)
67-
try:
68-
module = sys.modules[test.__class__.__module__]
69-
if _check_obj_labels(module, labels):
70-
return True
71-
except KeyError:
72-
pass
73-
return False
74-
75-
def set_match_tests2(accept_labels=None, ignore_labels=None):
76-
global _match_test_func2
77-
78-
if accept_labels is None:
79-
accept_labels = ()
80-
if ignore_labels is None:
81-
ignore_labels = ()
82-
# Create a copy since label lists can be mutable and so modified later
83-
accept_labels = tuple(accept_labels)
84-
ignore_labels = tuple(ignore_labels)
85-
86-
def match_function(test):
87-
accept = True
88-
ignore = False
89-
if accept_labels:
90-
accept = _check_test_labels(test, accept_labels)
91-
if ignore_labels:
92-
ignore = _check_test_labels(test, ignore_labels)
93-
return accept and not ignore
94-
95-
_match_test_func2 = match_function
96-
97-
9876
def _compile_match_function(patterns):
9977
patterns = list(patterns)
10078

Lib/test/libregrtest/findtests.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from test import support
66

7-
from .filter import match_test, set_match_tests, set_match_tests2
7+
from .filter import match_test, set_match_tests
88
from .utils import (
99
StrPath, TestName, TestTuple, TestList, TestFilter,
1010
abs_module_name, count, printlist)
@@ -85,12 +85,10 @@ def _list_cases(suite):
8585

8686
def list_cases(tests: TestTuple, *,
8787
match_tests: TestFilter | None = None,
88-
accept_labels: tuple[str, ...] | None = None,
89-
ignore_labels: tuple[str, ...] | None = None,
88+
match_labels: TestFilter | None = None,
9089
test_dir: StrPath | None = None):
9190
support.verbose = False
92-
set_match_tests(match_tests)
93-
set_match_tests2(accept_labels, ignore_labels)
91+
set_match_tests(match_tests, match_labels)
9492

9593
skipped = []
9694
for test_name in tests:

Lib/test/libregrtest/main.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,7 @@ def __init__(self, ns: Namespace, _add_python_opts: bool = False):
7979

8080
# Select tests
8181
self.match_tests: TestFilter = ns.match_tests
82-
if ns.accept_labels:
83-
self.accept_labels: tuple[str, ...] = tuple(ns.accept_labels)
84-
else:
85-
self.accept_labels = None
86-
if ns.ignore_labels:
87-
self.ignore_labels: tuple[str, ...] = tuple(ns.ignore_labels)
88-
else:
89-
self.ignore_labels = None
82+
self.match_labels: TestFilter = ns.match_labels
9083
self.exclude: bool = ns.exclude
9184
self.fromfile: StrPath | None = ns.fromfile
9285
self.starting_test: TestName | None = ns.start
@@ -408,8 +401,7 @@ def create_run_tests(self, tests: TestTuple):
408401
fail_fast=self.fail_fast,
409402
fail_env_changed=self.fail_env_changed,
410403
match_tests=self.match_tests,
411-
accept_labels=self.accept_labels,
412-
ignore_labels=self.ignore_labels,
404+
match_labels=self.match_labels,
413405
match_tests_dict=None,
414406
rerun=False,
415407
forever=self.forever,
@@ -662,8 +654,7 @@ def main(self, tests: TestList | None = None):
662654
elif self.want_list_cases:
663655
list_cases(selected,
664656
match_tests=self.match_tests,
665-
accept_labels=self.accept_labels,
666-
ignore_labels=self.ignore_labels,
657+
match_labels=self.match_labels,
667658
test_dir=self.test_dir)
668659
else:
669660
exitcode = self.run_tests(selected, tests)

Lib/test/libregrtest/runtests.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ class RunTests:
7373
fail_fast: bool
7474
fail_env_changed: bool
7575
match_tests: TestFilter
76-
accept_labels: tuple[str, ...] | None
77-
ignore_labels: tuple[str, ...] | None
76+
match_labels: TestFilter
7877
match_tests_dict: FilterDict | None
7978
rerun: bool
8079
forever: bool

Lib/test/libregrtest/setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from test import support
99
from test.support.os_helper import TESTFN_UNDECODABLE, FS_NONASCII
1010

11-
from .filter import set_match_tests, set_match_tests2
11+
from .filter import set_match_tests
1212
from .runtests import RunTests
1313
from .utils import (
1414
setup_unraisable_hook, setup_threading_excepthook, fix_umask,
@@ -93,8 +93,7 @@ def setup_tests(runtests: RunTests):
9393
support.PGO = runtests.pgo
9494
support.PGO_EXTENDED = runtests.pgo_extended
9595

96-
set_match_tests(runtests.match_tests)
97-
set_match_tests2(runtests.accept_labels, runtests.ignore_labels)
96+
set_match_tests(runtests.match_tests, runtests.match_labels)
9897

9998
if runtests.use_junit:
10099
support.junit_xml_list = []

0 commit comments

Comments
 (0)