From 75dab66916f277b950554dc694c7f08a8445cc15 Mon Sep 17 00:00:00 2001 From: Eivind Kvedalen Date: Tue, 3 Feb 2015 00:32:00 +0100 Subject: [PATCH] Added InterpreterSingleton::getValue(...) function. --- src/Base/Interpreter.cpp | 22 ++++++++++++++++++++++ src/Base/Interpreter.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index 9d0617c5e616..e3df2662bdc7 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -496,6 +496,28 @@ void InterpreterSingleton::runMethod(PyObject *pobject, const char *method, } } +PyObject * InterpreterSingleton::getValue(const char * key, const char * result_var) +{ + PyObject *module, *dict, *presult; /* "exec code in d, d" */ + + PyGILStateLocker locker; + module = PP_Load_Module("__main__"); /* get module, init python */ + if (module == NULL) + throw PyException(); /* not incref'd */ + dict = PyModule_GetDict(module); /* get dict namespace */ + if (dict == NULL) + throw PyException(); /* not incref'd */ + + + presult = PyRun_String(key, Py_file_input, dict, dict); /* eval direct */ + if (!presult) { + throw PyException(); + } + Py_DECREF(presult); + + return PyObject_GetAttrString(module, result_var); +} + void InterpreterSingleton::dbgObserveFile(const char* sFileName) { if (sFileName) diff --git a/src/Base/Interpreter.h b/src/Base/Interpreter.h index a5b6ac9e9374..2a1497503a23 100644 --- a/src/Base/Interpreter.h +++ b/src/Base/Interpreter.h @@ -242,6 +242,8 @@ class BaseExport InterpreterSingleton static const std::string strToPython(const std::string &Str){return strToPython(Str.c_str());} //@} + PyObject *getValue(const char *key, const char *result_var); + protected: // singleton static InterpreterSingleton *_pcSingelton;