2020-12-30 16:28:56 -05:00
|
|
|
PEP: 0648
|
|
|
|
Title: Extensible customizations of the interpreter at startup
|
|
|
|
Author: Mario Corchero <mariocj89@gmail.com>
|
|
|
|
Sponsor: Pablo Galindo
|
|
|
|
BDFL-Delegate: XXXX
|
|
|
|
Discussions-To: https://discuss.python.org/t/pep-648-extensible-customizations-of-the-interpreter-at-startup/6403
|
|
|
|
Status: Draft
|
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
2020-12-30 16:40:47 -05:00
|
|
|
Created: 30-Dec-2020
|
2021-01-04 06:49:52 -05:00
|
|
|
Python-Version: 3.10
|
2020-12-30 16:28:56 -05:00
|
|
|
Post-History: python-ideas: 16th Dec. python-dev: 18th Dec.
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
|
|
|
This pep proposes supporting extensible customization of the interpreter, by
|
|
|
|
allowing users to install scripts that will be executed at startup.
|
|
|
|
|
|
|
|
Motivation
|
|
|
|
==========
|
|
|
|
|
|
|
|
System administrators, tools that repackage the interpreter and some
|
|
|
|
libraries need to customize aspects of the interpreter at startup time.
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
This is usually achieved via ``sitecustomize.py`` for system administrators
|
|
|
|
whilst libraries rely on exploiting ``pth`` files. This PEP proposes a way of
|
2020-12-30 16:28:56 -05:00
|
|
|
achieving the same in a more user-friendly and structured way.
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
Limitations of ``pth`` files
|
|
|
|
----------------------------
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
If a library needs to perform any customization before an import or that
|
|
|
|
relates to the general working of the interpreter, they often rely on the
|
2021-01-04 06:49:52 -05:00
|
|
|
fact that ``pth`` files, which are loaded at startup, can include Python code
|
|
|
|
that will be executed when the ``pth`` file is evaluated.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
Note that ``pth`` files were originally developed to just add additional
|
|
|
|
directories to ``sys.path``, but they may also contain lines which start
|
|
|
|
with "import", which will be \``exec\``ed. Users have exploited this feature to
|
2020-12-30 16:28:56 -05:00
|
|
|
allow the customizations that they needed. See setuptools [#setuptools]_ or
|
|
|
|
betterexceptions [#betterexceptions]_ as examples.
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
Using ``pth`` files for this purpose is far from ideal for library developers,
|
2020-12-30 16:28:56 -05:00
|
|
|
as they need to inject code into a single line preceded by an import, making
|
|
|
|
it rather unreadable. Library developers following that practice will usually
|
|
|
|
create a module that performs all actions on import, as done by
|
|
|
|
betterexceptions [#betterexceptions]_, but the approach is still not really
|
|
|
|
user friendly.
|
|
|
|
|
|
|
|
Additionally, it is also non-ideal for users of the interpreter as if they
|
|
|
|
want to inspect what is being executed at Python startup they need to review
|
2021-01-04 06:49:52 -05:00
|
|
|
all the ``pth`` files for potential code execution which can be spread across
|
2020-12-30 16:28:56 -05:00
|
|
|
all site paths. Most of those pth will be "legit" pth files that just modify
|
|
|
|
the path, answering the question of "what is changing my interpreter at
|
|
|
|
startup" a rather complex one.
|
|
|
|
|
|
|
|
Lastly, there have been multiple suggestions for removing code execution from
|
2021-01-04 06:49:52 -05:00
|
|
|
``pth`` files, see [#bpo-24534]_ and [#bpo-33944]_.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
Limitations of ``sitecustomize.py``
|
|
|
|
-----------------------------------
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Whilst sitecustomize is an acceptable solution, it assumes a single person is
|
|
|
|
in charge of the system and the interpreter. If both the system administrator
|
|
|
|
and the responsibility of provisioning the interpreter want to add
|
|
|
|
customizations at the interpreter startup they need to agree on the contents
|
|
|
|
of the file and combine all the changes. This is not a major limitation
|
|
|
|
though, and it is not the main driver of this change, but should the change
|
|
|
|
happen, it will also improve the situation for these users, as rather than
|
2021-01-04 06:49:52 -05:00
|
|
|
having a ``sitecustomize.py`` which performs all those actions, they can have
|
2020-12-30 16:28:56 -05:00
|
|
|
custom isolated files named after the features they want to enhance. As an
|
2021-01-04 06:49:52 -05:00
|
|
|
example, Ubuntu could change their current ``sitecustomize.py`` to just be
|
|
|
|
``ubuntu_apport_python_hook``. This not only better represents its intent but
|
2020-12-30 16:28:56 -05:00
|
|
|
also gives users of the interpreter a better understanding of the
|
|
|
|
modifications happening on their interpreter.
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
Benefits of ``__sitecustomize__``
|
|
|
|
---------------------------------
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Having a structured way of injecting custom startup scripts, will allow
|
|
|
|
supporting in a better way the cases presented above. It will result in both
|
|
|
|
maintainers and users better experience as detailed, and allow CPython to
|
2021-01-04 06:49:52 -05:00
|
|
|
deprecate and eventually remove code execution from ``pth`` files, as desired
|
2020-12-30 16:28:56 -05:00
|
|
|
in the previously mentioned bpos.
|
|
|
|
Additionally, these solutions provide a unique way to support all use-cases
|
2021-01-04 06:49:52 -05:00
|
|
|
that before have been fulfilled via the misuse of ``pth`` files,
|
|
|
|
``sitecustomize.py`` and ``usercustomize.py``. The use of a ``__sitecustomize__``
|
2020-12-30 16:28:56 -05:00
|
|
|
will allow for packages, tools and system admins to inject scripts that will
|
|
|
|
be loaded at startups through an easy to understand mechanism.
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
2021-01-05 12:58:14 -05:00
|
|
|
This PEP proposes supporting extensible customization of the interpreter at
|
|
|
|
startup by allowing users to install scripts into a folder named
|
|
|
|
``__sitecustomize__`` located in a site path. Those scripts will be executed
|
|
|
|
at startup time.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
The ``site`` module will expose an option on its main function that allows
|
2021-01-05 12:58:14 -05:00
|
|
|
listing all scripts that will be executed, which will allow users to quickly
|
|
|
|
see all customizations that affect an interpreter.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
We will also work with build backends on facilitating the installation of
|
|
|
|
these files.
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
Why ``__sitecustomize__``
|
|
|
|
-------------------------
|
2020-12-30 16:28:56 -05:00
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
The name aims to follow the already existing concept of ``sitecustomize.py``.
|
2021-01-05 12:58:14 -05:00
|
|
|
As the folder will be within ``sys.path``, given that it is located in site
|
|
|
|
paths, we choose to use double underscore around its name, to prevent
|
|
|
|
colliding with the already existing ``sitecustomize.py``.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Disabling start scripts
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
In some scenarios, like when the startup time is key, it might be desired to
|
|
|
|
disable this option altogether. Whilst we could have added a new flag to do
|
2021-01-05 12:58:14 -05:00
|
|
|
so, we think that the already existing flag ``-S`` [#s-flag]_ is already good
|
|
|
|
enough, as it disables all ``site`` related manipulation. If the flag is
|
|
|
|
passed in, ``__sitecustomize__`` will not be used.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Order of execution
|
|
|
|
------------------
|
|
|
|
|
2021-01-05 12:58:14 -05:00
|
|
|
The scripts in ``__sitecustomize__`` will be executed in alphabetic order
|
|
|
|
after the evaluation of ``pth`` files. We considered executing them in random
|
|
|
|
order, but that could result in different results depending on how the
|
|
|
|
interpreter chooses to pick up those files. So even if it won't be a good
|
|
|
|
practice to rely on other files being executed, we think that is better than
|
|
|
|
having randomly different results on interpreter startup.
|
|
|
|
We chose to run the scripts after the ``pth`` files in case an user needs to
|
|
|
|
add items to the path before running a script.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Impact on startup time
|
|
|
|
----------------------
|
|
|
|
|
2021-01-05 12:58:14 -05:00
|
|
|
If an interpreter is not using this mechanism, the impact on performance is
|
|
|
|
expected to be minimal as this PEP just adds a check for
|
|
|
|
``__sitecustomize__`` when ``site.py`` is walking the site paths looking for
|
|
|
|
``pth`` files. This impact will be reduced in the future as we will remove
|
|
|
|
two other imports: "sitecustomize.py" and "usercustomize.py".
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
If the user has custom scripts, we think that the impact on the performance
|
2021-01-05 12:58:14 -05:00
|
|
|
of walking each of the folders is acceptable, as the user wants to use this
|
|
|
|
feature. If they need to run a time-sensitive application, they can always
|
|
|
|
use ``-S`` to disable this entirely.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Running "./python -c pass" with perf on 50 iterations, repeating 50 times the
|
|
|
|
command on each and getting the geometric mean on a commodity laptop did not
|
2021-01-05 12:58:14 -05:00
|
|
|
reveal any substantial raise on CPU time.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Failure handling
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Any error on any of the scripts will not be logged unless the interpreter is
|
|
|
|
run in verbose mode and it should not stop the evaluation of other scripts.
|
|
|
|
The user will just receive a message saying that the script failed to be
|
|
|
|
executed, that verbose mode can be used to get more information. This
|
2021-01-04 06:49:52 -05:00
|
|
|
behaviour follows the one already existing for ``sitecustomize.py``.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Scripts naming convention
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
Packages will be encouraged to include the name of the package within the
|
2021-01-04 06:49:52 -05:00
|
|
|
name of the script to avoid collisions between packages.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Relationship with sitecustomize and usercustomize
|
|
|
|
-------------------------------------------------
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
The existing logic for ``sitecustomize.py`` and ``usercustomize.py`` will be left
|
|
|
|
as is, later deprecated and scheduled for removal. Once ``__sitecustomize__`` is
|
2020-12-30 16:28:56 -05:00
|
|
|
supported, it will provide better integration for all existing users, and even
|
2021-01-04 06:49:52 -05:00
|
|
|
if it will indeed require a migration for system administrators, we expect the
|
2020-12-30 16:28:56 -05:00
|
|
|
effort required to be minimal, it will just require moving and renaming the
|
2021-01-04 06:49:52 -05:00
|
|
|
current ``sitecustomize.py`` into the new provided folder.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Identifying all installed scripts
|
|
|
|
---------------------------------
|
|
|
|
|
|
|
|
To facilitate debugging of the Python startup, a new option will be added to
|
|
|
|
the main of the site module to list all scripts that will be executed as part
|
2021-01-04 06:49:52 -05:00
|
|
|
of the ``__sitecustomize__`` initialization.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
How to teach this
|
|
|
|
=================
|
|
|
|
|
|
|
|
This can be documented and taught as simple as saying that the interpreter
|
2021-01-05 12:58:14 -05:00
|
|
|
will try to look for the ``__sitecustomize__`` folder at startup in its site
|
|
|
|
paths and if it finds any scripts with ``.py`` extension, it will then
|
|
|
|
execute it one by one.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
For system administrators and tools that package the interpreter, we can now
|
2021-01-04 06:49:52 -05:00
|
|
|
recommend placing files in ``__sitecustomize__`` as they used to place
|
|
|
|
``sitecustomize.py``. Being more comfortable on that their content won't be
|
2020-12-30 16:28:56 -05:00
|
|
|
overridden by the next person, as they can provide with specific files to
|
|
|
|
handle the logic they want to customize.
|
|
|
|
|
|
|
|
Library developers should be able to specify a new argument on tools like
|
|
|
|
setuptools that will inject those new files. Something like
|
2021-01-04 06:49:52 -05:00
|
|
|
``sitecustomize_scripts=["scripts/betterexceptions.py"]``, which allows them to
|
2020-12-30 16:28:56 -05:00
|
|
|
add those. Should the build backend not support that, they can manually
|
2021-01-04 06:49:52 -05:00
|
|
|
install them as they used to do with ``pth`` files. We will recommend them to
|
|
|
|
include the name of the package as part of the script's name.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Backward compatibility
|
|
|
|
======================
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
We propose to add support for ``__sitecustomize__`` in the next release of
|
2020-12-30 16:28:56 -05:00
|
|
|
Python, add a warning on the three next releases on the deprecation and
|
2021-01-04 06:49:52 -05:00
|
|
|
future removal of ``sitecustomize.py``, ``usercustomize.py`` and code execution
|
|
|
|
in ``pth`` files, and remove it after maintainers have had 4 releases to
|
2020-12-30 16:28:56 -05:00
|
|
|
migrate. Ignoring those lines in pth files.
|
|
|
|
|
2021-01-05 12:58:14 -05:00
|
|
|
Whilst the existing ``sitecutzomize.py`` mechanism was created targeting
|
|
|
|
System Administrators that placed it in a site path, the file could be
|
|
|
|
actually placed anywhere in the path at the time that the interpreter was
|
|
|
|
starting up. The new mechanism does not allow for users to place
|
|
|
|
``__sitecustomize__`` folders anywhere in the path, but only in site paths.
|
|
|
|
System administrators can recover a similar behavior to ``sitecustomize.py``
|
|
|
|
if they need it by adding a custom script in ``__sitecustomize__`` which just
|
|
|
|
imports ``sitecustomize`` as a migration path.
|
|
|
|
|
2020-12-30 16:28:56 -05:00
|
|
|
Reference Implementation
|
|
|
|
========================
|
|
|
|
|
|
|
|
An initial implementation that passes the CPython test suite is available for
|
|
|
|
evaluation [#reference-implementation]_.
|
|
|
|
|
|
|
|
This implementation is just for the reviewer to play with and check potential
|
|
|
|
issues that this PEP could generate.
|
|
|
|
|
|
|
|
Rejected Ideas
|
|
|
|
==============
|
|
|
|
|
|
|
|
Do nothing
|
|
|
|
----------
|
|
|
|
|
|
|
|
Whilst the current status "works" it presents the issues listed in the
|
|
|
|
motivation. After analysing the impact of this change, we believe it is worth
|
|
|
|
given the enhanced experience it brings.
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
Formalize using ``pth`` files
|
|
|
|
-----------------------------
|
2020-12-30 16:28:56 -05:00
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
Another option would be to just glorify and document the usage of ``pth`` files
|
2020-12-30 16:28:56 -05:00
|
|
|
to inject code at startup code, but that is a suboptimal experience for users
|
|
|
|
as listed in the motivation.
|
|
|
|
|
2021-01-05 12:58:14 -05:00
|
|
|
Making ``__sitecustomize__`` a namespace package
|
|
|
|
------------------------------------------------
|
|
|
|
|
|
|
|
We considered making the folder a namespace package and just import all the
|
|
|
|
modules within it, which allowed searching across all paths in ``sys.path``
|
|
|
|
at initialization time and provided a way to declare dependencies between
|
|
|
|
scripts by importing each other. This was rejected for multiple reasons:
|
|
|
|
|
|
|
|
1. This was unnecessarily broadening the list of paths where arbitrary scripts
|
|
|
|
are executed.
|
|
|
|
2. The logic brought additional complexity, like what to do if a package were
|
|
|
|
to install an ``__init__.py`` file in one of the locations.
|
|
|
|
3. It's cheaper to search for ``__sitecustomize__`` as we are looking for
|
|
|
|
``pth`` files already in the site paths compared to performing an actual
|
|
|
|
import of a namespace package.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
|
|
|
Support for shutdown custom scripts
|
|
|
|
-----------------------------------
|
|
|
|
|
2021-01-04 06:49:52 -05:00
|
|
|
``init.d`` users might be tempted to implement this feature in a way that users
|
2020-12-30 16:28:56 -05:00
|
|
|
could also add code at shutdown, but extra support for that is not needed, as
|
2021-01-04 06:49:52 -05:00
|
|
|
Python users can already do that via ``atexit``.
|
2020-12-30 16:28:56 -05:00
|
|
|
|
2021-01-12 12:26:30 -05:00
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document is placed in the public domain or under the CC0-1.0-Universal
|
|
|
|
license, whichever is more permissive.
|
|
|
|
|
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
2020-12-30 16:28:56 -05:00
|
|
|
.. [#bpo-24534]
|
|
|
|
https://bugs.python.org/issue24534
|
|
|
|
|
|
|
|
.. [#bpo-33944]
|
|
|
|
https://bugs.python.org/issue33944
|
|
|
|
|
|
|
|
.. [#s-flag]
|
|
|
|
https://docs.python.org/3/using/cmdline.html#id3
|
|
|
|
|
|
|
|
.. [#setuptools]
|
|
|
|
https://github.com/pypa/setuptools/blob/b6bbe236ed0689f50b5148f1172510b975687e62/setup.py#L100
|
|
|
|
|
|
|
|
.. [#betterexceptions]
|
|
|
|
https://github.com/Qix-/better-exceptions/blob/7b417527757d555faedc354c86d3b6fe449200c2/better_exceptions_hook.pth#L1
|
|
|
|
|
|
|
|
.. [#reference-implementation]
|
|
|
|
https://github.com/mariocj89/cpython/tree/pu/__sitecustomize__
|