Skip to content

Commit 9912b3d

Browse files
gh-77024: test.support: Improve documentation (GH-92513)
This is a rework of GH-5774 on current main. I was a bit more conservative in making changes than the original PR. See @csabella's comments on issue GH-77024 and the discussion on GH-5774 for explanations of several of the changes. Co-authored-by: Cheryl Sabella <[email protected]> Co-authored-by: Alex Waygood <[email protected]> (cherry picked from commit 8995177) Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 9369942 commit 9912b3d

File tree

1 file changed

+68
-42
lines changed

1 file changed

+68
-42
lines changed

Doc/library/test.rst

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,19 @@ The :mod:`test.support` module defines the following constants:
359359

360360
.. data:: MISSING_C_DOCSTRINGS
361361

362-
Return ``True`` if running on CPython, not on Windows, and configuration
363-
not set with ``WITH_DOC_STRINGS``.
362+
Set to ``True`` if Python is built without docstrings (the
363+
:c:macro:`WITH_DOC_STRINGS` macro is not defined).
364+
See the :option:`configure --without-doc-strings <--without-doc-strings>` option.
365+
366+
See also the :data:`HAVE_DOCSTRINGS` variable.
364367

365368

366369
.. data:: HAVE_DOCSTRINGS
367370

368-
Check for presence of docstrings.
371+
Set to ``True`` if function docstrings are available.
372+
See the :option:`python -OO <-O>` option, which strips docstrings of functions implemented in Python.
373+
374+
See also the :data:`MISSING_C_DOCSTRINGS` variable.
369375

370376

371377
.. data:: TEST_HTTP_URL
@@ -423,11 +429,6 @@ The :mod:`test.support` module defines the following functions:
423429
Used when tests are executed by :mod:`test.regrtest`.
424430

425431

426-
.. function:: system_must_validate_cert(f)
427-
428-
Raise :exc:`unittest.SkipTest` on TLS certification validation failures.
429-
430-
431432
.. function:: sortdict(dict)
432433

433434
Return a repr of *dict* with keys sorted.
@@ -445,12 +446,12 @@ The :mod:`test.support` module defines the following functions:
445446

446447
.. function:: match_test(test)
447448

448-
Match *test* to patterns set in :func:`set_match_tests`.
449+
Determine whether *test* matches the patterns set in :func:`set_match_tests`.
449450

450451

451-
.. function:: set_match_tests(patterns)
452+
.. function:: set_match_tests(accept_patterns=None, ignore_patterns=None)
452453

453-
Define match test with regular expression *patterns*.
454+
Define match patterns on test filenames and test method names for filtering tests.
454455

455456

456457
.. function:: run_unittest(*classes)
@@ -490,7 +491,9 @@ The :mod:`test.support` module defines the following functions:
490491
.. function:: check_impl_detail(**guards)
491492

492493
Use this check to guard CPython's implementation-specific tests or to
493-
run them only on the implementations guarded by the arguments::
494+
run them only on the implementations guarded by the arguments. This
495+
function returns ``True`` or ``False`` depending on the host platform.
496+
Example usage::
494497

495498
check_impl_detail() # Only on CPython (default).
496499
check_impl_detail(jython=True) # Only on Jython.
@@ -509,7 +512,7 @@ The :mod:`test.support` module defines the following functions:
509512
time the regrtest began.
510513

511514

512-
.. function:: get_original_stdout
515+
.. function:: get_original_stdout()
513516

514517
Return the original stdout set by :func:`record_original_stdout` or
515518
``sys.stdout`` if it's not set.
@@ -554,7 +557,7 @@ The :mod:`test.support` module defines the following functions:
554557

555558
.. function:: disable_faulthandler()
556559

557-
A context manager that replaces ``sys.stderr`` with ``sys.__stderr__``.
560+
A context manager that temporary disables :mod:`faulthandler`.
558561

559562

560563
.. function:: gc_collect()
@@ -567,8 +570,8 @@ The :mod:`test.support` module defines the following functions:
567570

568571
.. function:: disable_gc()
569572

570-
A context manager that disables the garbage collector upon entry and
571-
reenables it upon exit.
573+
A context manager that disables the garbage collector on entry. On
574+
exit, the garbage collector is restored to its prior state.
572575

573576

574577
.. function:: swap_attr(obj, attr, new_val)
@@ -633,14 +636,14 @@ The :mod:`test.support` module defines the following functions:
633636

634637
.. function:: calcobjsize(fmt)
635638

636-
Return :func:`struct.calcsize` for ``nP{fmt}0n`` or, if ``gettotalrefcount``
637-
exists, ``2PnP{fmt}0P``.
639+
Return the size of the :c:type:`PyObject` whose structure members are
640+
defined by *fmt*. The returned value includes the size of the Python object header and alignment.
638641

