Skip to content

Commit 8995177

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

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
@@ -368,13 +368,19 @@ The :mod:`test.support` module defines the following constants:
368368

369369
.. data:: MISSING_C_DOCSTRINGS
370370

371-
Return ``True`` if running on CPython, not on Windows, and configuration
372-
not set with ``WITH_DOC_STRINGS``.
371+
Set to ``True`` if Python is built without docstrings (the
372+
:c:macro:`WITH_DOC_STRINGS` macro is not defined).
373+
See the :option:`configure --without-doc-strings <--without-doc-strings>` option.
374+
375+
See also the :data:`HAVE_DOCSTRINGS` variable.
373376

374377

375378
.. data:: HAVE_DOCSTRINGS
376379

377-
Check for presence of docstrings.
380+
Set to ``True`` if function docstrings are available.
381+
See the :option:`python -OO <-O>` option, which strips docstrings of functions implemented in Python.
382+
383+
See also the :data:`MISSING_C_DOCSTRINGS` variable.
378384

379385

380386
.. data:: TEST_HTTP_URL
@@ -432,11 +438,6 @@ The :mod:`test.support` module defines the following functions:
432438
Used when tests are executed by :mod:`test.regrtest`.
433439

434440

435-
.. function:: system_must_validate_cert(f)
436-
437-
Raise :exc:`unittest.SkipTest` on TLS certification validation failures.
438-
439-
440441
.. function:: sortdict(dict)
441442

442443
Return a repr of *dict* with keys sorted.
@@ -454,12 +455,12 @@ The :mod:`test.support` module defines the following functions:
454455

455456
.. function:: match_test(test)
456457

457-
Match *test* to patterns set in :func:`set_match_tests`.
458+
Determine whether *test* matches the patterns set in :func:`set_match_tests`.
458459

459460

460-
.. function:: set_match_tests(patterns)
461+
.. function:: set_match_tests(accept_patterns=None, ignore_patterns=None)
461462

462-
Define match test with regular expression *patterns*.
463+
Define match patterns on test filenames and test method names for filtering tests.
463464

464465

465466
.. function:: run_unittest(*classes)
@@ -499,7 +500,9 @@ The :mod:`test.support` module defines the following functions:
499500
.. function:: check_impl_detail(**guards)
500501

501502
Use this check to guard CPython's implementation-specific tests or to
502-
run them only on the implementations guarded by the arguments::
503+
run them only on the implementations guarded by the arguments. This
504+
function returns ``True`` or ``False`` depending on the host platform.
505+
Example usage::
503506

504507
check_impl_detail() # Only on CPython (default).
505508
check_impl_detail(jython=True) # Only on Jython.
@@ -518,7 +521,7 @@ The :mod:`test.support` module defines the following functions:
518521
time the regrtest began.
519522

520523

521-
.. function:: get_original_stdout
524+
.. function:: get_original_stdout()
522525

523526
Return the original stdout set by :func:`record_original_stdout` or
524527
``sys.stdout`` if it's not set.
@@ -563,7 +566,7 @@ The :mod:`test.support` module defines the following functions:
563566

564567
.. function:: disable_faulthandler()
565568

566-
A context manager that replaces ``sys.stderr`` with ``sys.__stderr__``.
569+
A context manager that temporary disables :mod:`faulthandler`.
567570

568571

569572
.. function:: gc_collect()
@@ -576,8 +579,8 @@ The :mod:`test.support` module defines the following functions:
576579

577580
.. function:: disable_gc()
578581

579-
A context manager that disables the garbage collector upon entry and
580-
reenables it upon exit.
582+
A context manager that disables the garbage collector on entry. On
583+
exit, the garbage collector is restored to its prior state.
581584

582585

583586
.. function:: swap_attr(obj, attr, new_val)
@@ -651,14 +654,14 @@ The :mod:`test.support` module defines the following functions:
651654

652655
.. function:: calcobjsize(fmt)
653656

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

657660

658661
.. function:: calcvobjsize(fmt)
659662

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

663666

664667
.. function:: checksizeof(test, o, size)
@@ -674,6 +677,11 @@ The :mod:`test.support` module defines the following functions:
674677
have an associated comment identifying the relevant tracker issue.
675678

676679

680+
.. function:: system_must_validate_cert(f)
681+
682+
A decorator that skips the decorated test on TLS certification validation failures.
683+
684+
677685
.. decorator:: run_with_locale(catstr, *locales)
678686

679687
A decorator for running a function in a different locale, correctly
@@ -691,19 +699,19 @@ The :mod:`test.support` module defines the following functions:
691699
.. decorator:: requires_freebsd_version(*min_version)
692700

693701
Decorator for the minimum version when running test on FreeBSD. If the
694-
FreeBSD version is less than the minimum, raise :exc:`unittest.SkipTest`.
702+
FreeBSD version is less than the minimum, the test is skipped.
695703

696704

697705
.. decorator:: requires_linux_version(*min_version)
698706

699707
Decorator for the minimum version when running test on Linux. If the
700-
Linux version is less than the minimum, raise :exc:`unittest.SkipTest`.
708+
Linux version is less than the minimum, the test is skipped.
701709

702710

703711
.. decorator:: requires_mac_version(*min_version)
704712

705713
Decorator for the minimum version when running test on macOS. If the
706-
macOS version is less than the minimum, raise :exc:`unittest.SkipTest`.
714+
macOS version is less than the minimum, the test is skipped.
707715

708716

