diff options
author | Victor Stinner <vstinner@python.org> | 2022-05-05 23:34:11 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-05 23:34:11 (GMT) |
commit | ada8b6d1b1b02ae7c38f161c2a0ad866559fe18b (patch) | |
tree | 2da5c4813d3cf360a121a49268aa9fd2c9fe3cbb /Doc/library | |
parent | f6dd14c65336cda4e2ebccbc6408dfe3b0a68a34 (diff) | |
download | cpython-ada8b6d1b1b02ae7c38f161c2a0ad866559fe18b.zip cpython-ada8b6d1b1b02ae7c38f161c2a0ad866559fe18b.tar.gz cpython-ada8b6d1b1b02ae7c38f161c2a0ad866559fe18b.tar.bz2 |
gh-57684: Add -P cmdline option and PYTHONSAFEPATH env var (#31542)
Add the -P command line option and the PYTHONSAFEPATH environment
variable to not prepend a potentially unsafe path to sys.path.
* Add sys.flags.safe_path flag.
* Add PyConfig.safe_path member.
* Programs/_bootstrap_python.c uses config.safe_path=0.
* Update subprocess._optim_args_from_interpreter_flags() to handle
the -P command line option.
* Modules/getpath.py sets safe_path to 1 if a "._pth" file is
present.
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/sys.rst | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index f433448..5f3b9b5 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -520,6 +520,7 @@ always available. :const:`hash_randomization` :option:`-R` :const:`dev_mode` :option:`-X dev <-X>` (:ref:`Python Development Mode <devmode>`) :const:`utf8_mode` :option:`-X utf8 <-X>` + :const:`safe_path` :option:`-P` ============================= ================================================================ .. versionchanged:: 3.2 @@ -539,6 +540,9 @@ always available. Mode <devmode>` and the ``utf8_mode`` attribute for the new :option:`-X` ``utf8`` flag. + .. versionchanged:: 3.11 + Added the ``safe_path`` attribute for :option:`-P` option. + .. data:: float_info @@ -1138,15 +1142,19 @@ always available. the environment variable :envvar:`PYTHONPATH`, plus an installation-dependent default. - As initialized upon program startup, the first item of this list, ``path[0]``, - is the directory containing the script that was used to invoke the Python - interpreter. If the script directory is not available (e.g. if the interpreter - is invoked interactively or if the script is read from standard input), - ``path[0]`` is the empty string, which directs Python to search modules in the - current directory first. Notice that the script directory is inserted *before* - the entries inserted as a result of :envvar:`PYTHONPATH`. + By default, as initialized upon program startup, a potentially unsafe path + is prepended to :data:`sys.path` (*before* the entries inserted as a result + of :envvar:`PYTHONPATH`): + + * ``python -m module`` command line: prepend the current working + directory. + * ``python script.py`` command line: prepend the script's directory. + If it's a symbolic link, resolve symbolic links. + * ``python -c code`` and ``python`` (REPL) command lines: prepend an empty + string, which means the current working directory. - The initialization of :data:`sys.path` is documented at :ref:`sys-path-init`. + To not prepend this potentially unsafe path, use the :option:`-P` command + line option or the :envvar:`PYTHONSAFEPATH` environment variable? A program is free to modify this list for its own purposes. Only strings and bytes should be added to :data:`sys.path`; all other data types are |