Fix: Windows DLL path lookup with Python >= 3.8
authorMichael Jeanson <mjeanson@efficios.com>
Wed, 11 Oct 2023 21:42:30 +0000 (17:42 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 21 Oct 2023 11:56:09 +0000 (07:56 -0400)
Starting with Python 3.8 on Windows [1] the DLLs lookup when loading
native modules was changed to not search PATH like everything else on
this platform. Restore this behavior by doing it manually.

Simon adds: it only started to matter now because MSYS2 dropped a local
patch [2] that retained the old behaviour.

[1]: https://docs.python.org/3/library/os.html#os.add_dll_directory
[2]: https://www.msys2.org/news/#2023-08-06-python-changed-behavior-when-loading-dll-dependencies-of-extension-modules

Change-Id: Id436244a717f440db11c33b6d418f31c7838591d
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11043
(cherry picked from commit 86f24b106e04c81bf252f2a4b1b99ad99b938de8)
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11115
Tested-by: jenkins <jenkins@lttng.org>
setup.cfg
src/bindings/python/bt2/bt2/__init__.py

index 9a58dd3cf6812ae271139dc0e78da87c4c627f1e..1c524c6902854e8ec64e694f3ed1a75d6617073a 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -4,8 +4,13 @@
 #       formatting)
 ignore = E501,W503
 
-# __init__.py has a bunch of (expectedly) unused imports, so disable that
-# warning for this file.
-per-file-ignores = src/bindings/python/bt2/bt2/__init__.py:F401
+# Disabled warnings for `bt2/__init__.py`:
+#
+# F401:
+#     Has a bunch of (expectedly) unused imports.
+#
+# E402:
+#     Has code to set up the DLL search path before imports.
+per-file-ignores = src/bindings/python/bt2/bt2/__init__.py:F401,E402
 
 exclude = tests/utils/python/tap
index 7068f2409474c02cbf1d62e04de6b160e8f0470a..73cc668831e9d33c15df0f23fe26a10e32c13e1b 100644 (file)
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
+import os
 import sys
 
+# With Python ≥ 3.8 on Windows, the DLL lookup mechanism to load native
+# modules doesn't search the `PATH` environment variable like everything
+# else on this platform.
+#
+# See <https://docs.python.org/3/whatsnew/3.8.html#bpo-36085-whatsnew>.
+#
+# Restore this behaviour by doing it manually.
+if os.name == "nt" and sys.version_info >= (3, 8):
+    for path in os.getenv("PATH", "").split(os.pathsep):
+        if os.path.exists(path) and path != ".":
+            os.add_dll_directory(path)
+
+del os
+
 # import all public names
 from bt2.clock_class import ClockClassOffset
 from bt2.clock_snapshot import _ClockSnapshotConst
This page took 0.026091 seconds and 4 git commands to generate.