bpo-35872 and bpo-35873: Clears __PYVENV_LAUNCHER__ variable (GH-11745) · python/cpython@44467e8

@@ -18,6 +18,19 @@

1818

WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))

1919

WINSERVICE = 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):

4053

pipe_handle=rhandle)

4154

cmd = ' '.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+4367

with open(wfd, 'wb', closefd=True) as to_child:

4468

# start process

4569

try:

4670

hp, 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)

5074

except:

5175

_winapi.CloseHandle(rhandle)