]> granicus.if.org Git - python/commitdiff
Do not touch sys.path when site is imported and python was started with -S.
authorÉric Araujo <merwok@netwok.org>
Wed, 23 Mar 2011 01:06:24 +0000 (02:06 +0100)
committerÉric Araujo <merwok@netwok.org>
Wed, 23 Mar 2011 01:06:24 +0000 (02:06 +0100)
Original patch by Carl Meyer, review by Brett Cannon, small doc editions by
yours truly.  Fixes #11591.

Doc/library/site.rst
Doc/using/cmdline.rst
Doc/whatsnew/3.3.rst
Lib/site.py
Misc/NEWS
Misc/python.man

index b77f3cf02d3476f513d79e3143e39182caad6324..4b7a2344464ea665ed05a7f30043e342bcb1f25b 100644 (file)
@@ -13,7 +13,11 @@ import can be suppressed using the interpreter's :option:`-S` option.
 
 .. index:: triple: module; search; path
 
-Importing this module will append site-specific paths to the module search path.
+Importing this module will append site-specific paths to the module search
+path, unless :option:`-S` was used.  In that case, this module can be safely
+imported with no automatic modifications to the module search path.  To
+explicitly trigger the usual site-specific additions, call the
+:func:`site.main` function.
 
 .. index::
    pair: site-python; directory
@@ -114,6 +118,13 @@ empty, and the path manipulations are skipped; however the import of
 .. envvar:: PYTHONUSERBASE
 
 
+.. function:: main()
+
+   Adds all the standard site-specific directories to the module search
+   path.  This function is called automatically when this module is imported,
+   unless the :program:`python` interpreter was started with the :option:`-S`
+   flag.
+
 .. function:: addsitedir(sitedir, known_paths=None)
 
    Adds a directory to sys.path and processes its pth files.
index d1f47eba89738f57e4ca020275b614028f71c21a..b5a9b32c74da57226e3b420ea2a1aae8ce68b1e9 100644 (file)
@@ -239,7 +239,9 @@ Miscellaneous options
 .. cmdoption:: -S
 
    Disable the import of the module :mod:`site` and the site-dependent
-   manipulations of :data:`sys.path` that it entails.
+   manipulations of :data:`sys.path` that it entails.  Also disable these
+   manipulations if :mod:`site` is explicitly imported later (call
+   :func:`site.main` if you want them to be triggered).
 
 
 .. cmdoption:: -u
index d86826ca9ea6a3b6a2ce35b0a063f58ce19cd75b..7f05a846fc641354063896b1ebaf85871db964c6 100644 (file)
@@ -128,3 +128,8 @@ that may require changes to your code:
 
 * Stub
 
+
+.. Issue #11591: When :program:`python` was started with :option:`-S`,
+   ``import site`` will not add site-specific paths to the module search
+   paths.  In previous versions, it did.  See changeset for doc changes in
+   various files.  Contributed by Carl Meyer with editions by Éric Araujo.
index a2c0becbc1e935a15dcaf4e1921d6ce1d82d149f..fcfdbedf269ac9881bbd4ef68bc085709e9b886b 100644 (file)
@@ -508,6 +508,11 @@ def execusercustomize():
 
 
 def main():
+    """Add standard site-specific directories to the module search path.
+
+    This function is called automatically when this module is imported,
+    unless the python interpreter was started with the -S flag.
+    """
     global ENABLE_USER_SITE
 
     abs_paths()
@@ -526,7 +531,10 @@ def main():
     if ENABLE_USER_SITE:
         execusercustomize()
 
-main()
+# Prevent edition of sys.path when python was started with -S and
+# site is imported later.
+if not sys.flags.no_site:
+    main()
 
 def _script():
     help = """\
index e8a19264565740712f08876f60638c06818c118f..363d5e99b34ac049acfaff0b4186ec06ee24107e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,12 +81,15 @@ Core and Builtins
 Library
 -------
 
+- Issue #11591: Prevent "import site" from modifying sys.path when python
+  was started with -S.
+
 - Issue #11371: Mark getopt error messages as localizable.  Patch by Filip
   Gruszczyński.
 
 - Issue #11333: Add __slots__ to collections ABCs.
 
-- Issue #11628: cmp_to_key generated class should use __slots__
+- Issue #11628: cmp_to_key generated class should use __slots__.
 
 - Issue #5537: Fix time2isoz() and time2netscape() functions of
   httplib.cookiejar for expiration year greater than 2038 on 32-bit systems.
index 2d15d5d3368858139c35cf63598c9dfa5fe77455..53b77fcd1bc2120cf31a7278c6fe6284e667ebb8 100644 (file)
@@ -169,7 +169,9 @@ Disable the import of the module
 .I site
 and the site-dependent manipulations of
 .I sys.path
-that it entails.
+that it entails.  Also disable these manipulations if
+.I site
+is explicitly imported later.
 .TP
 .B \-u
 Force the binary I/O layers of stdin, stdout and stderr to be unbuffered.