]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(macos): find the libpq in non-standard places
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 5 Sep 2022 09:56:06 +0000 (10:56 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 5 Sep 2022 11:28:25 +0000 (12:28 +0100)
psycopg/psycopg/pq/_pq_ctypes.py

index 43dfc1ad492ff957375ec427c51a3bc42da72d70..58b9bd4b95aac0a840f4ee5f9ae08094932cfa86 100644 (file)
@@ -4,6 +4,7 @@ libpq access using ctypes
 
 # Copyright (C) 2020 The Psycopg Team
 
+import os
 import ctypes
 import ctypes.util
 import sys
@@ -13,12 +14,33 @@ from typing import List, Optional, Tuple
 
 from ..errors import NotSupportedError
 
+import logging
+
+logger = logging.getLogger("psycopg")
+
+
 if sys.platform == "win32":
     libname = ctypes.util.find_library("libpq.dll")
+
 elif sys.platform == "darwin":
     libname = ctypes.util.find_library("libpq.dylib")
+    # (hopefully) temporary hack: libpq not in a standard place
+    # https://github.com/orgs/Homebrew/discussions/3595
+    # If pg_config is available and agrees, let's use its indications.
+    if not libname:
+        try:
+            import subprocess as sp
+
+            libdir = sp.check_output(["pg_config", "--libdir"]).strip().decode()
+            libname = os.path.join(libdir, "libpq.dylib")
+            if not os.path.exists(libname):
+                libname = None
+        except Exception as ex:
+            logger.debug("couldn't use pg_config to find libpq: %s", ex)
+
 else:
     libname = ctypes.util.find_library("pq")
+
 if not libname:
     raise ImportError("libpq library not found")