Skip to content
Snippets Groups Projects
Commit 73665ae6 authored by AlQuemist's avatar AlQuemist
Browse files

PyInterpreter: minor cleanup and improvements

parent ad03e951
No related branches found
No related tags found
1 merge request!2755Improve the construction of a 'Sample' from a Python script
...@@ -151,8 +151,7 @@ PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::s ...@@ -151,8 +151,7 @@ PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::s
{ {
ASSERT(!pymodule_name.empty()); ASSERT(!pymodule_name.empty());
if (!path.empty()) addPythonPath(path);
addPythonPath(path);
// import the module // import the module
PyObject* pymodule = PyImport_ImportModule(pymodule_name.c_str()); PyObject* pymodule = PyImport_ImportModule(pymodule_name.c_str());
...@@ -170,6 +169,10 @@ PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::s ...@@ -170,6 +169,10 @@ PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::s
// Python stable ABI // Python stable ABI
void PyInterpreter::DecRef(PyObject* py_object) void PyInterpreter::DecRef(PyObject* py_object)
{ {
// Decrementing Python reference-count without
// Python initialized leads to memory access violation (segmentation fault)
ASSERT(PyInterpreter::isInitialized());
Py_XDECREF(py_object); Py_XDECREF(py_object);
} }
...@@ -225,7 +228,6 @@ void PyInterpreter::callFunction(PyObject* py_module, const std::string& fn_name ...@@ -225,7 +228,6 @@ void PyInterpreter::callFunction(PyObject* py_module, const std::string& fn_name
Py_DecRef(pFunc); Py_DecRef(pFunc);
} }
std::string PyInterpreter::runtimeInfo() std::string PyInterpreter::runtimeInfo()
{ {
std::string result; std::string result;
...@@ -305,8 +307,7 @@ std::string PyInterpreter::errorDescription(const std::string& title) ...@@ -305,8 +307,7 @@ std::string PyInterpreter::errorDescription(const std::string& title)
PyObjectPtr PyInterpreter::BornAgain::import(const std::string& path) PyObjectPtr PyInterpreter::BornAgain::import(const std::string& path)
{ {
if (!path.empty()) PyInterpreter::addPythonPath(path);
PyInterpreter::addPythonPath(path);
#ifndef _WIN32 #ifndef _WIN32
// store ctrl-C handler before Numpy messes it up // store ctrl-C handler before Numpy messes it up
...@@ -389,6 +390,10 @@ std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::st ...@@ -389,6 +390,10 @@ std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::st
{ {
PyObjectPtr tmpModule{PyInterpreter::BornAgain::importScript(script, path)}; PyObjectPtr tmpModule{PyInterpreter::BornAgain::importScript(script, path)};
if (!tmpModule.valid())
throw std::runtime_error("PyInterpreter::BornAgain: "
"Cannot obtain the dictionary from the script module");
PyObject* pDict = PyModule_GetDict(tmpModule.get()); PyObject* pDict = PyModule_GetDict(tmpModule.get());
if (!pDict) { if (!pDict) {
checkError(); checkError();
...@@ -402,11 +407,13 @@ std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::st ...@@ -402,11 +407,13 @@ std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::st
while (PyDict_Next(pDict, &pos, &key, &value)) { while (PyDict_Next(pDict, &pos, &key, &value)) {
if (PyCallable_Check(value)) { if (PyCallable_Check(value)) {
std::string func_name{PyInterpreter::pyStrtoString(key)}; std::string func_name{PyInterpreter::pyStrtoString(key)};
// do not import callables whose names begin with '__'
if (func_name.find("__") == std::string::npos) if (func_name.find("__") == std::string::npos)
fn_names.push_back(func_name); fn_names.push_back(func_name);
} }
} }
PyDict_Clear(pDict); // do not accumulate history between imports
Py_DecRef(pDict);
return fn_names; return fn_names;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment