-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-37354: Make Powershell Activate.ps1 script static to allow for signing #14967
Conversation
- Remove use of replacement text in the script - Make use of the pyvenv.cfg file for prompt value. - Add parameters to allow more flexibility - Make use of the current path, and assumptions about where �env puts things, to compensate - Make the script a bit more 'idiomatic' Powershell - Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
- with prompt - with usage in Linux on PowerShell core
Note, also fixes bpo-35667. |
Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" | ||
if ($pyvenvCfg -and $pyvenvCfg['prompt']) { | ||
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" | ||
$Prompt = $pyvenvCfg['prompt']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the line that gave me the most nightmares when thinking about how to do this, because the prompt value in the config file is quoted and escaped (basically, it's repr(prompt)
), so we need to do more processing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @zooba!
Agreed that the escaped character problem is a bit tricky in the context of Powershell... \n
is not really available in Powershell, for instance, but could be replaced with the back-tick-quote `. Finding and replacing all types of escaped characters (newlines, tabs, colors?) might be a bit of a deep dark hole (there be ANSI-escaped coloured dragons there)!
Would it be appropriate to add handling of escaped characters in a different issue/PR? I don't expect that the prior version of this Activate.ps1 script handled this problem particularly well either (a brief test with the latest 3.8 beta shows a few hiccups right away).
Of course, I would be most happy to tackle that problem next!
What sample input would show the shortcoming of this implementation, so that I can write some unit tests and fix it correctly?
Just from thinking about it briefly I can think up input that contains:
- newlines (
\n
,\r\n
) - tabs (
\t
) - colours? (
\u001b[31m
, ... ,\u001b[0m
)
Ideally I'd love to show something that would have the class of input we need to be concerned about working with, and have it handled at least as well as the prior implementation handled it.
A specific scenario that is trivial to setup and play with, for example the newline escaped character \n
in the prompt:
PS /home/d3r3kk/dev/venvtest> ../cpython/python -m venv --prompt "\nd3r3kk\n" .newline_prompt
PS /home/d3r3kk/dev/venvtest> cat ./.newline_prompt/pyvenv.cfg
home = /home/d3r3kk/dev/cpython
include-system-site-packages = false
version = 3.9.0
prompt = '\\nd3r3kk\\n'
PS /home/d3r3kk/dev/venvtest> ./.newline_prompt/bin/Activate.ps1
(\\nd3r3kk\\n) PS /home/d3r3kk/dev/venvtest> deactivate
PS /home/d3r3kk/dev/venvtest> exit
...and then in bash...
d3r3kk@deb:~/dev/venvtest$ . .newline_prompt/bin/activate
(
d3r3kk
) d3r3kk@deb:~/dev/venvtest$ deactivate
d3r3kk@deb:~/dev/venvtest$
...as we can see, the \\n
being stored in the pyvenv.cfg
is written to the prompt verbatim in Powershell with this change (and is also in the latest 3.8 beta release).
Note: it is possible in Powershell to replace some escaped character's \\
with a ` and it will work. (But definitely not colours, those are weird special creatures!).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a PowerShell tyro, so I can't comment on some of the finer points. The handling of escapes seems orthogonal to the making of the script to be entirely static, but I concur with the comment on the issue made by @zooba : "get it out in 3.8.0b4 and see how it fares".
Thanks for the review @vsajip! (and for introducing me to the term tyro). I've updated the PR with your suggestions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing more to add to my earlier review.
@zooba anything else for me to address? |
…gning (pythonGH-14967) - Remove use of replacement text in the script - Make use of the pyvenv.cfg file for prompt value. - Add parameters to allow more flexibility - Make use of the current path, and assumptions about where env puts things, to compensate - Make the script a bit more 'idiomatic' Powershell - Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now (cherry picked from commit 732775d) Co-authored-by: Derek Keeler <d3r3kk@users.noreply.github.com>
GH-15233 is a backport of this pull request to the 3.8 branch. |
…for signing (GH-14967) - Remove use of replacement text in the script - Make use of the pyvenv.cfg file for prompt value. - Add parameters to allow more flexibility - Make use of the current path, and assumptions about where env puts things, to compensate - Make the script a bit more 'idiomatic' Powershell - Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now (cherry picked from commit 732775d) Co-authored-by: Derek Keeler <d3r3kk@users.noreply.github.com>
…gning (pythonGH-14967) - Remove use of replacement text in the script - Make use of the pyvenv.cfg file for prompt value. - Add parameters to allow more flexibility - Make use of the current path, and assumptions about where env puts things, to compensate - Make the script a bit more 'idiomatic' Powershell - Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
…gning (pythonGH-14967) - Remove use of replacement text in the script - Make use of the pyvenv.cfg file for prompt value. - Add parameters to allow more flexibility - Make use of the current path, and assumptions about where env puts things, to compensate - Make the script a bit more 'idiomatic' Powershell - Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
…gning (pythonGH-14967) - Remove use of replacement text in the script - Make use of the pyvenv.cfg file for prompt value. - Add parameters to allow more flexibility - Make use of the current path, and assumptions about where env puts things, to compensate - Make the script a bit more 'idiomatic' Powershell - Add script documentation (Get-Help .\.venv\Scripts\Activate.ps1 shows PS help page now
Make Powershell Activation.ps1 script static and therefore sign-able. The script now determines the interpreter path based on where the Activate script is. The prompt is determined based on the directory name of the parent directory the script is located in, or can be overridden via an argument to the Activate script, or from within the
pyvenv.cfg
file.Tested On:
https://bugs.python.org/issue37354