Skip to content

Commit 4e242d1

Browse files
authored
Improve cross-references in runpy docs (#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
1 parent ee3bf45 commit 4e242d1

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`).
@@ -106,15 +106,16 @@ The :mod:`runpy` module provides two functions:
106106
Execute the code at the named filesystem location and return the resulting
107107
module globals dictionary. As with a script name supplied to the CPython
108108
command line, the supplied path may refer to a Python source file, a
109-
compiled bytecode file or a valid sys.path entry containing a ``__main__``
110-
module (e.g. a zipfile containing a top-level ``__main__.py`` file).
109+
compiled bytecode file or a valid :data:`sys.path` entry containing a
110+
:mod:`__main__` module
111+
(e.g. a zipfile containing a top-level ``__main__.py`` file).
111112

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

120121
The optional dictionary argument *init_globals* may be used to pre-populate
@@ -137,22 +138,22 @@ The :mod:`runpy` module provides two functions:
137138
supplied path, and ``__spec__``, ``__cached__``, ``__loader__`` and
138139
``__package__`` will all be set to :const:`None`.
139140

140-
If the supplied path is a reference to a valid sys.path entry, then
141-
``__spec__`` will be set appropriately for the imported ``__main__``
141+
If the supplied path is a reference to a valid :data:`sys.path` entry, then
142+
``__spec__`` will be set appropriately for the imported :mod:`__main__`
142143
module (that is, ``__spec__.name`` will always be ``__main__``).
143144
``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` will be
144145
:ref:`set as normal <import-mod-attrs>` based on the module spec.
145146

146147
A number of alterations are also made to the :mod:`sys` module. Firstly,
147-
``sys.path`` may be altered as described above. ``sys.argv[0]`` is updated
148+
:data:`sys.path` may be altered as described above. ``sys.argv[0]`` is updated
148149
with the value of ``path_name`` and ``sys.modules[__name__]`` is updated
149150
with a temporary module object for the module being executed. All
150151
modifications to items in :mod:`sys` are reverted before the function
151152
returns.
152153

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

@@ -165,7 +166,7 @@ The :mod:`runpy` module provides two functions:
165166
.. versionchanged:: 3.4
166167
Updated to take advantage of the module spec feature added by
167168
:pep:`451`. This allows ``__cached__`` to be set correctly in the
168-
case where ``__main__`` is imported from a valid sys.path entry rather
169+
case where ``__main__`` is imported from a valid :data:`sys.path` entry rather
169170
than being executed directly.
170171

171172
.. versionchanged:: 3.12

0 commit comments

Comments
 (0)