Skip to content

Commit 34bd36c

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 9303a5a commit 34bd36c

File tree

1 file changed

+68
-42
lines changed

1 file changed

+68
-42
lines changed

Doc/library/test.rst

+68-42
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)
@@ -642,14 +645,14 @@ The :mod:`test.support` module defines the following functions:
642645

643646
.. function:: calcobjsize(fmt)
644647

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

648651

649652
.. function:: calcvobjsize(fmt)
650653

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

654657

655658
.. function:: checksizeof(test, o, size)
@@ -665,6 +668,11 @@ The :mod:`test.support` module defines the following functions:
665668
have an associated comment identifying the relevant tracker issue.
666669

667670

671+
.. function:: system_must_validate_cert(f)
672+
673+
A decorator that skips the decorated test on TLS certification validation failures.
674+
675+
668676
.. decorator:: run_with_locale(catstr, *locales)
669677

670678
A decorator for running a function in a different locale, correctly
@@ -682,19 +690,19 @@ The :mod:`test.support` module defines the following functions:
682690
.. decorator:: requires_freebsd_version(*min_version)
683691

684692
Decorator for the minimum version when running test on FreeBSD. If the
685-
FreeBSD version is less than the minimum, raise :exc:`unittest.SkipTest`.
693+
FreeBSD version is less than the minimum, the test is skipped.
686694

687695

688696
.. decorator:: requires_linux_version(*min_version)
689697

690698
Decorator for the minimum version when running test on Linux. If the
691-
Linux version is less than the minimum, raise :exc:`unittest.SkipTest`.
699+
Linux version is less than the minimum, the test is skipped.
692700

693701

694702
.. decorator:: requires_mac_version(*min_version)
695703

696704
Decorator for the minimum version when running test on macOS. If the
697-
macOS version is less than the minimum, raise :exc:`unittest.SkipTest`.
705+
macOS version is less than the minimum, the test is skipped.
698706

699707

700708
.. decorator:: requires_IEEE_754
@@ -732,7 +740,7 @@ The :mod:`test.support` module defines the following functions:
732740
Decorator for only running the test if :data:`HAVE_DOCSTRINGS`.
733741

734742

735-
.. decorator:: cpython_only(test)
743+
.. decorator:: cpython_only
736744

737745
Decorator for tests only applicable to CPython.
738746

@@ -743,12 +751,12 @@ The :mod:`test.support` module defines the following functions:
743751
returns ``False``, then uses *msg* as the reason for skipping the test.
744752

745753

746-
.. decorator:: no_tracing(func)
754+
.. decorator:: no_tracing
747755

748756
Decorator to temporarily turn off tracing for the duration of the test.
749757

750758

751-
.. decorator:: refcount_test(test)
759+
.. decorator:: refcount_test
752760

753761
Decorator for tests which involve reference counting. The decorator does
754762
not run the test if it is not run by CPython. Any trace function is unset
@@ -771,10 +779,9 @@ The :mod:`test.support` module defines the following functions:
771779
means the test doesn't support dummy runs when ``-M`` is not specified.
772780

773781

774-
.. decorator:: bigaddrspacetest(f)
782+
.. decorator:: bigaddrspacetest
775783

776-
Decorator for tests that fill the address space. *f* is the function to
777-
wrap.
784+
Decorator for tests that fill the address space.
778785

779786

780787
.. function:: check_syntax_error(testcase, statement, errtext='', *, lineno=None, offset=None)
@@ -876,7 +883,7 @@ The :mod:`test.support` module defines the following functions:
876883

877884
.. function:: check_free_after_iterating(test, iter, cls, args=())
878885

879-
Assert that *iter* is deallocated after iterating.
886+
Assert instances of *cls* are deallocated after iterating.
880887

881888

882889
.. function:: missing_compiler_executable(cmd_names=[])
@@ -967,6 +974,16 @@ The :mod:`test.support` module defines the following classes:
967974
Class to save and restore signal handlers registered by the Python signal
968975
handler.
969976

977+
.. method:: save(self)
978+
979+
Save the signal handlers to a dictionary mapping signal numbers to the
980+
current signal handler.
981+
982+
.. method:: restore(self)
983+
984+
Set the signal numbers from the :meth:`save` dictionary to the saved
985+
handler.
986+
970987

971988
.. class:: Matcher()
972989

@@ -1110,11 +1127,11 @@ script execution tests.
11101127
variables *env_vars* succeeds (``rc == 0``) and return a ``(return code,
11111128
stdout, stderr)`` tuple.
11121129

