Recently I wanted to call Python from Fortran (see here) using a C wrapper. Below I post a simpler example. Second call of the function results in Segmentation fault at pModule = PyImport_Import(pName)
. I figured out that the problem is with the from scipy.optimize import newton
line - if I comment it everything works fine. Any ideas how to fix it?
rootC.c
#include "rootC.h"
#include <Python.h>
void root_(double* A, double* B, double* t, double* x)
{
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue, *sys, *path;
Py_Initialize();
sys = PyImport_ImportModule("sys");
path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyString_FromString("."));
pName = PyString_FromString("rootPY");
pModule = PyImport_Import(pName);
if (!pModule)
{
PyErr_Print();
printf("ERROR in pModule\n");
exit(1);
}
pFunc = PyObject_GetAttrString(pModule, "root");
pArgs = PyTuple_New(3);
PyTuple_SetItem(pArgs, 0, PyFloat_FromDouble((*A)));
PyTuple_SetItem(pArgs, 1, PyFloat_FromDouble((*B)));
PyTuple_SetItem(pArgs, 2, PyFloat_FromDouble((*t)));
pValue = PyObject_CallObject(pFunc, pArgs);
*x = PyFloat_AsDouble(pValue);
Py_Finalize();
}
rootC.h
#ifndef ROOT_H_
#define ROOT_H_
void root_(double*, double*, double*, double*);
#endif
rootPY.py
from mpmath import polylog, exp
from scipy.optimize import newton
def root(A,B,t):
return 1
main.c
#include "rootC.h"
#include <stdio.h>
int main()
{
double A = 0.4, B = 0.3, t = 0.1, x = 0.0;
root_(&A,&B,&t,&x);
printf("x = %.15f\n", x);
root_(&A,&B,&t,&x);
printf("x = %.15f\n", x);
return 0;
}
Makefile
CC = gcc
FC = gfortran
CFLAGS = -I/usr/include/python2.7
LFLAGS = -L/usr/local/lib -lpython2.7 -lm
.PHONY: all clean
all: main
main: main.o rootC.o
$(CC) $^ -o $@ $(LFLAGS)
main.o: main.c
$(CC) $(CFLAGS) -c $< -o $@
rootC.o: rootC.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o