Skip to content

Commit 1878419

Browse files
[3.11] Improve cross-references in runpy docs (GH-107673) (#107699)
Improve cross-references in `runpy` docs (GH-107673) - Add links to `__main__` and `sys.path` where appropriate - Ensure each paragraph never has more than one link to the same thing, to avoid visual clutter from too many links (cherry picked from commit 4e242d1) Co-authored-by: Kamil Turek <[email protected]>
1 parent 2345a8f commit 1878419

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Doc/library/runpy.rst

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The :mod:`runpy` module provides two functions:
3939

4040
The *mod_name* argument should be an absolute module name.
4141
If the module name refers to a package rather than a normal
42-
module, then that package is imported and the ``__main__`` submodule within
42+
module, then that package is imported and the :mod:`__main__` submodule within
4343
that package is then executed and the resulting module globals dictionary
4444
returned.
4545

@@ -74,15 +74,15 @@ The :mod:`runpy` module provides two functions:
7474

7575
Note that this manipulation of :mod:`sys` is not thread-safe. Other threads
7676
may see the partially initialised module, as well as the altered list of
77-
arguments. It is recommended that the :mod:`sys` module be left alone when
77+
arguments. It is recommended that the ``sys`` module be left alone when
7878
invoking this function from threaded code.
7979

8080
.. seealso::
8181
The :option:`-m` option offering equivalent functionality from the
8282
command line.
8383

8484
.. versionchanged:: 3.1
85-
Added ability to execute packages by looking for a ``__main__`` submodule.
85+
Added ability to execute packages by looking for a :mod:`__main__` submodule.
8686

8787
.. versionchanged:: 3.2
8888
Added ``__cached__`` global variable (see :pep:`3147`).
@@ -101,15 +101,16 @@ The :mod:`runpy` module provides two functions:
101101
Execute the code at the named filesystem location and return the resulting
102102
module globals dictionary. As with a script name supplied to the CPython
103103
command line, the supplied path may refer to a Python source file, a
104-
compiled bytecode file or a valid sys.path entry containing a ``__main__``
105-
module (e.g. a zipfile containing a top-level ``__main__.py`` file).
104+
compiled bytecode file or a valid :data:`sys.path` entry containing a
105+
:mod:`__main__` module
106+
(e.g. a zipfile containing a top-level ``__main__.py`` file).
106107

107108
For a simple script, the specified code is simply executed in a fresh
108-
module namespace. For a valid sys.path entry (typically a zipfile or
109+
module namespace. For a valid :data:`sys.path` entry (typically a zipfile or
109110
directory), the entry is first added to the beginning of ``sys.path``. The
110111
function then looks for and executes a :mod:`__main__` module using the
111112
updated path. Note that there is no special protection against invoking
112-
an existing :mod:`__main__` entry located elsewhere on ``sys.path`` if
113+
an existing ``__main__`` entry located elsewhere on ``sys.path`` if
113114
there is no such module at the specified location.
114115

115116
The optional dictionary argument *init_globals* may be used to pre-populate
@@ -132,22 +133,22 @@ The :mod:`runpy` module provides two functions:
132133
supplied path, and ``__spec__``, ``__cached__``, ``__loader__`` and
133134
``__package__`` will all be set to :const:`None`.
134135

135-
If the supplied path is a reference to a valid sys.path entry, then
136-
``__spec__`` will be set appropriately for the imported ``__main__``
136+
If the supplied path is a reference to a valid :data:`sys.path` entry, then
137+
``__spec__`` will be set appropriately for the imported :mod:`__main__`
137138
module (that is, ``__spec__.name`` will always be ``__main__``).
138139
``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` will be
139140
:ref:`set as normal <import-mod-attrs>` based on the module spec.
140141

141142
A number of alterations are also made to the :mod:`sys` module. Firstly,
142-
``sys.path`` may be altered as described above. ``sys.argv[0]`` is updated
143+
:data:`sys.path` may be altered as described above. ``sys.argv[0]`` is updated
143144
with the value of ``path_name`` and ``sys.modules[__name__]`` is updated
144145
with a temporary module object for the module being executed. All
145146
modifications to items in :mod:`sys` are reverted before the function
146147
returns.
147148

148149
Note that, unlike :func:`run_module`, the alterations made to :mod:`sys`
149150
are not optional in this function as these adjustments are essential to
150-
allowing the execution of sys.path entries. As the thread-safety
151+
allowing the execution of :data:`sys.path` entries. As the thread-safety
151152
limitations still apply, use of this function in threaded code should be
152153
either serialised with the import lock or delegated to a separate process.
153154

@@ -160,7 +161,7 @@ The :mod:`runpy` module provides two functions:
160161
.. versionchanged:: 3.4
161162
Updated to take advantage of the module spec feature added by
162163
:pep:`451`. This allows ``__cached__`` to be set correctly in the
163-
case where ``__main__`` is imported from a valid sys.path entry rather
164+
case where ``__main__`` is imported from a valid :data:`sys.path` entry rather
164165
than being executed directly.
165166

166167
.. seealso::

0 commit comments

Comments
 (0)