Issue 1:
Describe the issue:

I am experiencing a NumPy crash in a rather complicated environment. I have a Fortran+MPI application, GEOS (https://github.com/geos-esm) that calls a Python function at some point via a CFFI interface. My application crashes every time I try to 'import numpy' and the traceback looks like what is included in the 'Error message' section for each of the MPI ranks.

Via test_crash_interface, FV_StateMod.F90 calls the Python function

def import_numpy():

import numpy as np

print('imported numpy successfully')

The application runs successfully if I do not import numpy. Also, I can import other modules, e.g. mpi4py, just fine.

I tried to create a reproducer that simulates the GEOS infrastructure by using a simple Fortran driver, but in case of the reproducer, numpy is imported successfully. Also, I tried pybind11 instead of cffi as the Fortran-Python interface and I get the same crash.

Please let me know if there is any other information I can provide.

Reproduce the code example:

Via test_crash_interface, FV_StateMod.F90 calls the Python function

def import_numpy():
import numpy as np
print('imported numpy successfully')

Error message:

Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.
Backtrace for this error:
#0 0x2ba19513114f in ???
#1 0x2ba0cb73aa19 in longdouble_multiply
at numpy/core/src/umath/scalarmath.c.src:854
#2 0x2ba1956f900b in ???
...
#35 0x2ba19555d50e in ???
#36 0x2ba1ca10e8be in general_invoke_callback
at c/_cffi_backend.c:6180
#37 0x2ba1ca10eaad in cffi_call_python
at c/call_python.c:276
#38 0x2ba185630325 in test_crash_interface
at /discover/nobackup/pchakrab/code/gmao/GEOSfvdycore/build-debug/src/Components/@FVdycoreCubed_GridComp/testcrashinterface.c:1290
#39 0x88ad26 in __fv_statemod_MOD_fv_run
at /discover/nobackup/pchakrab/code/gmao/GEOSfvdycore/src/Components/@FVdycoreCubed_GridComp/FV_StateMod.F90:1779

NumPy/Python version information:

My environment is as follows:

GCC 11.2.0

Python 3.8.10

NumPy 1.21.2 (same crash with 1.21.5 and 1.22.0)

CFFI 1.15.0

I have reproduced this error on 2 different systems - (1) Intel Cascade Lake node running SLES 12, SP5, and (2) AMD Rome node running CentOS 7.

Issue 2:
Describe the issue:

We recently embedded a python interpreter in a scientific Fortran code, and we are having problems with debugging floating point errors that happen inside our code. We typically do this by enabling all the floating point traps, but we can't get past the import_array() call during initialization due to a SIGFPE. This happens with multiple versions of NumPy, the latest we are using is 1.18.1, and it also happens with all versions of python, GNU compilers, and Intel compilers that we have tried.

I vaguely recall seeing an email thread from a long time ago (that I cannot find again now) that links this to a for loop that continually multiplies a number until it overflows to define the limit for floating point numbers and I think that was related. Maybe somebody else can find it better than me.

The two source files to reproduce it are below and can be compiled with:

icx -c -g -O0 -traceback -I... -L... -lpython... test.c
ifort -g -O0 -fpe0 -I... -L... -lpython... test.o test.f90

or

gcc -c -g -O0 -I... -L... -lpython... test.c
gfortran -g -O0 -fbacktrace -I... -L... -lpython... -ffpe-trap=invalid,zero,overflow,underflow,inexact,denormal test.o test.f90

In the event this isn't a bug or won't be fixed, we could use some advice on how to get around the floating point error problems in import_array() because it takes away a really critical tool for tracking down problems in our scientific core code.

Reproduce the code example:

test.c

#include <numpy/arrayobject.h>
#include <Python.h>

void *initpython()
{
Py_Initialize();
import_array();
}

test.f90

PROGRAM test
USE ISO_C_BINDING, ONLY: C_PTR

IMPLICIT NONE

INTERFACE
FUNCTION InitPython() BIND(C)
USE ISO_C_BINDING, ONLY: C_PTR
IMPLICIT NONE

TYPE(C_PTR) :: InitPython
END FUNCTION InitPython
END INTERFACE

TYPE(C_PTR) :: ignoreThis

ignoreThis = InitPython()
END PROGRAM test

### Error message:

```shell
forrtl: error (72): floating overflow
Image PC Routine Line Source
a.out 0000000000404B0B Unknown Unknown Unknown
libc.so.6 0000147F231C07A0 Unknown Unknown Unknown
_multiarray_umath 0000147F21FC1CE7 Unknown Unknown Unknown
libpython3.7m.so. 0000147F2356C50C PyNumber_Multiply Unknown Unknown
libpython3.7m.so. 0000147F234EE63C _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F210A _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F236A4FAE _PyEval_EvalCodeW Unknown Unknown
libpython3.7m.so. 0000147F236A509F PyEval_EvalCodeEx Unknown Unknown
libpython3.7m.so. 0000147F235940CC PyEval_EvalCode Unknown Unknown
libpython3.7m.so. 0000147F23597F6E Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CAE91 _PyMethodDef_RawF Unknown Unknown
libpython3.7m.so. 0000147F235CB526 _PyCFunction_Fast Unknown Unknown
libpython3.7m.so. 0000147F234F1D61 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F236A4FAE _PyEval_EvalCodeW Unknown Unknown
libpython3.7m.so. 0000147F235CAFC0 _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F210A _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EDCB9 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB36F _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F235CBE03 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CC0AA _PyObject_CallMet Unknown Unknown
libpython3.7m.so. 0000147F235EEE20 PyImport_ImportMo Unknown Unknown
libpython3.7m.so. 0000147F235960F6 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB650 PyCFunction_Call Unknown Unknown
libpython3.7m.so. 0000147F234F1D61 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F236A4FAE _PyEval_EvalCodeW Unknown Unknown
libpython3.7m.so. 0000147F235CAFC0 _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB36F _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F235CBE03 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CC0AA _PyObject_CallMet Unknown Unknown
libpython3.7m.so. 0000147F235EEE20 PyImport_ImportMo Unknown Unknown
libpython3.7m.so. 0000147F235960F6 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB650 PyCFunction_Call Unknown Unknown
libpython3.7m.so. 0000147F234F1D61 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F236A4FAE _PyEval_EvalCodeW Unknown Unknown
libpython3.7m.so. 0000147F235CAFC0 _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB36F _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F235CBE03 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CC0AA _PyObject_CallMet Unknown Unknown
libpython3.7m.so. 0000147F235EEE20 PyImport_ImportMo Unknown Unknown
libpython3.7m.so. 0000147F235960F6 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB650 PyCFunction_Call Unknown Unknown
libpython3.7m.so. 0000147F234F1D61 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F236A4FAE _PyEval_EvalCodeW Unknown Unknown
libpython3.7m.so. 0000147F235CAFC0 _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB36F _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F235CBE03 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CC0AA _PyObject_CallMet Unknown Unknown
libpython3.7m.so. 0000147F235EEE20 PyImport_ImportMo Unknown Unknown
libpython3.7m.so. 0000147F235960F6 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB650 PyCFunction_Call Unknown Unknown
libpython3.7m.so. 0000147F234F1D61 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F236A4FAE _PyEval_EvalCodeW Unknown Unknown
libpython3.7m.so. 0000147F235CAFC0 _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234F4116 Unknown Unknown Unknown
libpython3.7m.so. 0000147F234EF808 _PyEval_EvalFrame Unknown Unknown
libpython3.7m.so. 0000147F234F3380 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CB36F _PyFunction_FastC Unknown Unknown
libpython3.7m.so. 0000147F235CBE03 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CC0AA _PyObject_CallMet Unknown Unknown
libpython3.7m.so. 0000147F235EEE20 PyImport_ImportMo Unknown Unknown
libpython3.7m.so. 0000147F235960F6 Unknown Unknown Unknown
libpython3.7m.so. 0000147F235CACD3 _PyMethodDef_RawF Unknown Unknown
libpython3.7m.so. 0000147F235CB526 _PyCFunction_Fast Unknown Unknown
libpython3.7m.so. 0000147F235CD06E PyObject_CallFunc Unknown Unknown
libpython3.7m.so. 0000147F235EF5C8 PyImport_Import Unknown Unknown
libpython3.7m.so. 0000147F235EF75D PyImport_ImportMo Unknown Unknown
a.out 0000000000403927 _import_array 1466 __multiarray_api.h
a.out 00000000004038D2 initpython 7 test.c
a.out 0000000000403C23 MAIN__ 17 test.f90
a.out 00000000004038A2 Unknown Unknown Unknown
libc.so.6 0000147F231AB540 Unknown Unknown Unknown
libc.so.6 0000147F231AB5EC __libc_start_main Unknown Unknown
a.out 00000000004037A5 Unknown Unknown Unknown

NumPy/Python version information:

Python 3.7.6 (default, Jan 8 2020, 19:59:22)

[GCC 7.3.0] :: Anaconda, Inc. on linux

Type "help", "copyright", "credits" or "license" for more information.

import sys, numpy; print(numpy.version, sys.version)

1.18.1 3.7.6 (default, Jan 8 2020, 19:59:22)

[GCC 7.3.0]