Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-57684: Add -P cmdline option and PYTHONSAFEPATH env var#31542

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 2 commits intopython:mainfromvstinner:add_path0
May 5, 2022
Merged

gh-57684: Add -P cmdline option and PYTHONSAFEPATH env var#31542

vstinner merged 2 commits intopython:mainfromvstinner:add_path0
May 5, 2022

Conversation

@vstinner
Copy link
Member

@vstinnervstinner commentedFeb 24, 2022
edited
Loading

  • Add -P command line option to not prepend an 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.
  • Add PYTHONSAFEPATH environment variable.

https://bugs.python.org/issue13475

pxeger, zopsicle, wimglenn, and jstriebel reacted with thumbs up emojiCAM-Gerlach, hroncok, ofek, flying-sheep, spiccinini, Hnasar, and jstriebel reacted with hooray emoji
@vstinner
Copy link
MemberAuthor

@CAM-Gerlach
Copy link
Member

CAM-Gerlach commentedFeb 24, 2022
edited
Loading

Thanks for this! This is a long-awaited and very useful change.

I was going to suggest fixes to some grammar errors and phrasing issues in the docs that were added, as well as an issue with the description in theman page sounding pretty cryptic for the average user, but I noticed this PR was still in a draft state. Is that just due to

https://docs.python.org/3/using/cmdline.html#cmdoption-c should be updated.

and it is otherwise ready for review? I'd be happy to help with that too, but it looks like a pretty small change, just updating it to reflect that an empty string rather than the working dir is added tosys.path[0].

hroncok reacted with thumbs up emoji

@hroncok
Copy link
Contributor

hroncok commentedFeb 24, 2022
edited
Loading

FTR I considerDon't addsys.path[0] a bit confusing, as there will always besomesys.path[0]. What aboutDon'tprependsys.path with anything?

torsava and CAM-Gerlach reacted with thumbs up emoji

@CAM-Gerlach
Copy link
Member

FTR I considerDon't addsys.path[0] a bit confusing, as there will always besomesys.path[0]. What aboutDon'tprependsys.path with anything?

Yep, I noticed that as well above. I can't claim either of your expertise, but I'm not a beginner either and have actively been following the progress of and looking forward to this feature, and yet I had to carefully re-read the linked issue to be sure that it was actually doing what I thought it was. As both a developer and a documentarian, it can be easy for me to write documentation (or PEPs, etc) that makes perfect sense to me but is confusing to others without deep implicit knowledge of the code—I've seen both sides of that a lot lately, as well as the value of having someone else look over it.

@ofek
Copy link
Contributor

Awesome! Is there an associated environment variable?

@CAM-Gerlach
Copy link
Member

Awesome! Is there an associated environment variable?

It would appear there isn't, as of yet.

@vstinnervstinner marked this pull request as ready for reviewApril 26, 2022 09:22
@vstinner
Copy link
MemberAuthor

I rebased my PR and marked it as ready for review ;-)

CAM-Gerlach reacted with heart emoji

@ofek
Copy link
Contributor

Can we add an associated environment variable?

@vstinner
Copy link
MemberAuthor

Since different people asked for an environment variable, I changed my mind and I addedPYTHONDONTADDPATH0 environment variable. Example:

$ ./python -c 'import sys, pprint; pprint.pprint(sys.path)'['', '/usr/local/lib/python311.zip', '/home/vstinner/python/main/Lib', '/home/vstinner/python/main/build/lib.linux-x86_64-3.11-pydebug', '/home/vstinner/.local/lib/python3.11/site-packages']$ PYTHONDONTADDPATH0=1 ./python -c 'import sys, pprint; pprint.pprint(sys.path)'['/usr/local/lib/python311.zip', '/home/vstinner/python/main/Lib', '/home/vstinner/python/main/build/lib.linux-x86_64-3.11-pydebug', '/home/vstinner/.local/lib/python3.11/site-packages']
ofek reacted with heart emoji

Comment on lines 310 to 312
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
Don't add:data:`sys.path[0] <sys.path>`: don'tprepend thecurrent working
directory (``python -m module``),an empty string (``python -c code``) or
the first command line argument (``python script.py``) to:data:`sys.path`.
Do notprepend theinitial working directory (``python -m module``), the
current workingdirectory (``python -c code``),or the directory of the
executed script (``python script.py``) to:data:`sys.path`.

I think saying "prepend" suffices instead of talking about addingsys.path[0].

