@@ -211,6 +211,28 @@ def search_up(prefix, *landmarks, test=isfile):
211211 prefix = dirname (prefix )
212212
213213
214+ # The traditional Windows path length limit, not counting the terminating NUL.
215+ MAX_PATH = 260
216+
217+
218+ def strip_extended_length_prefix (path ):
219+ """
220+ Remove the leading "\\ \\ ?\\ " of a Windows extended-length path, and
221+ rewrite the "\\ \\ ?\\ UNC\\ " form of a UNC path back to a plain "\\ \\ " one.
222+ Windows does not resolve relative components (such as ".." or "/") in an
223+ extended-length path, so keeping the prefix in a path that we later join
224+ onto is liable to break consumers. A path that is still too long to be
225+ used unprefixed keeps its prefix, as it has no other usable form.
226+ """
227+ if path .startswith ('\\ \\ ?\\ UNC\\ ' ):
228+ stripped = '\\ \\ ' + path [8 :]
229+ elif path .startswith ('\\ \\ ?\\ ' ):
230+ stripped = path [4 :]
231+ else :
232+ return path
233+ return stripped if len (stripped ) < MAX_PATH else path
234+
235+
214236# ******************************************************************************
215237# READ VARIABLES FROM config
216238# ******************************************************************************
@@ -319,6 +341,13 @@ def search_up(prefix, *landmarks, test=isfile):
319341 real_executable = base_executable
320342 #real_executable_dir = dirname(real_executable)
321343 executable = ENV_PYTHONEXECUTABLE or ENV___PYVENV_LAUNCHER__
344+ if os_name == 'nt' :
345+ # QUIRK: On Windows the venv launcher reports its own path through
346+ # GetModuleFileNameW, which hands back a "\\?\" extended-length path
347+ # whenever that is how the interpreter was invoked. Drop that prefix
348+ # again, so the venv prefix and sys.executable stay usable by tools
349+ # that join relative components onto them (gh-157917).
350+ executable = strip_extended_length_prefix (executable )
322351 executable_dir = dirname (executable )
323352
324353
0 commit comments