How to disable RTLD_NOW for Python 2.7.x dlopen() in Mac OS X Mavericks?

T

tcwan99

Hi,

I've tried searching for topics related to dlopen() of dynamic libraries inthe list but there's nothing relevant or recent.

I'm facing a problem with a 3rd party C/C++ Framework (dynamic library) on Mac OS X which I'm trying to import into Python using ctypes. UnfortunatelyOS X has deprecated a function which is called from the Framework and is no longer available as of 10.8+ (Mtn. Lion, now updated to Mavericks). In addition, there is no recent version of the 3rd party framework available andit does not look like it's actively updated. (The 3rd party framework in question is the Fantom framework/driver for the LEGO Mindstorms NXT/EV3).

Existing tools using the 3rd party framework runs ok, but attempting to access it using ctypes fails with a Symbol not Found error when Python 2.7.x attempts to dlopen() the library. I suspect it is due to the setting of RTLD_NOW when opening the framework. I'd like to try to specify RTLD_LAZY instead.

Unfortunately sys.setdlopenflags(0) does not appear to do anything. RTLD_NOW is still set, which I presume overrides RTLD_LAZY if both flags were set.I've tried this with both the Apple supplied python and the version from MacPorts.

Enclosed is a snippet of the code and the Traceback. Is there a way to force RTLD_NOW off?

Code:
# Load library.
#dll = ctypes.cdll.LoadLibrary(libpath)
RTLD_LAZY = 1
sys.setdlopenflags(0)
print "dlopen() flags = %d" % sys.getdlopenflags()
dll = ctypes.CDLL(libpath, RTLD_LAZY)

[traceback]

$ arch -i386 python --version
Python 2.7.5

$ arch -i386 python pyfantom.py
Running on Darwin Platform (Rel. 13.0.0)
Bluetooth Stack not supported (Rel >= 12.0.0)
Found Fantom Library ( /Library/Frameworks/fantom.framework/fantom )
dlopen() flags = 0
Traceback (most recent call last):
File "pyfantom.py", line 83, in <module>
dll = ctypes.CDLL(libpath, RTLD_LAZY)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: dlopen(/Library/Frameworks/fantom.framework/fantom, 3): Symbol notfound: _IOBluetoothSDPServiceRecordGetRFCOMMChannelID
Referenced from: /Library/Frameworks/fantom.framework/fantom
Expected in: /System/Library/Frameworks/IOBluetooth.framework/Versions/A/IOBluetooth

[/traceback]
 
N

Ned Deily

I'm facing a problem with a 3rd party C/C++ Framework (dynamic library) on
Mac OS X which I'm trying to import into Python using ctypes. Unfortunately
OS X has deprecated a function which is called from the Framework and is no
longer available as of 10.8+ (Mtn. Lion, now updated to Mavericks). In
addition, there is no recent version of the 3rd party framework available and
it does not look like it's actively updated. (The 3rd party framework in
question is the Fantom framework/driver for the LEGO Mindstorms NXT/EV3).

Existing tools using the 3rd party framework runs ok, but attempting to
access it using ctypes fails with a Symbol not Found error when Python 2.7.x
attempts to dlopen() the library. I suspect it is due to the setting of
RTLD_NOW when opening the framework. I'd like to try to specify RTLD_LAZY
instead.

Unfortunately sys.setdlopenflags(0) does not appear to do anything. RTLD_NOW
is still set, which I presume overrides RTLD_LAZY if both flags were set.
I've tried this with both the Apple supplied python and the version from
MacPorts.

Enclosed is a snippet of the code and the Traceback. Is there a way to force
RTLD_NOW off?

I'm not very familiar with ctypes internals but it looks like
sys.setdlopenflags is not intended to have an influence on ctypes; rather, it
conditions the Python interpreter's use of dlopen to load shared Python
modules. For ctypes itself, it looks like it unconditionally sets RTLD_NOW
when calling dlopen and probably with good reason:

http://hg.python.org/cpython/file/2.7/Modules/_ctypes/callproc.c#l1432

Sorry, I don't have a suggestion for you, assuming you want to just try to
ignore the error, other than perhaps running an older version of OS X in a VM
on 10.8.
 
T

Tat-Chee Wan

I'm not very familiar with ctypes internals but it looks like

sys.setdlopenflags is not intended to have an influence on ctypes; rather, it

conditions the Python interpreter's use of dlopen to load shared Python

modules. For ctypes itself, it looks like it unconditionally sets RTLD_NOW

when calling dlopen and probably with good reason:



http://hg.python.org/cpython/file/2.7/Modules/_ctypes/callproc.c#l1432

Ah, thanks for the link. I couldn't figure out where the RTLD_NOW flag was set.
Sorry, I don't have a suggestion for you, assuming you want to just try to

ignore the error, other than perhaps running an older version of OS X in a VM

on 10.8.

That's not an option for me unfortunately. Guess I'll have to look into other alternatives to solve the missing symbol problem.

T. C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top