1113-
If the ``__cleanenv`` keyword is set, *env_vars* is used as a fresh
1130+
If the *__cleanenv* keyword-only parameter is set, *env_vars* is used as a fresh
11141131
environment.
11151132

11161133
Python is started in isolated mode (command line option ``-I``),
1117-
except if the ``__isolated`` keyword is set to ``False``.
1134+
except if the *__isolated* keyword-only parameter is set to ``False``.
11181135

11191136
.. versionchanged:: 3.9
11201137
The function no longer strips whitespaces from *stderr*.
@@ -1225,15 +1242,17 @@ The :mod:`test.support.threading_helper` module provides support for threading t
12251242
is still alive after *timeout* seconds.
12261243

12271244

1228-
.. decorator:: reap_threads(func)
1245+
.. decorator:: reap_threads
12291246

12301247
Decorator to ensure the threads are cleaned up even if the test fails.
12311248

12321249

12331250
.. function:: start_threads(threads, unlock=None)
12341251

1235-
Context manager to start *threads*. It attempts to join the threads upon
1236-
exit.
1252+
Context manager to start *threads*, which is a sequence of threads.
1253+
*unlock* is a function called after the threads are started, even if an
1254+
exception was raised; an example would be :meth:`threading.Event.set`.
1255+
``start_threads`` will attempt to join the started threads upon exit.
12371256

12381257

12391258
.. function:: threading_cleanup(*original_values)
@@ -1315,7 +1334,10 @@ The :mod:`test.support.os_helper` module provides support for os tests.
13151334

13161335
.. data:: TESTFN_NONASCII
13171336

1318-
Set to a filename containing the :data:`FS_NONASCII` character.
1337+
Set to a filename containing the :data:`FS_NONASCII` character, if it exists.
1338+
This guarantees that if the filename exists, it can be encoded and decoded
1339+
with the default filesystem encoding. This allows tests that require a
1340+
non-ASCII filename to be easily skipped on platforms where they can't work.
13191341

13201342

13211343
.. data:: TESTFN_UNENCODABLE
@@ -1413,13 +1435,16 @@ The :mod:`test.support.os_helper` module provides support for os tests.
14131435
.. function:: rmdir(filename)
14141436

14151437
Call :func:`os.rmdir` on *filename*. On Windows platforms, this is
1416-
wrapped with a wait loop that checks for the existence of the file.
1438+
wrapped with a wait loop that checks for the existence of the file,
1439+
which is needed due to antivirus programs that can hold files open and prevent
1440+
deletion.
14171441

14181442

14191443
.. function:: rmtree(path)
14201444

14211445
Call :func:`shutil.rmtree` on *path* or call :func:`os.lstat` and
1422-
:func:`os.rmdir` to remove a path and its contents. On Windows platforms,
1446+
:func:`os.rmdir` to remove a path and its contents. As with :func:`rmdir`,
1447+
on Windows platforms
14231448
this is wrapped with a wait loop that checks for the existence of the files.
14241449

14251450

@@ -1466,7 +1491,8 @@ The :mod:`test.support.os_helper` module provides support for os tests.
14661491

14671492
.. function:: unlink(filename)
14681493

1469-
Call :func:`os.unlink` on *filename*. On Windows platforms, this is
1494+
Call :func:`os.unlink` on *filename*. As with :func:`rmdir`,
1495+
on Windows platforms, this is
14701496
wrapped with a wait loop that checks for the existence of the file.
14711497

14721498

@@ -1523,7 +1549,7 @@ The :mod:`test.support.import_helper` module provides support for import tests.
15231549
.. versionadded:: 3.1
15241550

15251551

1526-
.. function:: import_module(name, deprecated=False, *, required_on())
1552+
.. function:: import_module(name, deprecated=False, *, required_on=())
15271553

15281554
This function imports and returns the named module. Unlike a normal
15291555
import, this function raises :exc:`unittest.SkipTest` if the module
@@ -1565,15 +1591,15 @@ The :mod:`test.support.import_helper` module provides support for import tests.
15651591

15661592
A context manager to force import to return a new module reference. This
15671593
is useful for testing module-level behaviors, such as the emission of a
1568-
DeprecationWarning on import. Example usage::
1594+
:exc:`DeprecationWarning` on import. Example usage::
15691595

15701596
with CleanImport('foo'):
15711597
importlib.import_module('foo') # New reference.
15721598

15731599

15741600
.. class:: DirsOnSysPath(*paths)
15751601

1576-
A context manager to temporarily add directories to sys.path.
1602+
A context manager to temporarily add directories to :data:`sys.path`.
15771603

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

0 commit comments

Comments
 (0)