-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-150228: Improve the PEP 829 batch processing APIs #150542
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
base: main
Are you sure you want to change the base?
Changes from all commits
385e132
ab6f2ac
6cc7ccc
498f853
19b8b15
e2407d2
5405f69
c15af5b
1821c70
d54be32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -356,7 +356,27 @@ Module contents | |
| This function used to be called unconditionally. | ||
|
|
||
|
|
||
| .. function:: addsitedir(sitedir, known_paths=None, *, defer_processing_start_files=False) | ||
| .. class:: StartupState(known_paths=None) | ||
|
|
||
| Instances of this class are used as an accumulator for interpreter startup | ||
| configuration data, such as ``.pth`` and ``.start`` files, from one or more | ||
| site directories. These are used to batch the processing of these startup | ||
| files. The optional *known_paths* argument is a set of case-normalized | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does one get "a set of case-normalized paths" if not using the default? As a public API that feels like a footgun. Accepting an iterable or sequence of paths and doing that normalization for the user would be easier to use. |
||
| paths used to prevent duplicate :data:`sys.path` entries. With ``None`` | ||
| (the default), this set is built from the current :data:`sys.path`. | ||
| :func:`main` implicitly uses an instance of this class. | ||
|
|
||
| .. method:: process() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API question: calling this twice will re-exec imports and entry point code twice. At a minimum, document that this should not be done and is undefined behavior. Ideally protect against it. Can we have it consume the internal state (draining the internal path entries, import execs, and entrypoints) so that a repeat call is a no-op? |
||
|
|
||
| Apply the accumulated state by first adding the path extensions to | ||
| :data:`sys.path`, then executing the :file:`.start` file entry points | ||
| and :file:`.pth` file ``import`` lines (:ref:`deprecated | ||
|
Comment on lines
+371
to
+373
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the ordering here does not match the code in site.py's process(): make sure this and the docstring and the code all agree. (claude /review flagged this)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I worded it this way because a) I want to consistently emphasize the |
||
| <site-pth-files>`). | ||
|
|
||
| .. versionadded:: 3.15 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be moved up to apply to the whole class, not the method. |
||
|
|
||
|
|
||
| .. function:: addsitedir(sitedir, known_paths=None, *, startup_state=None) | ||
|
|
||
| Add a directory to sys.path and parse the :file:`.pth` and :file:`.start` | ||
| files found in that directory. Typically used in :mod:`sitecustomize` or | ||
|
|
@@ -366,17 +386,39 @@ Module contents | |
| used to prevent duplicate :data:`sys.path` entries. When ``None`` (the | ||
| default), the set is built from the current :data:`sys.path`. | ||
|
|
||
| While :file:`.pth` and :file:`.start` files are always parsed, set | ||
| *defer_processing_start_files* to ``True`` to prevent processing the | ||
| startup data found in those files, so that you can process them explicitly | ||
| (this is typically used by the :func:`main` function). | ||
| Pass an instance of :class:`StartupState` as *startup_state* to accumulate | ||
| startup data from multiple site directories before explicitly processing | ||
| with :meth:`StartupState.process`. The *known_paths* and *startup_state* | ||
| arguments cannot both be given. | ||
|
|
||
| For example: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| state = site.StartupState() | ||
| for sitedir in site_dirs: | ||
| site.addsitedir(sitedir, startup_state=state) | ||
| state.process() | ||
|
|
||
| Semantics and return values: | ||
|
|
||
| * When only *sitedir* is given, startup configuration is processed before | ||
| the function returns, and ``None`` is returned. | ||
| * When *known_paths* is given, startup configuration is processed before the | ||
| function returns, and the updated *known_paths* is returned. | ||
| * When *startup_state* is given, startup configuration is **not** | ||
| processed, and the state instance is returned. It is up to the caller to | ||
| call :meth:`StartupState.process` on this instance. | ||
| * It is a :exc:`TypeError` to pass both *known_paths* and *startup_state*. | ||
|
|
||
| .. versionchanged:: 3.15 | ||
|
|
||
| Also processes :file:`.start` files. See :ref:`site-start-files`. | ||
| All :file:`.pth` and :file:`.start` files are now read and | ||
| accumulated before any path extensions, ``import`` line execution, | ||
| or entry point invocations take place. | ||
| Also processes :file:`.start` files. See :ref:`site-start-files`. All | ||
| :file:`.pth` and :file:`.start` files are now read and accumulated | ||
| before any path extensions, entry point invocations, or ``import`` line | ||
| execution take place. | ||
|
|
||
| The *startup_state* keyword-only argument was added. | ||
|
|
||
|
|
||
| .. function:: getsitepackages() | ||
|
|
@@ -447,4 +489,3 @@ value greater than 2 if there is an error. | |
| * :pep:`370` -- Per user site-packages directory | ||
| * :pep:`829` -- Startup entry points and the deprecation of import lines in ``.pth`` files | ||
| * :ref:`sys-path-init` -- The initialization of :data:`sys.path`. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should StartupState.read_pth_file() and StartupState.read_start_file() be documented? they're public by API name. if not, why not make them _private named?