From 13d46137fecc1d45cfc73c850265c257f1fb0583 Mon Sep 17 00:00:00 2001 From: Ben Meister <161079167+Ben-Meister@users.noreply.github.com> Date: Mon, 15 Apr 2024 21:51:11 -0400 Subject: [PATCH] Add compatibility for Python before 3.8 --- libconcord/bindings/python/libconcord.py | 26 ++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/libconcord/bindings/python/libconcord.py b/libconcord/bindings/python/libconcord.py index 3259180..b2d3fdb 100644 --- a/libconcord/bindings/python/libconcord.py +++ b/libconcord/bindings/python/libconcord.py @@ -37,8 +37,30 @@ # Load the DLL if platform.system() == 'Windows': - with os.add_dll_directory(os.path.dirname(__file__)): - _dll = cdll.LoadLibrary('libconcord-%i.dll' % ABI_VERSION) + # Python 3.8+ does not use PATH to find DLLs, you can add a search path by + # using os.add_dll_directory as shown below. If you are locating the DLLs + # outside of the package directory, simply call os.add_dll_directory as + # shown before importing this file. + # + # Here we are adding the path to search for DLL files bundled with the + # package. + try: + with os.add_dll_directory(os.path.dirname(__file__)): + _dll = cdll.LoadLibrary('libconcord-%i.dll' % ABI_VERSION) + except: + # If we're on an old version of Python, os.add_dll_directory is not + # available, but PATH is, so let's use it, and restore it after loading + # the DLL and dependencies. (It's not enough to specify the path in the + # call to LoadLibrary.) Changes are temporary and only for this process. + syspath = os.environ['PATH'] + os.environ['PATH'] = syspath + ";" + os.path.dirname(__file__) + try: + _dll = cdll.LoadLibrary('libconcord-%i.dll' % ABI_VERSION) + except: + os.environ['PATH'] = syspath + raise + os.environ['PATH'] = syspath + elif platform.system() == 'Darwin': _dll = cdll.LoadLibrary('libconcord.%i.dylib' % ABI_VERSION) else: