Question: "load"ing a shared object in python

Mark Rowe mail.python.org at bdash.net.nz
Thu Jan 27 19:05:11 EST 2005


On Jan 28, 2005, at 6:48 AM, Rick L. Ratzel wrote:

> Pro Grammer <a at b.c> writes:
>
>> Hello, all,
>> I am not sure if this is the right place to ask, but could you kindly  
>> tell me
>> how to "load" a shared object (like libx.so) into python, so that the  
>> methods in
>> the .so can be used? That too, given that the shared object was  
>> written in c++,
>> compiled with g++ ?
>> Thanks,
>> Pro Grammer
>
>    Will the dl standard library module help you?  From the Python docs  
> at:
>
> http://docs.python.org/lib/module-dl.html

As Simon Brunning notes, ctypes  
(<http://starship.python.net/crew/theller/ctypes/>) is a robust  
alternative to the `dl' module.

> Example:
>
>>>> import dl, time
>>>> a=dl.open('/lib/libc.so.6')
>>>> a.call('time'), time.time()
> (929723914, 929723914.498)

A note about this example: the `dl' call returns an integer as that is  
what the C time function returns.  time.time() in Python is implemented  
in terms of C's gettimeofday, ftime or time depending on the platform.

> I'm guessing that there might be some C++ issues, but maybe it's worth
> looking into.

As far as I am aware, neither dl nor ctypes natively support C++.  This  
is a tricky matter, due to the lack of standards for C++ ABI's covering  
name mangling and vtable layout etc.  See the thread starting at  
<http://mail.python.org/pipermail/python-list/2004-November/ 
249513.html> for more information.

Regards,

Mark Rowe
<http://bdash.net.nz/>




More information about the Python-list mailing list