By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Fortran-compiled DLLs in Python

P: n/a
Hi there everybody!
The problem is the following:
we have a DLL (H2O.dll) - compiled in Visual Fortran - depending in turn on
another DLL.
H2O.dll contains only one (1) function, with a known name (WATER).
The WATER function is called with the following parameters:
- TT (datatype: double precision) [input]
- PP (datatype: double precision) [input]
- State (datatype: integer)
[input]
- DiERR (datatype: integer)
[output]
- SIG (datatype: double precision)
[output]
- Prop (datatype: double precision array[16 elements]) [output]

Obviously, we would like to make use of Python to handle the function and
pick out the results...
We thought of using the **ctypes** module to tackle the problem, but we have
stranded just about at the first attempt:
from ctypes import *
h2o = windll.H2O

h2o.WATER(c_long(40.0),c_long(1.0),c_int(2),c_int( dierr),c_int(Sig),c_long*1
6(prop))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: int expected instead of float instance

Who can show us the Right way, riding the Python? THANKS!

stephen
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"byte biscuit" <st*************@casaccia.enea.it> writes:
Hi there everybody!
The problem is the following:
we have a DLL (H2O.dll) - compiled in Visual Fortran - depending in turn on
another DLL.
H2O.dll contains only one (1) function, with a known name (WATER).
The WATER function is called with the following parameters:
- TT (datatype: double precision) [input]
- PP (datatype: double precision) [input]
- State (datatype: integer)
[input]
- DiERR (datatype: integer)
[output]
- SIG (datatype: double precision)
[output]
- Prop (datatype: double precision array[16 elements]) [output]

Obviously, we would like to make use of Python to handle the function and
pick out the results...
We thought of using the **ctypes** module to tackle the problem, but we have
stranded just about at the first attempt:
from ctypes import *
h2o = windll.H2O

h2o.WATER(c_long(40.0),c_long(1.0),c_int(2),c_int( dierr),c_int(Sig),c_long*1
6(prop))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: int expected instead of float instance


Looking at your function description above, I would guess you have call
it in the following way (although the error you get is that c_long(40.0)
complains about the float argument):

dierr = c_int()
prop = (c_double * 16)() # create a 16 element double array

h2o.WATER(c_double(40.0), c_double(1.0), c_int(2),
byref(dierr), prop)

Although I do know nothing about fortran (anymore).

Thomas
Jul 18 '05 #2

P: n/a
Hi Thomas!
an answer from the true creator of the ctypes module - nice!
anyway, we tried implementing your solution and got the following
Traceback Error:
h2o.WATER(c_double(40.0), c_double(1.0), c_int(2),byref(dierr), byref(sig), prop)


Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation

which would seem to be the classical exception raised through a failed
function call...?

allthebest

luca (+stephen)


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 18 '05 #3

P: n/a
"byte biscuit" <st*************@casaccia.enea.it> writes:
Hi there everybody!
The problem is the following:
we have a DLL (H2O.dll) - compiled in Visual Fortran - depending in turn on
another DLL.
H2O.dll contains only one (1) function, with a known name (WATER).
The WATER function is called with the following parameters:
- TT (datatype: double precision) [input]
- PP (datatype: double precision) [input]
- State (datatype: integer)
[input]
- DiERR (datatype: integer)
[output]
- SIG (datatype: double precision)
[output]
- Prop (datatype: double precision array[16 elements]) [output]

Obviously, we would like to make use of Python to handle the function and
pick out the results...
We thought of using the **ctypes** module to tackle the problem, but we have
stranded just about at the first attempt:
from ctypes import *
h2o = windll.H2O
h2o.WATER(c_long(40.0),c_long(1.0),c_int(2),c_int( dierr),c_int(Sig),c_long*1
6(prop))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: int expected instead of float instance

Who can show us the Right way, riding the Python? THANKS!

stephen
--
http://mail.python.org/mailman/listinfo/python-list
"Luca Simonetti" <si********@tiscali.it> writes:
Hi Thomas!
an answer from the true creator of the ctypes module - nice!
anyway, we tried implementing your solution and got the following
Traceback Error:
h2o.WATER(c_double(40.0), c_double(1.0), c_int(2),byref(dierr), byref(sig), prop)

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation

which would seem to be the classical exception raised through a failed
function call...?


This is the classical GP fault, which would normally crash a program.
ctypes catches it, but doesn't give more information (in current CVS, it
would have informed you about which memory location could not be written
or read, but that would probably not help you very much).
- Prop (datatype: double precision array[16 elements]) [output]


I do not know how fortran passes this parameter, in the example code I
posted I assumed that the caller allocates an array, passes a pointer to
it to fortran, and the fortran code fills in the elements.

Do you have C sample code that works?

Thomas
Jul 18 '05 #4

P: n/a
On Thu, 27 May 2004 13:36:26 +0000 (UTC), "Luca Simonetti"
<si********@tiscali.it> declaimed the following in comp.lang.python:
Hi Thomas!
an answer from the true creator of the ctypes module - nice!
anyway, we tried implementing your solution and got the following
Traceback Error:
h2o.WATER(c_double(40.0), c_double(1.0), c_int(2),byref(dierr), byref(sig), prop)

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation


