| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f442665 commit 27ebd9a
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -356,7 +356,79 @@ Module contents | |||
| 356 | 356 | This function used to be called unconditionally. | |
| 357 | 357 | ||
| 358 | 358 | ||
| 359 | - .. function:: addsitedir(sitedir, known_paths=None, *, defer_processing_start_files=False) | ||
| 359 | + .. function:: makepath(*paths) | ||
| 360 | + | ||
| 361 | + Join *paths* with :func:`os.path.join`, attempt to make the result | ||
| 362 | + absolute with :func:`os.path.abspath`, and return a 2-tuple containing | ||
| 363 | + the absolute path and its case-normalized form as produced by | ||
| 364 | + :func:`os.path.normcase`. If :func:`os.path.abspath` raises | ||
| 365 | + :exc:`OSError`, the joined path is used unchanged for the | ||
| 366 | + case-normalization step. | ||
| 367 | + | ||
| 368 | + The second element of the returned tuple is the form used throughout the | ||
| 369 | + :mod:`!site` module to compare paths on case-insensitive file systems, and | ||
| 370 | + is what populates the ``known_paths`` sets that prevent duplicate | ||
| 371 | + :data:`sys.path` entries in various APIs within this module. | ||
| 372 | + | ||
| 373 | + | ||
| 374 | + .. class:: StartupState(known_paths=None) | ||
| 375 | + | ||
| 376 | + Instances of this class accumulate interpreter startup configuration data | ||
| 377 | + from one or more site directories. They are the preferred interface for | ||
| 378 | + batching the processing of :file:`.pth` and :file:`.start` files across | ||
| 379 | + multiple site directories, so that every :data:`sys.path` extension is | ||
| 380 | + visible before any startup code runs. | ||
| 381 | + | ||
| 382 | + The optional *known_paths* argument is a set of case-normalized paths | ||
| 383 | + (which can be produced by :func:`makepath`) used to prevent duplicate | ||
| 384 | + :data:`sys.path` entries. When ``None`` (the default), the set is built | ||
| 385 | + from the current :data:`sys.path`. :func:`main` implicitly uses an | ||
| 386 | + instance of this class. | ||
| 387 | + | ||
| 388 | + Typical use: | ||
| 389 | + | ||
| 390 | + .. code-block:: python | ||
| 391 | + | ||
| 392 | + state = site.StartupState() | ||
| 393 | + for sitedir in site_dirs: | ||
| 394 | + state.addsitedir(sitedir) | ||
| 395 | + state.process() | ||
| 396 | + | ||
| 397 | + .. versionadded:: 3.15 | ||
| 398 | + | ||
| 399 | + .. method:: addsitedir(sitedir) | ||
| 400 | + | ||
| 401 | + Read the :file:`.pth` and :file:`.start` files in *sitedir* and | ||
| 402 | + record their :data:`sys.path` extensions, deprecated :file:`.pth` | ||
| 403 | + ``import`` lines, and :file:`.start` entry points on this state. | ||
| 404 | + The recorded data is not applied until :meth:`process` is called. | ||
| 405 | + | ||
| 406 | + .. method:: addusersitepackages() | ||
| 407 | + | ||
| 408 | + Add the per-user site-packages directory, if enabled and if it exists. | ||
| 409 | + The directory's startup data is accumulated for later processing by | ||
| 410 | + :meth:`process`. | ||
| 411 | + | ||
| 412 | + .. method:: addsitepackages(prefixes=None) | ||
| 413 | + | ||
| 414 | + Add global site-packages directories, computed from *prefixes* or from | ||
| 415 | + the global :data:`PREFIXES` when *prefixes* is ``None``. Each | ||
| 416 | + directory's startup data is accumulated for later processing by | ||
| 417 | + :meth:`process`. | ||
| 418 | + | ||
| 419 | + .. method:: process() | ||
| 420 | + | ||
| 421 | + Apply the accumulated state by first adding the path extensions to | ||
| 422 | + :data:`sys.path`, then executing the :file:`.start` file entry points | ||
| 423 | + and :file:`.pth` file ``import`` lines (:ref:`deprecated | ||
| 424 | + <site-pth-files>`). | ||
| 425 | + | ||
| 426 | + This method is not idempotent and must not be called more than once | ||
| 427 | + on the same instance. Doing so will apply the accumulated state | ||
| 428 | + more than once, re-running entry points and ``import`` lines. | ||
| 429 | + | ||
| 430 | + | ||
| 431 | + .. function:: addsitedir(sitedir, known_paths=None) | ||
| 360 | 432 | ||
| 361 | 433 | Add a directory to sys.path and parse the :file:`.pth` and :file:`.start` | |
| 362 | 434 | files found in that directory. Typically used in :mod:`sitecustomize` or | |
@@ -366,17 +438,15 @@ Module contents | |||
| 366 | 438 | used to prevent duplicate :data:`sys.path` entries. When ``None`` (the | |
| 367 | 439 | default), the set is built from the current :data:`sys.path`. | |
| 368 | 440 | ||
| 369 | - While :file:`.pth` and :file:`.start` files are always parsed, set | ||
| 370 | - *defer_processing_start_files* to ``True`` to prevent processing the | ||
| 371 | - startup data found in those files, so that you can process them explicitly | ||
| 372 | - (this is typically used by the :func:`main` function). | ||
| 441 | + For batched processing across multiple site directories, build a | ||
| 442 | + :class:`StartupState` explicitly and call :meth:`StartupState.addsitedir` | ||
| 443 | + on it; that defers :file:`.pth` and :file:`.start` processing until a | ||
| 444 | + single :meth:`StartupState.process` call, ensuring every :data:`sys.path` | ||
| 445 | + extension is visible before any startup code runs. | ||
| 373 | 446 | ||
| 374 | 447 | .. versionchanged:: 3.15 | |
| 375 | 448 | ||
| 376 | 449 | Also processes :file:`.start` files. See :ref:`site-start-files`. | |
| 377 | - All :file:`.pth` and :file:`.start` files are now read and | ||
| 378 | - accumulated before any path extensions, ``import`` line execution, | ||
| 379 | - or entry point invocations take place. | ||
| 380 | 450 | ||
| 381 | 451 | ||
| 382 | 452 | .. function:: getsitepackages() | |
@@ -447,4 +517,3 @@ value greater than 2 if there is an error. | |||
| 447 | 517 | * :pep:`370` -- Per user site-packages directory | |
| 448 | 518 | * :pep:`829` -- Startup entry points and the deprecation of import lines in ``.pth`` files | |
| 449 | 519 | * :ref:`sys-path-init` -- The initialization of :data:`sys.path`. | |
| 450 | - | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -478,7 +478,18 @@ matching :file:`.start` file is found, ``import`` lines in :file:`.pth` files | |||
| 478 | 478 | are ignored. There is no change to :data:`sys.path` extension lines in | |
| 479 | 479 | :file:`.pth` files. | |
| 480 | 480 | ||
| 481 | - (Contributed by Barry Warsaw in :gh:`148641`.) | ||
| 481 | + The :mod:`site` module also provides :class:`site.StartupState` to batch | ||
| 482 | + startup processing for multiple site directories, ensuring all static path | ||
| 483 | + extensions are applied before any startup code is executed. :func:`site.main` | ||
| 484 | + uses an instance of this class implicitly to batch process all startup | ||
| 485 | + configuration files during normal interpreter startup. Callers needing the | ||
| 486 | + same batching behavior can build a :class:`~site.StartupState` directly and | ||
| 487 | + drive it with :meth:`~site.StartupState.addsitedir`, | ||
| 488 | + :meth:`~site.StartupState.addusersitepackages`, and | ||
| 489 | + :meth:`~site.StartupState.addsitepackages`, then call | ||
| 490 | + :meth:`~site.StartupState.process` once at the end of the batch. | ||
| 491 | + | ||
| 492 | + (Contributed by Barry Warsaw in :gh:`148641` and :gh:`150228`.) | ||
| 482 | 493 | ||
| 483 | 494 | ||
| 484 | 495 | .. _whatsnew315-abi3t: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments