From 21f71465be009b7d7b9f152a3275b3612c307e42 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Sun, 27 Aug 2023 16:04:50 +0200
Subject: [PATCH 100/N] LoadLibraryExW: make sure to only use backslashes for
 paths

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
---
 Python/dynload_win.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index eac065e..8242e94 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -233,10 +233,22 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
     _Py_CheckPython3(); 
 #endif /* _MSC_VER */
 
+// So we can adjust the separators in the path below
+#define USE_UNICODE_WCHAR_CACHE 0
+
     wchar_t *wpathname = PyUnicode_AsWideCharString(pathname, NULL);
     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);
 
     {