709717
.. decorator:: requires_IEEE_754
@@ -741,7 +749,7 @@ The :mod:`test.support` module defines the following functions:
741749
Decorator for only running the test if :data:`HAVE_DOCSTRINGS`.
742750

743751

744-
.. decorator:: cpython_only(test)
752+
.. decorator:: cpython_only
745753

746754
Decorator for tests only applicable to CPython.
747755

@@ -752,12 +760,12 @@ The :mod:`test.support` module defines the following functions:
752760
returns ``False``, then uses *msg* as the reason for skipping the test.
753761

754762

755-
.. decorator:: no_tracing(func)
763+
.. decorator:: no_tracing
756764

757765
Decorator to temporarily turn off tracing for the duration of the test.
758766

759767

760-
.. decorator:: refcount_test(test)
768+
.. decorator:: refcount_test
761769

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

782790

783-
.. decorator:: bigaddrspacetest(f)
791+
.. decorator:: bigaddrspacetest
784792

785-
Decorator for tests that fill the address space. *f* is the function to
786-
wrap.
793+
Decorator for tests that fill the address space.
787794

788795

789796
.. function:: check_syntax_error(testcase, statement, errtext='', *, lineno=None, offset=None)
@@ -885,7 +892,7 @@ The :mod:`test.support` module defines the following functions:
885892

886893
.. function:: check_free_after_iterating(test, iter, cls, args=())
887894

888-
Assert that *iter* is deallocated after iterating.
895+
Assert instances of *cls* are deallocated after iterating.
889896

890897

891898
.. function:: missing_compiler_executable(cmd_names=[])
@@ -976,6 +983,16 @@ The :mod:`test.support` module defines the following classes:
976983
Class to save and restore signal handlers registered by the Python signal
977984
handler.
978985

986+
.. method:: save(self)
987+
988+
Save the signal handlers to a dictionary mapping signal numbers to the
989+
current signal handler.
990+
991+
.. method:: restore(self)
992+
993+
Set the signal numbers from the :meth:`save` dictionary to the saved
994+
handler.
995+
979996

980997
.. class:: Matcher()
981998

@@ -1112,11 +1129,11 @@ script execution tests.
11121129
variables *env_vars* succeeds (``rc == 0``) and return a ``(return code,
11131130
stdout, stderr)`` tuple.
11141131

1115-
If the ``__cleanenv`` keyword is set, *env_vars* is used as a fresh
1132+
If the *__cleanenv* keyword-only parameter is set, *env_vars* is used as a fresh
11161133
environment.
11171134

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

11211138
.. versionchanged:: 3.9
11221139
The function no longer strips whitespaces from *stderr*.
@@ -1227,15 +1244,17 @@ The :mod:`test.support.threading_helper` module provides support for threading t
12271244
is still alive after *timeout* seconds.
12281245

12291246

1230-
.. decorator:: reap_threads(func)
1247+
.. decorator:: reap_threads
12311248

12321249
Decorator to ensure the threads are cleaned up even if the test fails.
12331250

12341251

12351252
.. function:: start_threads(threads, unlock=None)
12361253

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

12401259

12411260
.. function:: threading_cleanup(*original_values)
@@ -1317,7 +1336,10 @@ The :mod:`test.support.os_helper` module provides support for os tests.
13171336

13181337
.. data:: TESTFN_NONASCII
13191338

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

13221344

13231345
.. data:: TESTFN_UNENCODABLE
@@ -1415,13 +1437,16 @@ The :mod:`test.support.os_helper` module provides support for os tests.
14151437
.. function:: rmdir(filename)
14161438

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

14201444

14211445
.. function:: rmtree(path)
14221446

14231447
Call :func:`shutil.rmtree` on *path* or call :func:`os.lstat` and
1424-
:func:`os.rmdir` to remove a path and its contents. On Windows platforms,
1448+
:func:`os.rmdir` to remove a path and its contents. As with :func:`rmdir`,
1449+
on Windows platforms
14251450
this is wrapped with a wait loop that checks for the existence of the files.
14261451

14271452

@@ -1468,7 +1493,8 @@ The :mod:`test.support.os_helper` module provides support for os tests.
14681493

14691494
.. function:: unlink(filename)
14701495

1471-
Call :func:`os.unlink` on *filename*. On Windows platforms, this is
1496+
Call :func:`os.unlink` on *filename*. As with :func:`rmdir`,
1497+
on Windows platforms, this is
14721498
wrapped with a wait loop that checks for the existence of the file.
14731499

14741500

@@ -1525,7 +1551,7 @@ The :mod:`test.support.import_helper` module provides support for import tests.
15251551
.. versionadded:: 3.1
15261552

15271553

1528-
.. function:: import_module(name, deprecated=False, *, required_on())
1554+
.. function:: import_module(name, deprecated=False, *, required_on=())
15291555

15301556
This function imports and returns the named module. Unlike a normal
15311557
import, this function raises :exc:`unittest.SkipTest` if the module
@@ -1567,15 +1593,15 @@ The :mod:`test.support.import_helper` module provides support for import tests.
15671593

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

15721598
with CleanImport('foo'):
15731599
importlib.import_module('foo') # New reference.
15741600

15751601

15761602
.. class:: DirsOnSysPath(*paths)
15771603

1578-
A context manager to temporarily add directories to sys.path.
1604+
A context manager to temporarily add directories to :data:`sys.path`.
15791605

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

0 commit comments

Comments
 (0)