[issue40542] path environment variable not created correctly

Eryk Sun report at bugs.python.org
Wed May 6 23:32:43 EDT 2020


Eryk Sun <eryksun at gmail.com> added the comment:

Prepending directories ahead of system directories in PATH affects programs that implement their own search, which includes shells such as cmd.exe that do so in order to support PATHEXT efficiently. That said, note that temporarily prepending to PATH in a particular environment is common. Consider an activated virtual environment or a developer command prompt.

Usually programs defer to a Windows API function when searching for a file. The API uses search paths from the runtime library functions RtlGetSearchPath, RtlGetExePath, and LdrGetDllPath.

With WINAPI SearchPathW, the default search path is from RtlGetSearchPath (undocumented). It includes the following directories in non-safe search mode (the default mode):

  1. %__APPDIR__%
  2. %__CD__%
  3. %SystemRoot%\System32
  4. %SystemRoot%\System
  5. %SystemRoot%
  6. %PATH%

and the following adjusted order in safe search mode:

  1. %__APPDIR__%
  3. %SystemRoot%\System32
  4. %SystemRoot%\System
  5. %SystemRoot%
  2. %__CD__%
  6. %PATH%

Safe search mode can be set via WINAPI SetSearchPathMode(BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE).

With WINAPI CreateProcessW, the search path is from RtlGetExePath (undocumented). This is similar to the result from RtlGetSearchPath, except, instead of supporting a safe search mode, RtlGetExePath allows excluding the current directory (%__CD__%) from the search path by setting the environment variable NoDefaultCurrentDirectoryInExePath.

With WINAPI LoadLibrary[Ex]W, the system uses the search path from LdrGetDllPath (undocumented). The default DLL search path at startup under normal circumstances is the same as the safe-mode result from RtlGetSearchPath -- except for special casing of known system DLLs and API sets.

----------
nosy: +eryksun

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40542>
_______________________________________


More information about the Python-bugs-list mailing list