unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [RFC/PATCH] python: search parent lib directory for libnotmuch.so
@ 2013-04-09  2:47 Jed Brown
  2013-04-09 14:13 ` Justus Winter
  0 siblings, 1 reply; 5+ messages in thread
From: Jed Brown @ 2013-04-09  2:47 UTC (permalink / raw)
  To: notmuch; +Cc: Sebastian Spaeth

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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [RFC/PATCH] python: search parent lib directory for libnotmuch.so
  2013-04-09  2:47 [RFC/PATCH] python: search parent lib directory for libnotmuch.so Jed Brown
@ 2013-04-09 14:13 ` Justus Winter
  2013-04-09 14:57   ` Jed Brown
  0 siblings, 1 reply; 5+ messages in thread
From: Justus Winter @ 2013-04-09 14:13 UTC (permalink / raw)
  To: Jed Brown, notmuch; +Cc: Sebastian Spaeth

Hi Jed,

Quoting Jed Brown (2013-04-09 04:47:26)
> 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.

May I ask why you cannot use LD_LIBRARY_PATH? I too install libnotmuch
to a non-standard location as unprivileged user and to make this
library available I add its path to LD_LIBRARY_PATH. It seems to work
just fine:

teythoon@thinkbox ~ % python -c 'import notmuch; print("\n".join(l.strip() for l in open("/proc/self/maps") if "notmuch" in l))'
7f4214339000-7f4214357000 r-xp 00000000 00:17 2120750                    /home/teythoon/.local/stow/notmuch/lib/libnotmuch.so.3.0.0
7f4214357000-7f4214557000 ---p 0001e000 00:17 2120750                    /home/teythoon/.local/stow/notmuch/lib/libnotmuch.so.3.0.0
7f4214557000-7f4214559000 rw-p 0001e000 00:17 2120750                    /home/teythoon/.local/stow/notmuch/lib/libnotmuch.so.3.0.0
teythoon@thinkbox ~ % python3.2 -c 'import notmuch; print("\n".join(l.strip() for l in open("/proc/self/maps") if "notmuch" in l))'
7fa5ecea4000-7fa5ecec2000 r-xp 00000000 00:17 2120750                    /home/teythoon/.local/stow/notmuch/lib/libnotmuch.so.3.0.0
7fa5ecec2000-7fa5ed0c2000 ---p 0001e000 00:17 2120750                    /home/teythoon/.local/stow/notmuch/lib/libnotmuch.so.3.0.0
7fa5ed0c2000-7fa5ed0c4000 rw-p 0001e000 00:17 2120750                    /home/teythoon/.local/stow/notmuch/lib/libnotmuch.so.3.0.0
teythoon@thinkbox ~ % env | grep '\.local'
PATH=/home/teythoon/.local/bin:/usr/local/stow/enlightenment/bin:/usr/local/bin:/usr/bin:/bin:/usr/games
LOCAL=/home/teythoon/.local
PYTHONPATH=/home/teythoon/.local/lib/python2.7/site-packages
LD_LIBRARY_PATH=/home/teythoon/.local/lib
PKG_CONFIG_PATH=/home/teythoon/.local/lib/pkgconfig
teythoon@thinkbox ~ % unset LD_LIBRARY_PATH
teythoon@thinkbox ~ % python -c 'import notmuch; print("\n".join(l.strip() for l in open("/proc/self/maps") if "notmuch" in l))'
7fd30b43c000-7fd30b457000 r-xp 00000000 fe:00 4241                       /usr/lib/libnotmuch.so.3.0.0
7fd30b457000-7fd30b657000 ---p 0001b000 fe:00 4241                       /usr/lib/libnotmuch.so.3.0.0
7fd30b657000-7fd30b658000 rw-p 0001b000 fe:00 4241                       /usr/lib/libnotmuch.so.3.0.0
teythoon@thinkbox ~ % python3.2 -c 'import notmuch; print("\n".join(l.strip() for l in open("/proc/self/maps") if "notmuch" in l))'
7fadb5704000-7fadb571f000 r-xp 00000000 fe:00 4241                       /usr/lib/libnotmuch.so.3.0.0
7fadb571f000-7fadb591f000 ---p 0001b000 fe:00 4241                       /usr/lib/libnotmuch.so.3.0.0
7fadb591f000-7fadb5920000 rw-p 0001b000 fe:00 4241                       /usr/lib/libnotmuch.so.3.0.0

> ---
> 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.

Well, if something like this is necessary and wanted (opinions
anyone?) at least let's not hardcode the assumption about the
directory layout but just walk up the tree until we find notmuch.so.3
or lib/notmuch.so.3. This way the bindings will find the correct
library even when they are included directly from within the source
tree.

Otoh, adding such behavior might be 'surprising' and lead to many
problems down the road like spurious bug reports just because the
magic library finder locates a rogue libnotmuch.so.3 somewhere.

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

How would you do that? fork'ing and exec'ing is not an option (and I'm
not sure what one could achieve by exec'ing it, but how else would you
talk to the dynamic linker?) , and poking around in binaries to get
their rpath isn't either in my opinion. And you would have to locate
the 'right' binary in the first place?

Cheers,
Justus

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC/PATCH] python: search parent lib directory for libnotmuch.so
  2013-04-09 14:13 ` Justus Winter
@ 2013-04-09 14:57   ` Jed Brown
  2013-04-09 15:21     ` Justus Winter
  0 siblings, 1 reply; 5+ messages in thread
From: Jed Brown @ 2013-04-09 14:57 UTC (permalink / raw)
  To: Justus Winter, notmuch; +Cc: Sebastian Spaeth

Justus Winter <4winter@informatik.uni-hamburg.de> writes:
>
> May I ask why you cannot use LD_LIBRARY_PATH? I too install libnotmuch
> to a non-standard location as unprivileged user and to make this
> library available I add its path to LD_LIBRARY_PATH. 

See libdir_in_ldconfig testing in configure: we make a significant
effort to set RPATH appropriately when installing to a location that is
not already searched (perhaps via LD_LIBRARY_PATH).  This currently does
not apply to the Python bindings, so while you can install without
LD_LIBRARY_PATH and still run the notmuch executable fine, you must set
LD_LIBRARY_PATH to use the Python bindings.  That is the inconsistency I
wanted to fix here.

>> 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.
>
> Well, if something like this is necessary and wanted (opinions
> anyone?) at least let's not hardcode the assumption about the
> directory layout but just walk up the tree until we find notmuch.so.3
> or lib/notmuch.so.3. This way the bindings will find the correct
> library even when they are included directly from within the source
> tree.

I actually wrote the more permissive version below, then decided I
preferred the stricter behavior because there was less chance of
accidentally finding a stale libnotmuch.so.3.  Note that in the source
tree, notmuch-shared already has RPATH pointing to the install location
so it's not valid without install.  The strict version of my patch has
similar behavior in that Python bindings are only valid when installed.
If you want to run them from the source tree, you'd have to add
/path/to/notmuch/lib to LD_LIBRARY_PATH.

diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py
index c7632c3..2fd383f 100644
--- a/bindings/python/notmuch/globals.py
+++ b/bindings/python/notmuch/globals.py
@@ -24,7 +24,16 @@ from ctypes import CDLL, Structure, POINTER
 try:
     nmlib = CDLL("libnotmuch.so.3")
 except:
-    raise ImportError("Could not find shared 'notmuch' library.")
+    import os.path
+    path = os.path.abspath(__file__)
+    while True:
+        path = os.path.dirname(path)
+        try:
+            nmlib = CDLL(os.path.join(path, 'libnotmuch.so.3'))
+            break
+        except:
+            if path == '/':
+                raise ImportError("Could not find shared 'notmuch' library.")
 
 from .compat import Python3StringMixIn, encode_utf8 as _str
 

> Otoh, adding such behavior might be 'surprising' and lead to many
> problems down the road like spurious bug reports just because the
> magic library finder locates a rogue libnotmuch.so.3 somewhere.
>
>> An alternative would be to find libnotmuch.so using the notmuch
>> executable.
>
> How would you do that? fork'ing and exec'ing is not an option (and I'm
> not sure what one could achieve by exec'ing it, but how else would you
> talk to the dynamic linker?) , and poking around in binaries to get
> their rpath isn't either in my opinion. And you would have to locate
> the 'right' binary in the first place?

I don't like the indirection either, but the binary is compiled with
knowledge of prefix/RPATH, so if we wanted a single canonical location
to specify this information, I would make it the binary.

