Calling function in shared lib

119 views
Skip to first unread message

Olaf Wasmuth

unread,
Jun 4, 2003, 10:08:39 AM6/4/03
to
I am trying to write python code that is able to invoke
a function that was compiled into a shared library
(actually, FORTRAN routines). I did succeed for a specific
routine in a specific library, but now I want to do this
in a more flexible way.

That is, given the library path and the "signature" of the
routine (e.g. from a configuration file) at run time, I
would like to call this function from within Python. My
target platform is Unix (Solaris), so I'm thinking of
using the GNU libltdl to access the library and do the
call. But how can I feed the parameters into it ? Did
anybody do this already, or do you have some pointers
for me ?

Regards,

Olaf.

Thomas Heller

unread,
Jun 4, 2003, 12:17:09 PM6/4/03
to
Olaf Wasmuth <owas...@web.de> writes:

Sounds almost like ctypes:
http://starship.python.net/crew/theller/ctypes/
although I've never heard it runs on Solaris.

But I'll gladly accept patches!

Thomas

Fernando Perez

unread,
Jun 4, 2003, 1:22:01 PM6/4/03
to
Olaf Wasmuth wrote:


google(f2py)

It's a dream. Check the mailing list archives for some notes I wrote on how to
get started, which can be used as a companion to the standard docs. Mail me if
you can't find it.

best,

f.

Olaf Wasmuth

unread,
Jun 6, 2003, 6:51:49 AM6/6/03
to
Thomas Heller wrote:

Hi Thomas,

the good news is that ctypes works seems to work
(at least somewhat) under Solaris (!!!):
#!/usr/bin/env python
from ctypes import *

libc = cdll.LoadLibrary("/lib/libc.so")
print libc
libc.sleep( c_int(3) )
print libc.sleep

I get:
<CDLL '/lib/libc.so', handle ff200960 at ee380>
... and then, after 3 seconds ...
<ctypes._CdeclFuncPtr object at 0xe2200>

Nice tool, thanks for your suggestion ! Now I have
to see how this works out with compiled FORTRAN
routines ...

Regards,

Olaf.
--
Python makes me miss Smalltalk less

Olaf Wasmuth

unread,
Jun 6, 2003, 12:03:07 PM6/6/03
to
Olaf Wasmuth wrote:

Here is my update:

add.F:
======
SUBROUTINE ADD_II(A,B)
INTEGER*4 A,B

A = A+B
END

-> put this in libadd.so

add.py:
=======
from ctypes import *
libadd = cdll.LoadLibrary("./libadd.so")
#
# ADD_II becomes ADD_II_
# in Python, C and C++
#
method = libadd.ADD_II_
x = c_int(47)
y = c_int(11)
print "x = %d, y = %d" % (x.value, y.value)
#
# The byref() is necessary since
# FORTRAN does references,
# and not values (like e.g. C)
#
method( byref(x), byref(y) )
print "x = %d, y = %d" % (x.value, y.value)

-> now: python add.py

output:
======
x = 47, y = 11
x = 58, y = 11

Two things to keep in mind (at least under Solaris):
- FORTRAN uses call-by-reference (see code above)
- SUB(<string>,<int>)
must be treated as if it were declared as:
SUB(<string>,<int>,len(<string>))

Thomas, Fernando: thank you very much for your
advice ! I didn't try f2py yet, because building
it is quite an involved process, and ctypes seems
to be just what I wanted (I am trying to build RPMs
from all software I use and I was glad to find out
that it is supported by the distutils just fine).

Regards,

Olaf.

Reply all
Reply to author
Forward
0 new messages