Skip to content

Commit 8baca8e

Browse files
joaocgreisBridgeAR
authored andcommitted
build,win: find Python in paths with spaces
When looking for Python in the registry, as specified in PEP514, this was not able to handle installations in a path with spaces, like Program Files. This ensures the whole path is used, fixing the issue. PR-URL: #29236 Reviewed-By: Christian Clauss <cclauss@me.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 8877b5f commit 8baca8e

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

tools/msvs/find_python.cmd

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
@IF NOT DEFINED DEBUG_HELPER @ECHO OFF
2+
23
echo Looking for Python 2.x
3-
SETLOCAL
4+
setlocal enabledelayedexpansion
5+
46
:: If python.exe is in %Path%, just validate
57
FOR /F "delims=" %%a IN ('where python.exe 2^> NUL') DO (
68
SET need_path=0
@@ -11,32 +13,36 @@ FOR /F "delims=" %%a IN ('where python.exe 2^> NUL') DO (
1113
:: Query the 3 locations mentioned in PEP 514 for a python2 InstallPath
1214
FOR %%K IN ( "HKCU\Software", "HKLM\SOFTWARE", "HKLM\Software\Wow6432Node") DO (
1315
SET need_path=1
14-
CALL :find-main-branch %%K
16+
CALL :find-versions-v2 %%K
1517
:: If validate returns 0 just jump to the end
1618
IF NOT ERRORLEVEL 1 GOTO :validate
1719
)
20+
1821
goto :no-python
1922

20-
:: Helper subroutine to handle quotes in %1
21-
:find-main-branch
22-
SET main_key="%~1\Python\PythonCore"
23-
REG QUERY %main_key% /s 2> NUL | findstr "2." | findstr InstallPath > NUL 2> NUL
24-
IF NOT ERRORLEVEL 1 CALL :find-key %main_key%
25-
EXIT /B
26-
27-
:: Query registry sub-tree for InstallPath
28-
:find-key
29-
FOR /F "delims=" %%a IN ('REG QUERY %1 /s 2^> NUL ^| findstr "2." ^| findstr InstallPath') DO IF NOT ERRORLEVEL 1 CALL :find-path %%a
30-
EXIT /B
31-
32-
:: Parse the value of %1 as the path for python.exe
33-
:find-path
34-
FOR /F "tokens=3*" %%a IN ('REG QUERY %1 /ve') DO (
35-
SET pt=%%a
36-
IF NOT ERRORLEVEL 1 SET p=%pt%
37-
EXIT /B 0
23+
24+
:: Find Python 2 installations in a registry location
25+
:find-versions-v2
26+
for /f "delims=" %%a in ('reg query "%~1\Python\PythonCore" /f * /k 2^> nul ^| findstr /r ^^HK ^| findstr "\\2\."') do (
27+
call :read-installpath %%a
28+
if not errorlevel 1 exit /b 0
3829
)
39-
EXIT /B 1
30+
exit /b 1
31+
32+
:: Read the InstallPath of a given Environment Key to %p%
33+
:: https://www.python.org/dev/peps/pep-0514/#installpath
34+
:read-installpath
35+
:: %%a will receive token 3
36+
:: %%b will receive *, corresponding to token 4 and all after
37+
for /f "skip=2 tokens=3*" %%a in ('reg query "%1\InstallPath" /ve /t REG_SZ 2^> nul') do (
38+
set "head=%%a"
39+
set "tail=%%b"
40+
set "p=!head!"
41+
if not "!tail!"=="" set "p=!head! !tail!"
42+
exit /b 0
43+
)
44+
exit /b 1
45+
4046

4147
:: Check if %p% holds a path to a real python2 executable
4248
:validate

0 commit comments

Comments
 (0)