From 7a25045fb92aefdd137cce7e7d3e5df20638b2c7 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Mon, 15 May 2023 17:14:51 +0200
Subject: [PATCH] add call to initialize() => resolved segfault

---
 PyCore/Embed/PyInterpreter.cpp | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/PyCore/Embed/PyInterpreter.cpp b/PyCore/Embed/PyInterpreter.cpp
index 8a73b0e2d35..3873f5d8af2 100644
--- a/PyCore/Embed/PyInterpreter.cpp
+++ b/PyCore/Embed/PyInterpreter.cpp
@@ -178,6 +178,9 @@ void PyInterpreter::setPythonPath(const std::string& path)
 PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::string& path)
 {
     ASSERT(!pymodule_name.empty());
+
+    PyInterpreter::Numpy::initialize();
+
     if(!path.empty())
         addPythonPath(path);
 
@@ -583,15 +586,16 @@ PyObjectPtr PyInterpreter::BornAgain::import(const std::string& path)
         PyInterpreter::addPythonPath(path);
 
 #ifndef _WIN32
-    // Stores signal handler before numpy's mess it up.
-    // This is to make ctrl-c working from terminal.
-
-    PyOS_sighandler_t sighandler;
-    sighandler = PyOS_getsig(SIGINT);
+    // store ctrl-C handler before Numpy messes it up
+    PyOS_sighandler_t sighandler = PyOS_getsig(SIGINT);
 #endif
 
     PyObject* ba_pymodule = PyImport_ImportModule("bornagain");
 
+#ifndef _WIN32
+    PyOS_setsig(SIGINT, sighandler); // restore previous ctrl-C handler
+#endif
+
     if (!ba_pymodule || !PyModule_Check(ba_pymodule)) {
         checkError();
         throw std::runtime_error(
@@ -600,11 +604,6 @@ PyObjectPtr PyInterpreter::BornAgain::import(const std::string& path)
                              + path + "')"));
     }
 
-    // restores single handler to make ctr-c alive.
-#ifndef _WIN32
-    PyOS_setsig(SIGINT, sighandler);
-#endif
-
     return {ba_pymodule};
 }
 
@@ -635,6 +634,8 @@ PyObjectPtr PyInterpreter::BornAgain::importScript(const std::string& script,
 std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::string& script,
                                                                    const std::string& path)
 {
+    PyInterpreter::Numpy::initialize();
+
     PyObjectPtr tmpModule{PyInterpreter::BornAgain::importScript(script, path)};
 
     PyObject* pDict = PyModule_GetDict(tmpModule.get());
-- 
GitLab