classification
Title: activate for venv containing apostrophe doesn't work in powershell
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: cheryl.sabella, eryksun, vinay.sajip, xtreak
Priority: normal Keywords:

Created on 2019-01-05 21:34 by cheryl.sabella, last changed 2019-01-07 00:09 by eryksun.

Messages (3)
msg333075 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python triager) Date: 2019-01-05 21:34
On Windows 10, when I try to activate a venv in powershell where the name contains an apostrophe, I get the following error ("don't" is the name of the venv):

PS N:\projects\cpython\don't\Scripts> .\activate.ps1
At N:\projects\cpython\don't\Scripts\Activate.ps1:42 char:28
+     function global:prompt {
+                            ~
Missing closing '}' in statement block or type definition.
At N:\projects\cpython\don't\Scripts\Activate.ps1:37 char:40
+ if (! $env:VIRTUAL_ENV_DISABLE_PROMPT) {
+                                        ~
Missing closing '}' in statement block or type definition.
At N:\projects\cpython\don't\Scripts\Activate.ps1:43 char:61
+         Write-Host -NoNewline -ForegroundColor Green '(don't) '
+                                                             ~
Unexpected token ')' in expression or statement.
At N:\projects\cpython\don't\Scripts\Activate.ps1:43 char:63
+         Write-Host -NoNewline -ForegroundColor Green '(don't) '
+                                                               ~
The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

This works OK in Command Prompt.
msg333116 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2019-01-06 18:17
From the error I think the quotes are not properly escaped while doing text replacement in venv activate file template at [0] while it's generated? On linux/Mac double quotes are used and hence the error is not triggered with single quote in virtualenv name. Using double quote in virtualenv name triggers the parse error.

$  python3.7 -m venv py37\"-bpo-35667-venv
$  source py37\"-bpo-35667-venv/bin/activate
py37"-bpo-35667-venv/bin/activate:57: parse error near `then'

Looking into the py37\"-bpo-35667-venv/bin/activate script there is below line where the double quote is unbalanced : 

    if [ "x(py37"-bpo-35667-venv) " != x ] ; then
	PS1="(py37"-bpo-35667-venv) ${PS1:-}"
    else

I tried escaping the quotes with backslash at [0] but I get a similar error. I guess it's similar case in Powershell as I can see from Activate.ps1 but I haven't tested it. I am not sure about the difference in semantics between powershell and command prompt with respect to quoting since I don't have access to Windows.

py37\'-bpo-35667-venv/bin/Activate.ps1 file with unbalanced quote

    function global:prompt {
        Write-Host -NoNewline -ForegroundColor Green '(py37'-bpo-35667-venv) '
        _OLD_VIRTUAL_PROMPT
    }


[0] https://github.com/python/cpython/blob/a5b76167dedf4d15211a216c3ca7b98e3cec33b8/Lib/venv/__init__.py#L280
msg333125 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-01-07 00:09
> I am not sure about the difference in semantics between powershell and 
> command prompt with respect to quoting since

CMD only uses single quotes in `for /f` loops, in which it's either a command or a literal string (w/ the "usebackq" option). This is not a problem in activate.bat. 

Double quotes in file names generally shouldn't be a problem in Windows. As far as I know, only the named-pipe file system allows them in pipe-file names (e.g. r'\\?\pipe\"pipename"'), and for regular files they're allowed in stream names (e.g. 'filename:"streamname"'). Otherwise, double quote should be reserved in file names as the DOS_DOT wildcard character.
History
Date User Action Args
2019-01-07 00:09:14eryksunsetnosy: + eryksun
messages: + msg333125
2019-01-06 18:17:40xtreaksetnosy: + vinay.sajip, xtreak
messages: + msg333116
2019-01-05 21:34:29cheryl.sabellacreate