[C++-sig] Re: Boost.Python appears to be catching Fortran READ "exceptions".

Mike mike at thewilsonfamily.freeserve.co.uk
Tue Feb 8 01:07:08 CET 2005


Thanks for your quick reply Ralf.  Thinking along your suggested line of
exceptions, and given that I know that this _does_ work as expected...

  #include <iostream>
  extern "C" void __stdcall FortDll();
  int main(int ,char** ) {
       FortDll( ); // performs a Fortran  READ(33,*,END=999) I
       std::cout << "FortDll worked with no exceptions.\n"; // this line
gets executed!
  }

...I  then had the idea of trying this:

  #include <iostream>
  #include <Python.h>
  extern "C" void __stdcall FortDll();

  int main(int, char**)
  {
    // Call Fortran function FortDll
    FortDll();   // Line #A - ensures Fortran RTL is loaded first.

    Py_Initialize();

    // Now call FortDll again
    PyRun_SimpleString("import hello\n"
                       "hello.call_fortran()\n"); // this calls FortDll()
    Py_Finalize();
 }

The output is:

           1
           2
           3
           4
 999 reached
           1
           2
           3
           4
 999 reached


No exceptions!  If  I remove Line #A then I get my 1\n2\n3\n4 follwed by the
  "RuntimeError: unidentifiable C++ exception"
message (no "999 reached").

The aboves points to one possible solution (ensure the Fortran RTL is loaded
before Python which is called via my "main" program), but it would be nice
to be able to do a similar sort of thing starting from the Python prompt.

I'll have a further look at the FORTRAN docs & see if that provides any
clues.

Thanks again,

Mike

>
>
> From: "Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com>
> Subject: Re: [C++-sig] Boost.Python appears to be catching Fortran
> READ "exceptions".
> To: Development of Python/C++ integration <c++-sig at python.org>
> Message-ID: <20050206204018.58393.qmail at web20226.mail.yahoo.com>
> Content-Type: text/plain; charset=us-ascii
>
> --- Mike <mike at thewilsonfamily.freeserve.co.uk> wrote:
>> 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?
>
> It is not Python or Boost.Python doing anything special, it depends
> on your Fortran compiler how I/O errors are dealt with. It must be
> the Fortran runtime-library throwing the exception. I'd look at the
> Fortran documentation to find out about the interrelation of Fortran
> I/O errors and C++ exceptions. Ralf
> P.S.: I always found it easier to translate Fortran code to C++
> rather than dealing with inter-language complications.
>
P.S. I did translate the routine based on the given code,
but it may not be possible in other cases.





More information about the Cplusplus-sig mailing list