Skip to content

Commit a4f97eb

Browse files
committed
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 09c46b4 commit a4f97eb

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
@@ -827,10 +827,10 @@ $(BUILDPYTHON): Programs/python.o $(LINK_PYTHON_DEPS) python_exe.o
827827
$(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -municode -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS) python_exe.o
828828

829829
$(BUILDVENVLAUNCHER): $(BUILDPYTHON) venvlauncher.o $(srcdir)/PC/launcher.c
830-
$(LINKCC) -D_CONSOLE -DVENV_REDIRECT $(PY_STDMODULE_CFLAGS) -municode -static -static-libgcc -static-libstdc++ venvlauncher.o $(srcdir)/PC/launcher.c -o $@ -lversion
830+
$(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
831831

832832
$(BUILDVENVWLAUNCHER): $(BUILDPYTHONW) venvwlauncher.o $(srcdir)/PC/launcher.c
833-
$(LINKCC) -D_WINDOWS -DVENV_REDIRECT $(PY_STDMODULE_CFLAGS) -mwindows -municode -static -static-libgcc -static-libstdc++ venvwlauncher.o $(srcdir)/PC/launcher.c -o $@ -lversion
833+
$(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
834834

835835
platform: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt
836836
$(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
@@ -1930,7 +1930,8 @@ process(int argc, wchar_t ** argv)
19301930
if (!cch) {
19311931
error(0, L"Cannot determine memory for home path");
19321932
}
1933-
cch += (DWORD)wcslen(PYTHON_EXECUTABLE) + 4; /* include sep, null and quotes */
1933+
cch += (DWORD)max(wcslen(PYTHON_EXECUTABLE_WITH_VERSION),
1934+
wcslen(PYTHON_EXECUTABLE)) + 4; /* include sep, null and quotes */
19341935
executable = (wchar_t *)malloc(cch * sizeof(wchar_t));
19351936
if (executable == NULL) {
19361937
error(RC_NO_MEMORY, L"A memory allocation failed");
@@ -1948,13 +1949,22 @@ process(int argc, wchar_t ** argv)
19481949
executable[cch_actual++] = L'\\';
19491950
executable[cch_actual] = L'\0';
19501951
}
1951-
if (wcscat_s(&executable[1], cch - 1, PYTHON_EXECUTABLE)) {
1952+
if (wcscat_s(&executable[1], cch - 1, PYTHON_EXECUTABLE_WITH_VERSION)) {
19521953
error(RC_BAD_VENV_CFG, L"Cannot create executable path from '%ls'",
19531954
venv_cfg_path);
19541955
}
19551956
/* there's no trailing quote, so we only have to skip one character for the test */
1957+
// Check if the versioned executable (PYTHON_EXECUTABLE_WITH_VERSION) exists first
19561958
if (GetFileAttributesW(&executable[1]) == INVALID_FILE_ATTRIBUTES) {
1957-
error(RC_NO_PYTHON, L"No Python at '%ls'", executable);
1959+
// If not found, try PYTHON_EXECUTABLE
1960+
executable[cch_actual] = L'\0'; // Reset the path
1961+
if (wcscat_s(&executable[1], cch - 1, PYTHON_EXECUTABLE)) {
1962+
error(RC_BAD_VENV_CFG, L"Cannot create executable path from '%ls'",
1963+
venv_cfg_path);
1964+
}
1965+
if (GetFileAttributesW(&executable[1]) == INVALID_FILE_ATTRIBUTES) {
1966+
error(RC_NO_PYTHON, L"No Python at '%ls'", executable);
1967+
}
19581968
}
19591969
/* now append the final quote */
19601970
wcscat_s(executable, cch, L"\"");

0 commit comments

Comments
 (0)