Fix: Windows DLL path lookup with Python >= 3.8
[babeltrace.git] / src / bindings / python / bt2 / bt2 / __init__.py
index f1c0954b273b5c135d6695493bc20dc60bf9f738..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
@@ -250,10 +265,10 @@ class _MemoryError(_Error):
 
 
 class UnknownObject(Exception):
-    '''
+    """
     Raised when a component class handles a query for an object it doesn't
     know about.
-    '''
+    """
 
     pass
 
This page took 0.025612 seconds and 4 git commands to generate.