[RFC/PATCH] python: search parent lib directory for libnotmuch.so

Jed Brown jed at 59A2.org
Mon Apr 8 19:47:26 PDT 2013


If libnotmuch.so is installed to a path that is not searched by
dlopen(3), we must import it using an absolute path because the Python
bindings do not have the luxury of RPATH linking.  So strip off the
trailing directories from the install location and try CDLL with an
absolute path.
---
This is sort of a hack, but I don't know a less intrusive way to get
libnotmuch.so from somewhere dlopen(3) doesn't already search.

The absolute path version won't do the right thing in case of 'setup.py
develop', otherwise we could use it in all cases.  It may still make
sense to make the absolute path version take precedence.

An alternative would be to find libnotmuch.so using the notmuch
executable.

 bindings/python/notmuch/globals.py | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py
index c7632c3..2473f04 100644
--- a/bindings/python/notmuch/globals.py
+++ b/bindings/python/notmuch/globals.py
@@ -24,7 +24,24 @@ from ctypes import CDLL, Structure, POINTER
 try:
     nmlib = CDLL("libnotmuch.so.3")
 except:
-    raise ImportError("Could not find shared 'notmuch' library.")
+    try:
+        # If Notmuch is installed to a location not searched by
+        # dlopen(3), we must import it using an absolute path.  We
+        # can't just reset LD_LIBRARY_PATH because ld.so reads it when
+        # Python is starting and will not reread the environment.  We
+        # assume the install directory structure has the form:
+        #
+        #   /base-path/lib/pythonX.Y/site-packages/notmuch/globals.py
+        #
+        # so that we can get the library directory by stripping off
+        # the last four elements of the path.
+        import os.path
+        path = os.path.abspath(__file__)
+        for i in range(4):
+            path = os.path.dirname(path)
+        nmlib = CDLL(os.path.join(path, 'libnotmuch.so.3'))
+    except:
+        raise ImportError("Could not find shared 'notmuch' library.")
 
 from .compat import Python3StringMixIn, encode_utf8 as _str
 
-- 
1.8.2.1



More information about the notmuch mailing list