I work with a python swig-wrapped C++ library. In it's __init__.py
file, it sets the dlopen flag RTLD_GLOBAL before importing the shared object file containing the implementation code.
This causes the subsequent import of scipy.linalg to segfault, at least on my machine. I think this behavior depends on the way in which scipy was built and what it's linked against though.
# minimal example of what's going on
$ cat test.py
import sys
import ctypes
flags = sys.getdlopenflags()
sys.setdlopenflags(flags | ctypes.RTLD_GLOBAL)
import scipy.linalg
$ python test.py
[1] 16886 segmentation fault (core dumped) python test.py
- Why does this happen? What is going on?
- Under what conditions might setting RTLD_GLOBAL be necessary? The code that I work with contains the comment "# The following is an evil incantation that is needed to permit the POSIX "dlopen" function to work. I do not understand it. If a better solution is known, please forward to the PyOpenMM code maintainers." When I remove the
sys.setdlopenflags(flags | ctypes.RTLD_GLOBAL)
line, everything seems to work fine with the library, so maybe this is specific to certain python versions or platforms?