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

Original file line numberDiff line numberDiff line change

@@ -19,6 +19,13 @@

1919

WINSERVICE = sys.executable.lower().endswith("pythonservice.exe")

2020
2121
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+
2229

def _close_handles(*handles):

2330

for handle in handles:

2431

_winapi.CloseHandle(handle)

@@ -50,12 +57,23 @@ def __init__(self, process_obj):

5057

pipe_handle=rhandle)

5158

cmd = ' '.join('"%s"' % x for x in cmd)

5259
60+

python_exe = spawn.get_executable()

61+
62+

# bpo-35797: When running in a venv, we bypass the redirect

63+

# executor and launch our base Python.

64+

if WINENV and _path_eq(python_exe, sys.executable):

65+

python_exe = sys._base_executable

66+

env = os.environ.copy()

67+

env["__PYVENV_LAUNCHER__"] = sys.executable

68+

else:

69+

env = None

70+
5371

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

5472

# start process

5573

try:

5674

hp, ht, pid, tid = _winapi.CreateProcess(

57-

spawn.get_executable(), cmd,

58-

None, None, False, 0, None, None, None)

75+

python_exe, cmd,

76+

env, None, False, 0, None, None, None)

5977

_winapi.CloseHandle(ht)

6078

except:

6179

_winapi.CloseHandle(rhandle)