Skip to content

Commit 072e56b

Browse files
Use mark_module() to mark a module
mark() had two different meanings depending on whether the globals argument was passed. It is now only a decorator of a test method or class, and mark_module() marks a whole module, by default the caller's. The label and its value are positional-only: a keyword would be mistaken for the name of the label. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent feb3f8e commit 072e56b

4 files changed

Lines changed: 30 additions & 19 deletions

File tree

Doc/library/test.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,10 @@ The :mod:`!test.support` module defines the following functions:
531531
rather than looking directly in the path directories.
532532

533533

534-
.. function:: mark(label, value=True, *, globals=None)
534+
.. function:: mark(label, value=True, /)
535535

536-
Add a label to tests.
537-
The ``@mark('label')`` decorator adds a label to method or class.
538-
``test.support.mark('label', globals=globals())`` adds a label to the whole
539-
module.
536+
Add a label to a test. Use ``@mark('label')`` as a decorator of a test
537+
method or class.
540538

541539
The optional *value* (``True`` by default) is matched on the command line
542540
by ``--label label=value``, whereas ``--label label`` matches any value.
@@ -546,6 +544,16 @@ The :mod:`!test.support` module defines the following functions:
546544
automatically.
547545

548546

547+
.. function:: mark_module(label, value=True, /, *, globals=None)
548+
549+
Add a label to every test of a module. Call it at the module level::
550+
551+
test.support.mark_module('pickletest')
552+
553+
The module is the caller, unless its :func:`globals` dict is passed as the
554+
*globals* argument.
555+
556+
549557
.. function:: get_pagesize()
550558

551559
Get size of a page in bytes.

Lib/test/support/__init__.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def requires(resource, msg=None):
340340
"""Raise ResourceDenied if the specified resource is not available."""
341341
f = sys._getframe(1)
342342
if f.f_globals is f.f_locals:
343-
mark(f'requires_{resource}', globals=f.f_globals)
343+
mark_module(f'requires_{resource}', globals=f.f_globals)
344344
if not is_resource_enabled(resource):
345345
if msg is None:
346346
msg = "Use of the %r resource not enabled" % resource
@@ -682,7 +682,7 @@ def requires_working_socket(*, module=False, globals=None):
682682
if module or globals is not None:
683683
if globals is None:
684684
globals = sys._getframe(1).f_globals
685-
mark(label, globals=globals)
685+
mark_module(label, globals=globals)
686686
if not has_socket_support:
687687
raise unittest.SkipTest(msg)
688688
else:
@@ -1374,24 +1374,27 @@ def wrapper(self):
13741374
#=======================================================================
13751375
# unittest integration.
13761376

1377-
def mark(label, value=True, *, globals=None):
1378-
"""Add a label to test.
1379-
1380-
To add a label to method or class, use it as a decorator.
1381-
1382-
To add a label to module, pass the globals() dict as the globals argument.
1377+
def mark(label, value=True, /):
1378+
"""Add a label to a test method or class. Use it as a decorator.
13831379
13841380
The optional value (``True`` by default) can be matched on the command
13851381
line with ``--label name=value``.
13861382
"""
1387-
if globals is not None:
1388-
globals[f'_label_{label}'] = value
1389-
return
13901383
def decorator(test):
13911384
setattr(test, f'_label_{label}', value)
13921385
return test
13931386
return decorator
13941387

1388+
def mark_module(label, value=True, /, *, globals=None):
1389+
"""Add a label to every test of a module.
1390+
1391+
The module is the caller, unless its globals() dict is passed as the
1392+
globals argument.
1393+
"""
1394+
if globals is None:
1395+
globals = sys._getframe(1).f_globals
1396+
globals[f'_label_{label}'] = value
1397+
13951398
def combine(*decorators):
13961399
def decorator(test):
13971400
for deco in reversed(decorators):

Lib/test/support/import_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ def import_module(name, deprecated=False, *, required_on=()):
9090
with _ignore_deprecated_imports(deprecated):
9191
f = sys._getframe(1)
9292
if f.f_globals is f.f_locals:
93-
from test.support import mark
94-
mark(f'requires_{name}', globals=f.f_globals)
93+
from test.support import mark_module
94+
mark_module(f'requires_{name}', globals=f.f_globals)
9595
try:
9696
return importlib.import_module(name)
9797
except ImportError as msg:

Lib/test/support/threading_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def requires_working_threading(*, module=False, globals=None):
247247
if module or globals is not None:
248248
if globals is None:
249249
globals = sys._getframe(1).f_globals
250-
support.mark(label, globals=globals)
250+
support.mark_module(label, globals=globals)
251251
if not can_start_thread:
252252
raise unittest.SkipTest(msg)
253253
else:

0 commit comments

Comments
 (0)