639642

640643
.. function:: calcvobjsize(fmt)
641644

642-
Return :func:`struct.calcsize` for ``nPn{fmt}0n`` or, if ``gettotalrefcount``
643-
exists, ``2PnPn{fmt}0P``.
645+
Return the size of the :c:type:`PyVarObject` whose structure members are
646+
defined by *fmt*. The returned value includes the size of the Python object header and alignment.
644647

645648

646649
.. function:: checksizeof(test, o, size)
@@ -656,6 +659,11 @@ The :mod:`test.support` module defines the following functions:
656659
have an associated comment identifying the relevant tracker issue.
657660

658661

662+
.. function:: system_must_validate_cert(f)
663+
664+
A decorator that skips the decorated test on TLS certification validation failures.
665+
666+
659667
.. decorator:: run_with_locale(catstr, *locales)
660668

661669
A decorator for running a function in a different locale, correctly
@@ -673,19 +681,19 @@ The :mod:`test.support` module defines the following functions:
673681
.. decorator:: requires_freebsd_version(*min_version)
674682

675683
Decorator for the minimum version when running test on FreeBSD. If the
676-
FreeBSD version is less than the minimum, raise :exc:`unittest.SkipTest`.
684+
FreeBSD version is less than the minimum, the test is skipped.
677685

678686

679687
.. decorator:: requires_linux_version(*min_version)
680688

681689
Decorator for the minimum version when running test on Linux. If the
682-
Linux version is less than the minimum, raise :exc:`unittest.SkipTest`.
690+
Linux version is less than the minimum, the test is skipped.
683691

684692

685693
.. decorator:: requires_mac_version(*min_version)
686694

687695
Decorator for the minimum version when running test on macOS. If the
688-
macOS version is less than the minimum, raise :exc:`unittest.SkipTest`.
696+
macOS version is less than the minimum, the test is skipped.
689697

690698

691699
.. decorator:: requires_IEEE_754
@@ -723,7 +731,7 @@ The :mod:`test.support` module defines the following functions:
723731
Decorator for only running the test if :data:`HAVE_DOCSTRINGS`.
724732

725733

726-
.. decorator:: cpython_only(test)
734+
.. decorator:: cpython_only
727735

728736
Decorator for tests only applicable to CPython.
729737

@@ -734,12 +742,12 @@ The :mod:`test.support` module defines the following functions:
734742
returns ``False``, then uses *msg* as the reason for skipping the test.
735743

736744

737-
.. decorator:: no_tracing(func)
745+
.. decorator:: no_tracing
738746

739747
Decorator to temporarily turn off tracing for the duration of the test.
740748

741749

742-
.. decorator:: refcount_test(test)
750+
.. decorator:: refcount_test
743751

744752
Decorator for tests which involve reference counting. The decorator does
745753
not run the test if it is not run by CPython. Any trace function is unset
@@ -762,10 +770,9 @@ The :mod:`test.support` module defines the following functions:
762770
means the test doesn't support dummy runs when ``-M`` is not specified.
763771

764772

765-
.. decorator:: bigaddrspacetest(f)
773+
.. decorator:: bigaddrspacetest
766774

767-
Decorator for tests that fill the address space. *f* is the function to
768-
wrap.
775+
Decorator for tests that fill the address space.
769776

770777

771778
.. function:: check_syntax_error(testcase, statement, errtext='', *, lineno=None, offset=None)
@@ -867,7 +874,7 @@ The :mod:`test.support` module defines the following functions:
867874

868875
.. function:: check_free_after_iterating(test, iter, cls, args=())
869876

870-
Assert that *iter* is deallocated after iterating.
877+
Assert instances of *cls* are deallocated after iterating.
871878

872879

873880
.. function:: missing_compiler_executable(cmd_names=[])
@@ -958,6 +965,16 @@ The :mod:`test.support` module defines the following classes:
958965
Class to save and restore signal handlers registered by the Python signal
959966
handler.
960967

968+
.. method:: save(self)
969+
970+
Save the signal handlers to a dictionary mapping signal numbers to the
971+
current signal handler.
972+
973+
.. method:: restore(self)
974+
975+
Set the signal numbers from the :meth:`save` dictionary to the saved
976+
handler.
977+
961978

962979
.. class:: Matcher()
963980

@@ -1101,11 +1118,11 @@ script execution tests.
11011118
variables *env_vars* succeeds (``rc == 0``) and return a ``(return code,
11021119
stdout, stderr)`` tuple.
11031120

