Skip to content

Commit

Permalink
LoadLibraryExW: make sure to only use backslashes for paths
Browse files Browse the repository at this point in the history
It seems like in case the path passed to it is absolute, but contains
forward slashes then LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR does not work
and DLLs in the same directory as the extension are not considered.

This occurs in our fork because in MSYS2-mode the extension loader will
normalize to forward slashes before.

Normalize everything to backslashes again before passing it to LoadLibraryExW.

Fixes #151
  • Loading branch information
lazka committed Aug 27, 2023
1 parent a6cef77 commit e8971f6
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Python/dynload_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
_Py_CheckPython3();
#endif

// So we can adjust the separators in the path below
#define USE_UNICODE_WCHAR_CACHE 0

#if USE_UNICODE_WCHAR_CACHE
const wchar_t *wpathname = _PyUnicode_AsUnicode(pathname);
#else /* USE_UNICODE_WCHAR_CACHE */
Expand All @@ -258,6 +261,15 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
if (wpathname == NULL)
return NULL;

// LoadLibraryExW only considers paths using backslashes as "fully qualified",
// and for example LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR doesn't work with forward slashes.
// https://github.com/msys2-contrib/cpython-mingw/issues/151
for (size_t i = 0; wpathname[i] != L'\0'; ++i) {
if (wpathname[i] == L'/') {
wpathname[i] = L'\\';
}
}

PyOS_snprintf(funcname, sizeof(funcname), "%.20s_%.200s", prefix, shortname);

{
Expand Down

0 comments on commit e8971f6

Please sign in to comment.