Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Windows: resolve python.exe from install_python folder when chaining venv setup

LNPANDYfab9f8f712ca
diff --git a/frontend-qt/nspb_qt/model.py b/frontend-qt/nspb_qt/model.py
index 650ff569..c69ae1cf 100644
--- a/frontend-qt/nspb_qt/model.py
+++ b/frontend-qt/nspb_qt/model.py
@@ -977,8 +977,9 @@ class Model(QObject):
log=lambda s: self.log(str(s) + '\n'),
progress=progress_logger(self.t('dep.phase.download')))
self.log(self.t('dep.python.done', str(path)) + '\n')
- self.python_path = str(path)
- ok = self._make_venv(str(path))
+ py_exe = Path(path) / 'python.exe' if Path(path).is_dir() else Path(path)
+ self.python_path = str(py_exe)
+ ok = self._make_venv(str(py_exe))
if not ok:
detail = 'venv / package install failed'
self.log(self.t('dep.install.fail', detail) + '\n')
diff --git a/frontend-qt/nspb_qt/platform_util.py b/frontend-qt/nspb_qt/platform_util.py
index b84326ad..1e09c17c 100644
--- a/frontend-qt/nspb_qt/platform_util.py
+++ b/frontend-qt/nspb_qt/platform_util.py
@@ -110,6 +110,11 @@ def python_candidates(extra_candidates: list[str] | None = None) -> list[str]:
"""
cands: list[str] = [venv_python()] + [c for c in (extra_candidates or []) if c]
if IS_WIN:
+ local_app = Path(os.environ.get('LOCALAPPDATA', str(Path.home() / 'AppData' / 'Local')))
+ tools_root = local_app / 'NS Port Builder' / 'tools'
+ if tools_root.is_dir():
+ for d in sorted(tools_root.glob('python-*'), reverse=True):
+ cands.append(str(d / 'python.exe'))
cands += [
os.path.expandvars(r'%LOCALAPPDATA%\Programs\Python\Python312\python.exe'),
os.path.expandvars(r'%LOCALAPPDATA%\Programs\Python\Python311\python.exe'),
@@ -178,7 +183,13 @@ def venv_base(preferred: str) -> str | None:
`-m venv`, and the Python row never recovered from it. So a base that does not run at the
required version falls back to the newest candidate that does.
"""
- if Path(preferred).is_file() and os.access(preferred, os.X_OK):
+ p = Path(preferred) if preferred else Path()
+ if preferred and p.is_dir():
+ sub = p / ('python.exe' if IS_WIN else 'bin/python3')
+ if sub.is_file():
+ p = sub
+ preferred = str(sub)
+ if preferred and p.is_file() and os.access(preferred, os.X_OK):
v = _python_version(preferred)
if v and _ver_tuple(v) >= MIN_PYTHON:
return preferred
diff --git a/frontend-qt/tests/test_win.py b/frontend-qt/tests/test_win.py
index 1480d424..3e66f710 100644
--- a/frontend-qt/tests/test_win.py
+++ b/frontend-qt/tests/test_win.py
@@ -2048,12 +2048,32 @@ class VenvWheelsTest(unittest.TestCase):
self.assertEqual(bundled_wheels(), wheels)
def test_win_python_install_chains_make_venv_immediately(self) -> None:
- """Installing Python on a fresh PC must immediately build the venv and install packages in the same pass."""
+ """Installing Python on a fresh PC must pass `python.exe` (not the install folder) to _make_venv / venv_base."""
+ from nspb_qt import platform_util as pu
src = (QT / 'nspb_qt' / 'model.py').read_text(encoding='utf-8')
branch = src[src.index("elif what == 'python':"):src.index("elif what == 'llvm':")]
self.assertIn('install_python(', branch)
- self.assertIn('self._make_venv(str(path))', branch,
- 'the Python installer must chain _make_venv immediately so packages are not skipped')
+ self.assertIn("Path(path) / 'python.exe'", branch)
+ self.assertIn('self._make_venv(str(py_exe))', branch)
+
+ # End-to-end check: install_python() returns a directory (`.../tools/python-3.12.10`),
+ # and on a fresh PC with no system Python in python_candidates(), venv_base must resolve
+ # `python.exe` inside that directory and python_candidates() must discover tools/python-*/python.exe.
+ with tempfile.TemporaryDirectory() as td:
+ tools_dir = Path(td) / 'NS Port Builder' / 'tools'
+ py_dir = tools_dir / 'python-3.12.10'
+ py_dir.mkdir(parents=True)
+ py_exe = py_dir / 'python.exe'
+ py_exe.write_bytes(b'MZ')
+ py_exe.chmod(0o755)
+ with mock.patch.object(pu, 'IS_WIN', True), \
+ mock.patch.object(pu, '_python_version', return_value='3.12.10'), \
+ mock.patch.object(pu, 'python_candidates', return_value=[]):
+ # Even when a caller passes the folder returned by install_python(), venv_base resolves python.exe
+ self.assertEqual(pu.venv_base(str(py_dir)), str(py_exe))
+ with mock.patch.object(pu, 'IS_WIN', True), \
+ mock.patch.dict(os.environ, {'LOCALAPPDATA': td}, clear=False):
+ self.assertIn(str(py_exe), pu.python_candidates())
class MetalPickFlowShapeTest(unittest.TestCase):