From f671c036eab4d7a6e13f36fead5cc37d7b589133 Mon Sep 17 00:00:00 2001
From: Naveen M K <naveen521kk@gmail.com>
Date: Sat, 5 Aug 2023 14:10:27 +0530
Subject: [PATCH 097/N] Search DLLs only on paths added using
 `add_dll_directory()`.

This is the default behavior on upstream Python. We previously
reverted this behavior because it broke some use cases.

The old behavior can be restored by setting the environment variable
PYTHONLEGACYWINDOWSDLLLOADING to 1.

Fixes https://github.com/msys2-contrib/cpython-mingw/issues/48
Also fixes https://github.com/msys2-contrib/cpython-mingw/issues/141

Signed-off-by: Naveen M K <naveen521kk@gmail.com>
---
 Lib/test/__main__.py                   |  8 ++++++++
 Python/dynload_win.c                   | 16 ++++++++++++++--
 Tools/build/check_extension_modules.py |  4 ++++
 mingw_smoketests.py                    |  3 +++
 4 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/Lib/test/__main__.py b/Lib/test/__main__.py
index 82b50ad..25bac39 100644
--- a/Lib/test/__main__.py
+++ b/Lib/test/__main__.py
@@ -1,2 +1,10 @@
+import os
+import sys
+
 from test.libregrtest.main import main
+
+if sys.platform == "win32":
+    # Enable DLL loading from PATH.
+    os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
+
 main(_add_python_opts=True)
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index 56588ef..7e0eca3 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -4,6 +4,7 @@
 #include "Python.h"
 #include "pycore_fileutils.h"     // _Py_add_relfile()
 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
+#include "pycore_initconfig.h"
 
 #include "pycore_importdl.h"      // dl_funcptr
 #include "patchlevel.h"           // PY_MAJOR_VERSION
@@ -206,6 +207,18 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
     dl_funcptr p;
     char funcname[258], *import_python;
 
+    int use_legacy = 0;
+    DWORD load_library_flags = 0;
+
+    _Py_get_env_flag(1, &use_legacy, "PYTHONLEGACYWINDOWSDLLLOADING");
+
+    if (use_legacy) {
+        load_library_flags = LOAD_WITH_ALTERED_SEARCH_PATH;
+    } else {
+        load_library_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
+                              LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
+    }
+
 #ifdef _MSC_VER
     _Py_CheckPython3(); 
 #endif /* _MSC_VER */
@@ -230,8 +243,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
            AddDllDirectory function. We add SEARCH_DLL_LOAD_DIR to
            ensure DLLs adjacent to the PYD are preferred. */
         Py_BEGIN_ALLOW_THREADS
-        hDLL = LoadLibraryExW(wpathname, NULL,
-                              LOAD_WITH_ALTERED_SEARCH_PATH);
+        hDLL = LoadLibraryExW(wpathname, NULL, load_library_flags);
         Py_END_ALLOW_THREADS
         PyMem_Free(wpathname);
 
diff --git a/Tools/build/check_extension_modules.py b/Tools/build/check_extension_modules.py
index e0c7a92..fab01cf 100644
--- a/Tools/build/check_extension_modules.py
+++ b/Tools/build/check_extension_modules.py
@@ -64,6 +64,10 @@ WINDOWS_MODULES = {
     "winsound",
 }
 
+if sys.platform == "win32":
+    # Enable DLL loading from PATH.
+    os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
+
 
 logger = logging.getLogger(__name__)
 
diff --git a/mingw_smoketests.py b/mingw_smoketests.py
index 516005c..ca1f652 100644
--- a/mingw_smoketests.py
+++ b/mingw_smoketests.py
@@ -34,6 +34,9 @@ if os.environ.get("MSYSTEM", ""):
 else:
     SEP = "\\"
 
+if sysconfig.is_python_build():
+    os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
+
 _UCRT = sysconfig.get_platform() not in ('mingw_x86_64', 'mingw_i686')
 
 
