-
-
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
Open
warsaw
wants to merge
16
commits into
python:main
Choose a base branch
from
warsaw:gh150228
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+494
−349
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
385e132
gh-150228: Improve the PEP 829 batch processing APIs
warsaw ab6f2ac
Elaborate what's new
warsaw 6cc7ccc
Merge branch 'main' into gh150228
warsaw 498f853
Fix markup typo
warsaw 19b8b15
Update Misc/NEWS.d/next/Library/2026-05-27-11-18-36.gh-issue-150228.p…
warsaw e2407d2
Update Misc/NEWS.d/next/Library/2026-05-27-11-18-36.gh-issue-150228.p…
warsaw 5405f69
Fix a typo and point the what's new to the issue not the branch
warsaw c15af5b
Some minor improvements
warsaw 1821c70
Merge branch 'main' into gh150228
warsaw d54be32
Merge branch 'main' into gh150228
warsaw 5c0faf5
Make test_impl_exec_imports_suppressed_by_matching_start() more robust
warsaw a329772
As per review, move some methods to the private API
warsaw 74b0dab
Resolve several review feedbacks
warsaw 0917fc4
Merge branch 'main' into gh150228
warsaw 16ba1e3
Update documentation
warsaw 56e1b9a
Address another feedback comment
warsaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -356,7 +356,47 @@ Module contents | |||||
| This function used to be called unconditionally. | ||||||
|
|
||||||
|
|
||||||
| .. function:: addsitedir(sitedir, known_paths=None, *, defer_processing_start_files=False) | ||||||
| .. function:: makepath(*paths) | ||||||
|
|
||||||
| Join *paths* with :func:`os.path.join`, attempt to make the result | ||||||
| absolute with :func:`os.path.abspath`, and return a 2-tuple containing | ||||||
| the absolute path and its case-normalized form as produced by | ||||||
| :func:`os.path.normcase`. If :func:`os.path.abspath` raises | ||||||
| :exc:`OSError`, the joined path is used unchanged for the | ||||||
| case-normalization step. | ||||||
|
|
||||||
| The second element of the returned tuple is the form used throughout the | ||||||
| :mod:`!site` module to compare paths on case-insensitive file systems, and | ||||||
| is what populates the ``known_paths`` sets that prevent duplicate | ||||||
| :data:`sys.path` entries in various APIs within this module. | ||||||
|
|
||||||
|
|
||||||
| .. 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 | ||||||
|
warsaw marked this conversation as resolved.
|
||||||
| paths (which can be produced by :func:`makepath`) 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. | ||||||
|
|
||||||
| .. versionadded:: 3.15 | ||||||
|
|
||||||
| .. method:: process() | ||||||
|
warsaw marked this conversation as resolved.
|
||||||
|
|
||||||
| 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 | ||||||
|
warsaw marked this conversation as resolved.
|
||||||
| <site-pth-files>`). | ||||||
|
|
||||||
| This method is not idempotent and must not be called more than once | ||||||
| on the same instance. Doing so would apply the accumulated state | ||||||
|
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. Is this a contract, or detail of the current implementation?
Suggested change
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. Why "could" instead of "would"? |
||||||
| more than once, re-running entry points and ``import`` lines. | ||||||
|
|
||||||
|
|
||||||
| .. 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 +406,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 +509,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`. | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.