Skip to content

Commit a21d67c

Browse files
lazkaAlexpux
authored andcommitted
venvlauncher: try looking for the versioned .exe first and then fall back
By default venvlauncher only looks for python.exe in the python "home", which fails in the MSYS2 case where there could be multiple python versions in the same prefix i.e. python.exe could be 3.12 while the venv was created with python3.13.exe. Upstream CPython doesn't have this problem since they never have multiple Python versions in the same prefix. On the other hand the Python test suite (test_sysconfig specifically) creates a venv for the uninstalled Python build dir, where only python.exe exists. To cover both cases we first try to look for the python3.XY.exe and then fall back to python.exe.
1 parent 8f26e19 commit a21d67c

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Makefile.pre.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,10 +918,10 @@ $(BUILDPYTHON): Programs/python.o $(LINK_PYTHON_DEPS) python_exe.o
918918
$(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -municode -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS) python_exe.o
919919

920920
$(BUILDVENVLAUNCHER): $(BUILDPYTHON) venvlauncher.o $(srcdir)/PC/launcher.c
921-
$(LINKCC) -D_CONSOLE -DVENV_REDIRECT $(PY_STDMODULE_CFLAGS) -municode -static -static-libgcc -static-libstdc++ venvlauncher.o $(srcdir)/PC/launcher.c -o $@ -lversion
921+
$(LINKCC) -D_CONSOLE -DVENV_REDIRECT -DPYTHON_EXECUTABLE_WITH_VERSION="L\"python$(LDVERSION)$(EXE)\"" $(PY_STDMODULE_CFLAGS) -municode -static -static-libgcc -static-libstdc++ venvlauncher.o $(srcdir)/PC/launcher.c -o $@ -lversion
922922

923923
$(BUILDVENVWLAUNCHER): $(BUILDPYTHONW) venvwlauncher.o $(srcdir)/PC/launcher.c
924-
$(LINKCC) -D_WINDOWS -DVENV_REDIRECT $(PY_STDMODULE_CFLAGS) -mwindows -municode -static -static-libgcc -static-libstdc++ venvwlauncher.o $(srcdir)/PC/launcher.c -o $@ -lversion
924+
$(LINKCC) -D_WINDOWS -DVENV_REDIRECT -DPYTHON_EXECUTABLE_WITH_VERSION="L\"pythonw$(LDVERSION)$(EXE)\"" $(PY_STDMODULE_CFLAGS) -mwindows -municode -static -static-libgcc -static-libstdc++ venvwlauncher.o $(srcdir)/PC/launcher.c -o $@ -lversion
925925

926926
platform: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt
927927
$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform

PC/launcher.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,7 +1931,8 @@ process(int argc, wchar_t ** argv)
19311931
if (!cch) {
19321932
error(0, L"Cannot determine memory for home path");
19331933
}
1934-
cch += (DWORD)wcslen(PYTHON_EXECUTABLE) + 4; /* include sep, null and quotes */
1934+
cch += (DWORD)max(wcslen(PYTHON_EXECUTABLE_WITH_VERSION),
1935+
wcslen(PYTHON_EXECUTABLE)) + 4; /* include sep, null and quotes */
19351936
executable = (wchar_t *)malloc(cch * sizeof(wchar_t));
19361937
if (executable == NULL) {
19371938
error(RC_NO_MEMORY, L"A memory allocation failed");
@@ -1949,13 +1950,22 @@ process(int argc, wchar_t ** argv)
19491950
executable[cch_actual++] = L'\\';
19501951
executable[cch_actual] = L'\0';
19511952
}
1952-
if (wcscat_s(&executable[1], cch - 1, PYTHON_EXECUTABLE)) {
1953+
if (wcscat_s(&executable[1], cch - 1, PYTHON_EXECUTABLE_WITH_VERSION)) {
19531954
error(RC_BAD_VENV_CFG, L"Cannot create executable path from '%ls'",
19541955
venv_cfg_path);
19551956
}
19561957
/* there's no trailing quote, so we only have to skip one character for the test */
1958+
// Check if the versioned executable (PYTHON_EXECUTABLE_WITH_VERSION) exists first
19571959
if (GetFileAttributesW(&executable[1]) == INVALID_FILE_ATTRIBUTES) {
1958-
error(RC_NO_PYTHON, L"No Python at '%ls'", executable);
1960+
// If not found, try PYTHON_EXECUTABLE
1961+
executable[cch_actual] = L'\0'; // Reset the path
1962+
if (wcscat_s(&executable[1], cch - 1, PYTHON_EXECUTABLE)) {
1963+
error(RC_BAD_VENV_CFG, L"Cannot create executable path from '%ls'",
1964+
venv_cfg_path);
1965+
}
1966+
if (GetFileAttributesW(&executable[1]) == INVALID_FILE_ATTRIBUTES) {
1967+
error(RC_NO_PYTHON, L"No Python at '%ls'", executable);
1968+
}
19591969
}
19601970
/* now append the final quote */
19611971
wcscat_s(executable, cch, L"\"");

0 commit comments

Comments
 (0)