Skip to content

[3.12] Improve cross-references in runpy docs (GH-107673) #107698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Doc/library/runpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The :mod:`runpy` module provides two functions:

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

Expand Down Expand Up @@ -74,15 +74,15 @@ The :mod:`runpy` module provides two functions:

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

.. seealso::
The :option:`-m` option offering equivalent functionality from the
command line.

.. versionchanged:: 3.1
Added ability to execute packages by looking for a ``__main__`` submodule.
Added ability to execute packages by looking for a :mod:`__main__` submodule.

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

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

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

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

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

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

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

.. versionchanged:: 3.12
Expand Down