With the-m option,sys.path[0] is the full path of the initial working directory. With the-c option and the REPL, the current working directory is prepended tosys.path. It's an implementation detail that it's represented in this case as an empty string. It's not exactly an obscure implementation detail, since most people know thatnormpath('') ->'.', but I think it's better to be explicit about the behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You're emphasizingsys.path[0] again. There's always asys.path[0]. How aboutPYTHONUSESAFEPATH?

Copy link
Member

@merwokmerwokApr 27, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

How does the doc call the value that is prepended? Main script base directory? Could bePYTHONDONTADDMAINDIR orPYTHONNOMAINDIR.

(I wondered about includingIMPORT orPATH but then you get something clunky or you add prepositions and get too long)

@vstinner
Copy link
MemberAuthor

Proposed names so far (on python-dev and this PR), I add spaces for readability:

  • PYTHON DONT ADD PATH0
  • PYTHON DONT ADD PWD
  • PYTHON DONT ADD MAIN DIR
  • PYTHON DONT ADD IMPLICT DIRS
  • PYTHON DONT ADD SCRIPT DIR
  • PYTHON USE SAFE PATH

@vstinner
Copy link
MemberAuthor

vstinner commentedMay 4, 2022
edited
Loading

Proposed long option names (in the issue):

@vstinner
Copy link
MemberAuthor

Perl 5.26 no longer includes the current working directly by default in@INC to fixCVE-2016-1238 security vulnerability:https://metacpan.org/pod/perl5260delta#Removal-of-the-current-directory-(%22.%22)-from-@INC

PERL_USE_UNSAFE_INC env var can be used to opt-in for "unsafe"@INC (add current working directory).

Perl also has a "taint mode" enabled by the Perl-T command line option.

ofek and gpshead reacted with heart emoji

@vstinnervstinner changed the titlebpo-13475: Add -P command line optiongh-57684: Add -P cmdline option and PYTHONSAFEPATH env varMay 4, 2022
@vstinner
Copy link
MemberAuthor

I updated my PR: it now uses thePYTHONSAFEPATH environment variable name.

  • Replacebpo-13475 withgh-57684
  • Renameadd_path0 tosafe_path (opposite meaning)
  • Renamesys.flags.dont_add_path0 tosys.flags.safe_path
  • RenamePYTHONDONTADDPATH0 env var toPYTHONSAFEPATH (shorter, easier to write)
  • PR rebased, I fixed conflicts.
hroncok reacted with heart emoji

@hroncok
Copy link
Contributor

I love PYTHONSAFEPATH!

ofek reacted with heart emoji

@JelleZijlstra
Copy link
Member

I'm not a fan of the "safe path" wording. Things can be "safe" or "unsafe" in many ways and the word doesn't give a clear indication of what the precise behavior is.

Would something like "skip_cwd_in_path" work?

gpshead and CAM-Gerlach reacted with thumbs up emoji

@merwok
Copy link
Member

It wouldn’t, because as detailed before it’s not always the working dir that’s added.

Copy link
Member

@zwarezware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not a fan of the "safe path" wording. Things can be "safe" or "unsafe" in many ways and the word doesn't give a clear indication of what the precise behavior is.

Same reservations here. What about changing the sense of the name, toPYTHONUNSAFEPATH/sys.flags.unsafe_path with a default ofTrue? Not a strong enough feeling to be blocking, though :)

@zware
Copy link
Member

provide an opt-in way "to be safe".

That's the bit that makes me squirm, though :). It makes things less potentially unsafe, but I can well imagine someone coming along in 5 years with some new contrivance that shows that no, in fact, the path is not completely safe withPYTHONSAFEPATH set.

Maybe make itPYTHONSAFERPATH? That wording is harder to argue against, but also hard to tell apart fromPYTHONSAFEPATH.

I fully recognize that this is just bikeshedding, though. Don't hold this up on quibbles about future maybes :)

JelleZijlstra reacted with thumbs up emoji

@vstinner
Copy link
MemberAuthor

Maybe we should consider adding-p option which is the opposite of-P: add the unsafe path.

For example,PYTHONSAFEPATH=1 python -p script.py [...] would ensure that Python still adds the current directory tosys.path to runscript.py. The problem is that when you run a script, you don't know in advance inPYTHONSAFEPATH=1 env var is set or not: it's inherited in the parent process environment or can be set system-wide.

Otherwise,script.py fails onimport foo to loadfoo.py which is in the same directory thanscript.py.

@vstinner
Copy link
MemberAuthor

Maybe we should consider adding -p option which is the opposite of -P: add the unsafe path.

For example,PYTHONSAFEPATH=1 python Tools/freeze/freeze.py fails onimport checkextensions withImportError: it is supposed to loadTools/freeze/checkextensions.py. Thefreeze.py script is incompatible with "safe path".

