Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c09fca6

Browse files
committed
Do not touch sys.path when site is imported and python was started with -S.
Original patch by Carl Meyer, review by Brett Cannon, small doc editions by yours truly. Fixes #11591.
1 parent f59c7b2 commit c09fca6

6 files changed

Lines changed: 36 additions & 5 deletions

File tree

Doc/library/site.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import can be suppressed using the interpreter's :option:`-S` option.
1313

1414
.. index:: triple: module; search; path
1515

16-
Importing this module will append site-specific paths to the module search path.
16+
Importing this module will append site-specific paths to the module search
17+
path, unless :option:`-S` was used. In that case, this module can be safely
18+
imported with no automatic modifications to the module search path. To
19+
explicitly trigger the usual site-specific additions, call the
20+
:func:`site.main` function.
1721

1822
.. index::
1923
pair: site-python; directory
@@ -114,6 +118,13 @@ empty, and the path manipulations are skipped; however the import of
114118
.. envvar:: PYTHONUSERBASE
115119

116120

121+
.. function:: main()
122+
123+
Adds all the standard site-specific directories to the module search
124+
path. This function is called automatically when this module is imported,
125+
unless the :program:`python` interpreter was started with the :option:`-S`
126+
flag.
127+
117128
.. function:: addsitedir(sitedir, known_paths=None)
118129

119130
Adds a directory to sys.path and processes its pth files.

Doc/using/cmdline.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ Miscellaneous options
239239
.. cmdoption:: -S
240240

241241
Disable the import of the module :mod:`site` and the site-dependent
242-
manipulations of :data:`sys.path` that it entails.
242+
manipulations of :data:`sys.path` that it entails. Also disable these
243+
manipulations if :mod:`site` is explicitly imported later (call
244+
:func:`site.main` if you want them to be triggered).
243245

244246

245247
.. cmdoption:: -u

Doc/whatsnew/3.3.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,8 @@ that may require changes to your code:
128128

129129
* Stub
130130

131+
132+
.. Issue #11591: When :program:`python` was started with :option:`-S`,
133+
``import site`` will not add site-specific paths to the module search
134+
paths. In previous versions, it did. See changeset for doc changes in
135+
various files. Contributed by Carl Meyer with editions by Éric Araujo.

Lib/site.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ def execusercustomize():
508508

509509

510510
def main():
511+
"""Add standard site-specific directories to the module search path.
512+
513+
This function is called automatically when this module is imported,
514+
unless the python interpreter was started with the -S flag.
515+
"""
511516
global ENABLE_USER_SITE
512517

513518
abs_paths()
@@ -526,7 +531,10 @@ def main():
526531
if ENABLE_USER_SITE:
527532
execusercustomize()
528533

529-
main()
534+
# Prevent edition of sys.path when python was started with -S and
535+
# site is imported later.
536+
if not sys.flags.no_site:
537+
main()
530538

531539
def _script():
532540
help = """\

Misc/NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,15 @@ Core and Builtins
8181
Library
8282
-------
8383

84+
- Issue #11591: Prevent "import site" from modifying sys.path when python
85+
was started with -S.
86+
8487
- Issue #11371: Mark getopt error messages as localizable. Patch by Filip
8588
Gruszczyński.
8689

8790
- Issue #11333: Add __slots__ to collections ABCs.
8891

89-
- Issue #11628: cmp_to_key generated class should use __slots__
92+
- Issue #11628: cmp_to_key generated class should use __slots__.
9093

9194
- Issue #5537: Fix time2isoz() and time2netscape() functions of
9295
httplib.cookiejar for expiration year greater than 2038 on 32-bit systems.

Misc/python.man

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ Disable the import of the module
169169
.I site
170170
and the site-dependent manipulations of
171171
.I sys.path
172-
that it entails.
172+
that it entails. Also disable these manipulations if
173+
.I site
174+
is explicitly imported later.
173175
.TP
174176
.B \-u
175177
Force the binary I/O layers of stdin, stdout and stderr to be unbuffered.

0 commit comments

Comments
 (0)