[C++-sig] Boost.Python appears to be catching Fortran READ "exceptions".
Mike
mike at thewilsonfamily.freeserve.co.uk
Sun Feb 6 18:49:22 CET 2005
Hi,
I am a new Boost.Python user.
Summary
---------
I have created a Boost.Python module (named 'hello') that calls a FORTRAN
subroutine (named FortDll) via a C++ function (named call_fortran ). The
FORTRAN code, which reads a data file, has the line:
READ(....,END=999) ! goto line labled 999 when eof reached
to handle the end of file condition. In non-Python environment it works as
expected, but under Python the eof condition is being caught by Boost.Python
as a "RuntimeError: unidentifiable C++ exception". If I create my module
without Boost.Python then I just get a crash.
Details
-------
I have the following Boost.Python module:
----Start of C++ code ----
// Using MS.NET 7.1.3088, Python 2.4, boost 1.31
// creates hello.dll
#include <boost/python.hpp>
extern "C" void __stdcall FortDll();
void call_fortran()
{
FortDll();
}
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
def("call_fortran", call_fortran);
}
----End of C++ code ----
The code for FortDll is as follows:
----Start of FORTRAN (F90) code ----
!
!Built using Intel(R) Fortran Compiler for MS.NET 2003, Version
8.0.1877.2003
!
SUBROUTINE FortDll
! Export subroutine into Dll
!DEC$ATTRIBUTES DLLEXPORT, DEFAULT, STDCALL, DECORATE, ALIAS:'FortDll'
::FortDll
INTEGER*4 I
OPEN(UNIT=33, FILE='INTS.DAT', ACCESS='SEQUENTIAL', STATUS='OLD')
DO WHILE(.TRUE.)
777 READ(33,*,END=999) I
! When the eof is reached the above line causes
! an exception in Python,so that .....
PRINT *,I
ENDDO
999 CONTINUE ! <<<<<< ...this line isn't reached!!!
PRINT *,'999 reached'
CLOSE(33)
END SUBROUTINE
----End of FORTRAN (F90) code ----
The test data file ints.dat is
----Start of ints.dat----
1
2
3
4
---EOF----
The results from testing are as follows:
----Start of Python session in debugger----
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> hello.call_fortran()
1
2
3
4
Traceback (most recent call last):
File "<stdin>", line 1, in ?
RuntimeError: unidentifiable C++ exception
>>>
----End of Python session----
The problem appears at line labled 777 in the Fortran code. As shown above
in the Python test case, when the FORTRAN hits a read error it appears
that the normal execution of code, as specified by
READ(..., END= 999) I ! goto line 999 when eof is reached
is not getting a chance to be executed.
When I catch the exception in the debugger, I get
First-chance exception at 0x00b58ca9 (FortDll.dll) in python.exe:
0xC0000005: Access violation reading location 0x00000000
My initial thought that this was to do with Boost.Python, however I get a
crash/core dump
using a Python module built around:
static PyObject * call_fortran(PyObject *self, PyObject *args)
{
FortDll(); // causes Python to crash when eof is reached.
return Py_BuildValue("i", 0);
}
Note that in a 'non-Python' environment, similar to this:
int main(int ,char** ) {
FortDll( );
}
the output is :
1
2
3
4
999 reached
ie. FortDll() works as expected.
My app I want to prototype in Python has several C++ classes that rely on
some routines written in FORTRAN. Can I stop Python handling the errors
that would have been handled in any case by the FORTRAN run time system?
Regards
Mike
More information about the Cplusplus-sig
mailing list