bpo-35872 and bpo-35873: Clears __PYVENV_LAUNCHER__ variable (GH-11745) · python/cpython@44467e8
@@ -18,6 +18,19 @@
1818WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))
1919WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")
202021+22+def _path_eq(p1, p2):
23+return p1 == p2 or os.path.normcase(p1) == os.path.normcase(p2)
24+25+WINENV = (hasattr(sys, '_base_executable') and
26+not _path_eq(sys.executable, sys._base_executable))
27+28+29+def _close_handles(*handles):
30+for handle in handles:
31+_winapi.CloseHandle(handle)
32+33+2134#
2235# We define a Popen class similar to the one from subprocess, but
2336# whose constructor takes a process object as its argument.
@@ -40,12 +53,23 @@ def __init__(self, process_obj):
4053pipe_handle=rhandle)
4154cmd = ' '.join('"%s"' % x for x in cmd)
425556+python_exe = spawn.get_executable()
57+58+# bpo-35797: When running in a venv, we bypass the redirect
59+# executor and launch our base Python.
60+if WINENV and _path_eq(python_exe, sys.executable):
61+python_exe = sys._base_executable
62+env = os.environ.copy()
63+env["__PYVENV_LAUNCHER__"] = sys.executable
64+else:
65+env = None
66+4367with open(wfd, 'wb', closefd=True) as to_child:
4468# start process
4569try:
4670hp, ht, pid, tid = _winapi.CreateProcess(
47-spawn.get_executable(), cmd,
48-None, None, False, 0, None, None, None)
71+python_exe, cmd,
72+env, None, False, 0, None, None, None)
4973_winapi.CloseHandle(ht)
5074except:
5175_winapi.CloseHandle(rhandle)