Search code examples
pythonnumpyfortranf2py

How can I get info on run time errors for Python modules built with f2py


Edit 3/6: Sorry, this was a false alarm. It seems to be working now. I believe the -g flag on gfortran and --debug-capi are sufficient after all to generate line numbers for GDB (gnu debugger). I will make a short answer summarizing how to do this in case it is helpful for others (was a bit of a learning experience for me). But please feel free to post additional answers that improve on this. In particular I would be interested in being able to get line numbers without using GDB, if that's possible.

This must be a common problem but I can't find any answers. The error is in in the Fortran library (what I call f2.f90 below), but it's a runtime error that isn't triggered until I am using the code via Python.

To make it a little more explicit:

  1. I compile several files with gfortran:

    gfortran -c f1.f90 f2.f90
    
  2. I generate a module with f2py:

    f2py -c f1.o f2.o -m main main.f90
    
  3. In python:

    import main
    
  4. In python, call subroutine main, inside of main:

    main.main()
    

I can get this to work successfully but when I play around with f2.f90 I will often have some error, and when that happens it's in step 4 and will crash python and only give me the option to close it. Unfortunately I get no information about what line in f2.f90 caused the problem or any other info.

So, pretty simple question, is there a way I can cause step 4 to give me some basic info about the run time crash? Mainly the line number of the problem.

I've tried adding -g -fbacktrace to gfortran call and --debug-capi to f2py and then using gdb python with backtrace. This looks somewhat promising as it at least tells me that the offending file is f2.f90, but not the line number. If I try "backtrace full" it will tell me No symbol table info available which is probably where the line number info is held?


Solution

  • In brief, this combo of flags seems to allow gdb to produce line number of any runtime errors in f1.f90 or f2.f90:

    C:\> gfortran -g -fbacktrace -c f1.f90 f2.f90
    C:\> f2py --debug-capi -c -I. f1.o f2.o -m main main.f90
    C:\> gdb python
    
    (gdb) run main.py
    

    Where main.py imports/calls main.f90 which is using/calling f1.f90 and f2.f90. Can then do backtrace to get some additional info.

    (gdb) backtrace