If you don't want to trust Python install directory hierarchy, we could
have 'setup.py install' write some info about RPATH.

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [RFC/PATCH] python: search parent lib directory for libnotmuch.so
  2013-04-09 14:57   ` Jed Brown
@ 2013-04-09 15:21     ` Justus Winter
  2013-04-09 15:31       ` Jed Brown
  0 siblings, 1 reply; 5+ messages in thread
From: Justus Winter @ 2013-04-09 15:21 UTC (permalink / raw)
  To: Jed Brown, notmuch; +Cc: Sebastian Spaeth

Quoting Jed Brown (2013-04-09 16:57:05)
> Justus Winter <4winter@informatik.uni-hamburg.de> writes:
> >
> > May I ask why you cannot use LD_LIBRARY_PATH? I too install libnotmuch
> > to a non-standard location as unprivileged user and to make this
> > library available I add its path to LD_LIBRARY_PATH. 
> 
> See libdir_in_ldconfig testing in configure: we make a significant
> effort to set RPATH appropriately when installing to a location that is
> not already searched (perhaps via LD_LIBRARY_PATH).  This currently does
> not apply to the Python bindings, so while you can install without
> LD_LIBRARY_PATH and still run the notmuch executable fine, you must set
> LD_LIBRARY_PATH to use the Python bindings.  That is the inconsistency I
> wanted to fix here.

But why do we do that? I always thought that rpath causes more
problems and is to be avoided if possible [0]. But otoh, I didn't even
knew that the notmuch build system uses rpath.

0: e.g. http://wiki.debian.org/RpathIssue

> I don't like the indirection either, but the binary is compiled with
> knowledge of prefix/RPATH, so if we wanted a single canonical location
> to specify this information, I would make it the binary.
> 
> If you don't want to trust Python install directory hierarchy, we could
> have 'setup.py install' write some info about RPATH.

I actually have no strong feelings for or against this proposal. I'm
merely surprised that there is an issue that you are trying to fix
here since exactly this configuration has worked for me since the day
I started using notmuch.

And from my point of view LD_LIBRARY_PATH is the correct way to
instruct the dynamic linker (and as a similar facility the ctypes
library loader) where to look for additional libraries.

Cheers,
Justus

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC/PATCH] python: search parent lib directory for libnotmuch.so
  2013-04-09 15:21     ` Justus Winter
@ 2013-04-09 15:31       ` Jed Brown
  0 siblings, 0 replies; 5+ messages in thread
From: Jed Brown @ 2013-04-09 15:31 UTC (permalink / raw)
  To: Justus Winter, notmuch; +Cc: Sebastian Spaeth

Justus Winter <4winter@informatik.uni-hamburg.de> writes:
> But why do we do that? I always thought that rpath causes more
> problems and is to be avoided if possible [0]. But otoh, I didn't even
> knew that the notmuch build system uses rpath.
>
> 0: e.g. http://wiki.debian.org/RpathIssue

RPATH is bad for distributions, but great for private installs.

> I actually have no strong feelings for or against this proposal. I'm
> merely surprised that there is an issue that you are trying to fix
> here since exactly this configuration has worked for me since the day
> I started using notmuch.

Sure, but you always set LD_LIBRARY_PATH instead of relying on RPATH.

> And from my point of view LD_LIBRARY_PATH is the correct way to
> instruct the dynamic linker (and as a similar facility the ctypes
> library loader) where to look for additional libraries.

LD_LIBRARY_PATH is inconvenient as hell if you have multiple installs
living in different places, i.e., you write /path/to/A/bin/notmuch and
have it use /path/to/B/lib/libnotmuch.so.3 just because you placed
/path/to/B/lib in your LD_LIBRARY_PATH at some point in the distant past
when it was needed by /path/to/B/bin/other-thing that may not even be
installed any more.

This happens a lot more with languages other than C and when you have
different compilers with mutually-incompatible ABIs or packages that
didn't bother with soname versioning.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-04-09 15:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-09  2:47 [RFC/PATCH] python: search parent lib directory for libnotmuch.so Jed Brown
2013-04-09 14:13 ` Justus Winter
2013-04-09 14:57   ` Jed Brown
2013-04-09 15:21     ` Justus Winter
2013-04-09 15:31       ` Jed Brown

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).