@merwok
Copy link
Member

These tools are exceptions; they could add their parent directory to sys.path explicitly.

gpshead reacted with thumbs up emoji

@gpshead
Copy link
Member

Maybe we should consider adding-p option which is the opposite of-P: add the unsafe path.

I think that should be left for the future. Lets see if anyone actuallywants it first. If we do go the route of aiming for a default flip in the future it might make sense.

@ofek
Copy link
Contributor

ofek commentedMay 5, 2022

I'm still thinking about changing the default to be safe by default, but "later" and it might require a PEP.

I'd be very much in favor of such a change 🙂

@vstinnervstinner merged commitada8b6d intopython:mainMay 5, 2022
@vstinnervstinner deleted the add_path0 branchMay 5, 2022 23:34
@vstinner
Copy link
MemberAuthor

That's the bit that makes me squirm, though :). It makes things less potentially unsafe, but I can well imagine someone coming along in 5 years with some new contrivance that shows that no, in fact, the path is not completely safe with PYTHONSAFEPATH set.

PYTHONSAFEPATH=1 is not safe at all :-) It does exactly one thing: not preprend a path to sys.path[0] which is "potentially" unsafe. It's just that it's really hard to select a short environment name which either summarizes well what the variable does, or explains in length what it does. For example, thePYTHONDONTADDIMPLICTDIRS name was proposed, but ... it's quite long to type :-(

@vstinner
Copy link
MemberAuthor

@gpshead:

I think that should be left for the future. Lets see if anyone actually wants it first. If we do go the route of aiming for a default flip in the future it might make sense.

Flipping the default can now be done by settingPYTHONSAFEPATH=1 system wide (or in the current shell).

The problem is that right now, ifPYTHONSAFEPATH=1 is set, it's no longer possible to run programs which rely on the Python 3.10 behavior to load modules in the current directory (as Pythonfreeze.py tool), without having to modify the source code of these programs.

IMO command line options must have the priority over environment variables. Python does not provide command line options to disable the effect of all Python environment variables. For example,PYTHONUNBUFFERED=1 cannot be "undone" by a command line option. But the effect of this variable is limited: I'm not aware of any program broken by it.

The Python UTF-8 Mode is way more likely to cause compatibility issues and so-X utf8=0 can disable iton the command line: ignorePYTHONUTF8=1 env var.

We don't have to add-p, but it makesPYTHONSAFEPATH=1 harder to use. The current workaround is to use-E to ignore environment variables, or-I (isolated mode) but this option has more effects (like not adding user site directories).

@vstinner
Copy link
MemberAuthor

I merged my PR, thanks@gpshead for the review.

I'm not fully comfortable of merging this PR in a rush (before the Python 3.11 feature freeze), but IMO the scope of the feature is limited and well defined. It's different than other options discussed previously, especially the idea of changing thedefault behavior to "safe" (not prepend a path to sys.path).

The idea of putting the feature is beta1 is to give users the ability to play with it before Python 3.11 final release, to get feedback, adjust issues, and maybe consider reverting the change if it causes too many issues.

gpshead reacted with thumbs up emoji

@vstinner
Copy link
MemberAuthor

@ofek:

I'd be very much in favor of such a change slightly_smiling_face

As written previously, you can now simply set PYTHONSAFEPATH=1 env var system wide, and Python 3.11 sys.path will be "safer" by default ;-) On Linux, just put it in/etc/environment.

@vstinner
Copy link
MemberAuthor

Follow-up change:329afe7 "Fix tests failing with the PYTHONSAFEPATH=1 env var".

@vstinner
Copy link
MemberAuthor

I created#92361 to add a new-p option which can be used to opt-in for Python 3.10 sys.path behavior whenPYTHONSAFEPATH=1 is set globally. It can also be used with the-I isolated mode to prepend the unsafe path tosys.path.

JelleZijlstra added a commit to python/typeshed that referenced this pull requestMay 7, 2022
hauntsaninja pushed a commit to python/typeshed that referenced this pull requestMay 7, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@merwokmerwokmerwok left review comments

@eryksuneryksuneryksun left review comments

@zwarezwarezware left review comments

@gpsheadgpsheadgpshead approved these changes

+1 more reviewer

@hroncokhroncokhroncok left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

11 participants

@vstinner@CAM-Gerlach@hroncok@ofek@JelleZijlstra@merwok@bedevere-bot@gpshead@zware@eryksun@the-knights-who-say-ni

[8]ページ先頭

©2009-2025 Movatter.jp