From afabf79b7a967bf50471cd0e546c23865bb3c7e3 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Tue, 18 Feb 2020 17:38:32 +0100
Subject: [PATCH 26/26] Revert part of https://bugs.python.org/issue33895
 https://github.com/python/cpython/commit/4860f01ac0f07cdc8fc0cc27c33f5a64e5cfec9f

LIEF crashes because Python code gets called from its static initialization code (CreateModule2 gets called, more?)

So we disable some of this commit and add a new env. var CONDA_DLL_SEARCH_MODIFICATION_DROP_GIL_AS_PER_UPSTREAM
to revert to upstream's decision here.

There really isn't any way to get the ProcAddress without allowing static initialization to happen and
for LIEF, that calls back into Python. It is unclear why this isn't a problem for other extension modules.
This could be an issue with LIEF too, of course.

Pinging @tonyroberts, @romainthomas and @zooba for your thoughts.

This issue has been a bit of a nightmare for me. In the process I did make great strides making our CPython
more debuggable which should pay off in future.
---
 Python/dynload_win.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index cf5cb5cf78..9a8ffcf3f9 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -188,6 +188,11 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
 #if HAVE_SXS
         ULONG_PTR cookie = 0;
 #endif
+        /* The fix in https://bugs.python.org/issue33895 breaks 'import lief'. The fix means that any static initialization
+           cannot call into Python (or if it does it needs to aquire the GIL itself.
+           Other worse ideas:
+           DONT_RESOLVE_DLL_REFERENCES :: https://devblogs.microsoft.com/oldnewthing/20050214-00/?p=36463 */
+        int drop_gil = _wgetenv(L"CONDA_DLL_SEARCH_MODIFICATION_DROP_GIL_AS_PER_UPSTREAM") ? 1 : 0;
 
         /* Don't display a message box when Python can't load a DLL */
         old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
@@ -200,13 +205,21 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
            AddDllDirectory function. We add SEARCH_DLL_LOAD_DIR to
            ensure DLLs adjacent to the PYD are preferred. */
         /* This resyncs values in PATH to AddDllDirectory() */
+
+
         extern int CondaEcosystemModifyDllSearchPath(int, int);
         CondaEcosystemModifyDllSearchPath(1, 1);
 
-        Py_BEGIN_ALLOW_THREADS
-        hDLL = LoadLibraryExW(wpathname, NULL,
-                              LOAD_WITH_ALTERED_SEARCH_PATH);
-        Py_END_ALLOW_THREADS
+        if (drop_gil) {
+            Py_BEGIN_ALLOW_THREADS
+            hDLL = LoadLibraryExW(wpathname, NULL,
+                                LOAD_WITH_ALTERED_SEARCH_PATH);
+            Py_END_ALLOW_THREADS
+        } else {
+            hDLL = LoadLibraryExW(wpathname, NULL,
+                                LOAD_WITH_ALTERED_SEARCH_PATH);
+        }
+
 #if HAVE_SXS
         _Py_DeactivateActCtx(cookie);
 #endif
-- 
2.24.3 (Apple Git-128)