1104-
If the ``__cleanenv`` keyword is set, *env_vars* is used as a fresh
1121+
If the *__cleanenv* keyword-only parameter is set, *env_vars* is used as a fresh
11051122
environment.
11061123

11071124
Python is started in isolated mode (command line option ``-I``),
1108-
except if the ``__isolated`` keyword is set to ``False``.
1125+
except if the *__isolated* keyword-only parameter is set to ``False``.
11091126

11101127
.. versionchanged:: 3.9
11111128
The function no longer strips whitespaces from *stderr*.
@@ -1216,15 +1233,17 @@ The :mod:`test.support.threading_helper` module provides support for threading t
12161233
is still alive after *timeout* seconds.
12171234

12181235

1219-
.. decorator:: reap_threads(func)
1236+
.. decorator:: reap_threads
12201237

12211238
Decorator to ensure the threads are cleaned up even if the test fails.
12221239

12231240

12241241
.. function:: start_threads(threads, unlock=None)
12251242

1226-
Context manager to start *threads*. It attempts to join the threads upon
1227-
exit.
1243+
Context manager to start *threads*, which is a sequence of threads.
1244+
*unlock* is a function called after the threads are started, even if an
1245+
exception was raised; an example would be :meth:`threading.Event.set`.
1246+
``start_threads`` will attempt to join the started threads upon exit.
12281247

12291248

12301249
.. function:: threading_cleanup(*original_values)
@@ -1306,7 +1325,10 @@ The :mod:`test.support.os_helper` module provides support for os tests.
13061325

13071326
.. data:: TESTFN_NONASCII
13081327

1309-
Set to a filename containing the :data:`FS_NONASCII` character.
1328+
Set to a filename containing the :data:`FS_NONASCII` character, if it exists.
1329+
This guarantees that if the filename exists, it can be encoded and decoded
1330+
with the default filesystem encoding. This allows tests that require a
1331+
non-ASCII filename to be easily skipped on platforms where they can't work.
13101332

13111333

13121334
.. data:: TESTFN_UNENCODABLE
@@ -1404,13 +1426,16 @@ The :mod:`test.support.os_helper` module provides support for os tests.
14041426
.. function:: rmdir(filename)
14051427

14061428
Call :func:`os.rmdir` on *filename*. On Windows platforms, this is
1407-
wrapped with a wait loop that checks for the existence of the file.
1429+
wrapped with a wait loop that checks for the existence of the file,
1430+
which is needed due to antivirus programs that can hold files open and prevent
1431+
deletion.
14081432

14091433

14101434
.. function:: rmtree(path)
14111435

14121436
Call :func:`shutil.rmtree` on *path* or call :func:`os.lstat` and
1413-
:func:`os.rmdir` to remove a path and its contents. On Windows platforms,
1437+
:func:`os.rmdir` to remove a path and its contents. As with :func:`rmdir`,
1438+
on Windows platforms
14141439
this is wrapped with a wait loop that checks for the existence of the files.
14151440

14161441

@@ -1457,7 +1482,8 @@ The :mod:`test.support.os_helper` module provides support for os tests.
14571482

14581483
.. function:: unlink(filename)
14591484

1460-
Call :func:`os.unlink` on *filename*. On Windows platforms, this is
1485+
Call :func:`os.unlink` on *filename*. As with :func:`rmdir`,
1486+
on Windows platforms, this is
14611487
wrapped with a wait loop that checks for the existence of the file.
14621488

14631489

@@ -1514,7 +1540,7 @@ The :mod:`test.support.import_helper` module provides support for import tests.
15141540
.. versionadded:: 3.1
15151541

15161542

1517-
.. function:: import_module(name, deprecated=False, *, required_on())
1543+
.. function:: import_module(name, deprecated=False, *, required_on=())
15181544

15191545
This function imports and returns the named module. Unlike a normal
15201546
import, this function raises :exc:`unittest.SkipTest` if the module
@@ -1556,15 +1582,15 @@ The :mod:`test.support.import_helper` module provides support for import tests.
15561582

15571583
A context manager to force import to return a new module reference. This
15581584
is useful for testing module-level behaviors, such as the emission of a
1559-
DeprecationWarning on import. Example usage::
1585+
:exc:`DeprecationWarning` on import. Example usage::
15601586

15611587
with CleanImport('foo'):
15621588
importlib.import_module('foo') # New reference.
15631589

15641590

15651591
.. class:: DirsOnSysPath(*paths)
15661592

1567-
A context manager to temporarily add directories to sys.path.
1593+
A context manager to temporarily add directories to :data:`sys.path`.
15681594

15691595
This makes a copy of :data:`sys.path`, appends any directories given
15701596
as positional arguments, then reverts :data:`sys.path` to the copied

0 commit comments

Comments
 (0)