C passes by value, and to get a "modifiable" argument, the
programmer has to explicitly pass an address (as the value) and then,
also, explicitly dereference through the value.

Traditionally, FORTRAN passes ALL arguments by address, not
value. This may vary a bit in more modern implementations -- character
strings may pass a "string descriptor", which is a structure containing
the address of the string (as normal) along with an added word
containing the size of the space allocated for the string.

I suspect you need to use byref() on all of the arguments.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #5

P: n/a
I feel we're getting there...

recapitulating:
from ctypes import *
h2o = windll.H2O # loads DLL in python
tt=c_double(20.) # tt, pp and state are inputs, given values in C datatypes pp=c_double(1.0)
state=c_int(1)
dierr = c_int() #dierr, sig and prop are outputs, initialised in C datatypes sig = c_double()
prop = (c_double * 16)() # create a 16 element double array
access violation error:
h2o.WATER(tt, pp, state, byref(dierr), byref(sig), prop) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation

which in fact - Dennis - is caused by non-referencing, because:
h2o.WATER(byref(tt), byref(pp), byref(state), byref(dierr), byref(sig),
prop)
returns without error.

It is interesting (for me, that is) to note that prop in fact does NOT need
referencing, whereas all other variables (input&output) DO. But, the
returned answer is:

0

which shouldn't be of course............... so there does seem to be a
problem in the definition/passing of the prop parameter, I would say?

stephen
"Thomas Heller" <th*****@python.net> ha scritto nel messaggio
news:ma*************************************@pytho n.org...
This is the classical GP fault, which would normally crash a program.
ctypes catches it, but doesn't give more information (in current CVS, it
would have informed you about which memory location could not be written
or read, but that would probably not help you very much).
- Prop (datatype: double precision array[16 elements]) [output]


I do not know how fortran passes this parameter, in the example code I
posted I assumed that the caller allocates an array, passes a pointer to
it to fortran, and the fortran code fills in the elements.

Do you have C sample code that works?

Thomas

"Dennis Lee Bieber" <wl*****@ix.netcom.com> ha scritto nel messaggio
news:p0********************************@4ax.com...
C passes by value, and to get a "modifiable" argument, the programmer has to explicitly pass an address (as the value) and then,
also, explicitly dereference through the value.

Traditionally, FORTRAN passes ALL arguments by address, not
value. This may vary a bit in more modern implementations -- character
strings may pass a "string descriptor", which is a structure containing
the address of the string (as normal) along with an added word
containing the size of the space allocated for the string.

I suspect you need to use byref() on all of the arguments.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #6

P: n/a
On Fri, 28 May 2004 12:06:33 +0200, "byte biscuit"
<st*************@casaccia.enea.it> declaimed the following in
comp.lang.python:

It is interesting (for me, that is) to note that prop in fact does NOT need
referencing, whereas all other variables (input&output) DO. But, the
returned answer is:
Okay, my "all" was a bit over done... Since C normally passes
(as a value) the address of the first element, that already matched what
FORTRAN expected.
0

which shouldn't be of course............... so there does seem to be a
problem in the definition/passing of the prop parameter, I would say?
I suspect the declaration...

code> prop = (c_double * 16)()

I've not used ctypes, so I don't fully understand the above, but
it looks very much like it is making 16 copies of the c_double FUNCTION,
and then invoking this "array" with no arguments. The closest thing I
find in the ctypes tutorial page implies you don't want the trailing ()

stephen
"Thomas Heller" <th*****@python.net> ha scritto nel messaggio
news:ma*************************************@pytho n.org...

This is the classical GP fault, which would normally crash a program.
ctypes catches it, but doesn't give more information (in current CVS, it
would have informed you about which memory location could not be written
or read, but that would probably not help you very much).
- Prop (datatype: double precision array[16 elements]) [output]


I do not know how fortran passes this parameter, in the example code I
posted I assumed that the caller allocates an array, passes a pointer to
it to fortran, and the fortran code fills in the elements.

Do you have C sample code that works?

Thomas

"Dennis Lee Bieber" <wl*****@ix.netcom.com> ha scritto nel messaggio
news:p0********************************@4ax.com...
C passes by value, and to get a "modifiable" argument, the

programmer has to explicitly pass an address (as the value) and then,
also, explicitly dereference through the value.

Traditionally, FORTRAN passes ALL arguments by address, not
value. This may vary a bit in more modern implementations -- character
strings may pass a "string descriptor", which is a structure containing
the address of the string (as normal) along with an added word
containing the size of the space allocated for the string.

I suspect you need to use byref() on all of the arguments.


-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #7

P: n/a

Did you consider f2py? If you do not have the source you will
need to write the interface by hand, something you need to do for ctypes
anyway.

http://cens.ioc.ee/projects/f2py2e/

Sincerely yours
Pierre Schnizer

--
Remove nospam from the adress for direct replies
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.