2013-02-14 14:58:26 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
2013-02-14 14:58:26 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "python/pythoninterpreter.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/utility.h"
|
|
|
|
#include <boost/foreach.hpp>
|
2013-02-14 14:58:26 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-02-14 16:43:58 +01:00
|
|
|
PythonInterpreter::PythonInterpreter(const PythonLanguage::Ptr& language,
|
|
|
|
const Script::Ptr& script)
|
2013-02-14 14:58:26 +01:00
|
|
|
: ScriptInterpreter(script), m_Language(language), m_ThreadState(NULL)
|
|
|
|
{
|
|
|
|
PyEval_AcquireLock();
|
2013-02-16 07:49:36 +01:00
|
|
|
PythonInterpreter *interp = m_Language->GetCurrentInterpreter();
|
|
|
|
m_Language->SetCurrentInterpreter(this);
|
2013-02-14 14:58:26 +01:00
|
|
|
|
2013-02-16 07:49:36 +01:00
|
|
|
PyInterpreterState *pinterp = m_Language->GetMainThreadState()->interp;
|
|
|
|
m_ThreadState = PyThreadState_New(pinterp);
|
2013-02-14 14:58:26 +01:00
|
|
|
|
2013-02-14 15:51:50 +01:00
|
|
|
(void) PyThreadState_Swap(m_ThreadState);
|
2013-02-14 14:58:26 +01:00
|
|
|
PyRun_SimpleString(script->GetCode().CStr());
|
2013-02-14 16:43:58 +01:00
|
|
|
(void) PyThreadState_Swap(NULL);
|
2013-02-14 15:51:50 +01:00
|
|
|
|
2013-02-16 07:49:36 +01:00
|
|
|
m_Language->SetCurrentInterpreter(interp);
|
2013-02-14 15:51:50 +01:00
|
|
|
PyEval_ReleaseLock();
|
2013-02-14 14:58:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PythonInterpreter::~PythonInterpreter(void)
|
|
|
|
{
|
|
|
|
PyEval_AcquireLock();
|
|
|
|
|
2013-02-14 16:02:43 +01:00
|
|
|
(void) PyThreadState_Swap(NULL);
|
|
|
|
|
2013-02-14 14:58:26 +01:00
|
|
|
PyThreadState_Clear(m_ThreadState);
|
|
|
|
PyThreadState_Delete(m_ThreadState);
|
|
|
|
|
|
|
|
PyEval_ReleaseLock();
|
|
|
|
}
|
|
|
|
|
2013-02-15 16:34:33 +01:00
|
|
|
void PythonInterpreter::RegisterPythonFunction(const String& name, PyObject *function)
|
2013-02-14 16:43:58 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-02-14 16:43:58 +01:00
|
|
|
SubscribeFunction(name);
|
|
|
|
|
|
|
|
Py_INCREF(function);
|
|
|
|
m_Functions[name] = function;
|
|
|
|
}
|
|
|
|
|
2013-02-15 16:34:33 +01:00
|
|
|
void PythonInterpreter::UnregisterPythonFunction(const String& name)
|
2013-02-14 16:43:58 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-02-14 16:43:58 +01:00
|
|
|
UnsubscribeFunction(name);
|
|
|
|
|
|
|
|
m_Functions.erase(name);
|
|
|
|
}
|
|
|
|
|
2013-03-27 07:27:44 +01:00
|
|
|
Value PythonInterpreter::ProcessCall(const String& function, const std::vector<Value>& arguments)
|
2013-02-14 14:58:26 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-02-14 14:58:26 +01:00
|
|
|
PyEval_AcquireThread(m_ThreadState);
|
2013-02-16 07:49:36 +01:00
|
|
|
PythonInterpreter *interp = m_Language->GetCurrentInterpreter();
|
|
|
|
m_Language->SetCurrentInterpreter(this);
|
|
|
|
|
|
|
|
try {
|
2013-03-18 11:02:18 +01:00
|
|
|
std::map<String, PyObject *>::iterator it = m_Functions.find(function);
|
2013-02-16 07:49:36 +01:00
|
|
|
|
|
|
|
if (it == m_Functions.end())
|
2013-03-18 11:02:18 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Function '" + function + "' does not exist."));
|
2013-02-16 07:49:36 +01:00
|
|
|
|
|
|
|
PyObject *func = it->second;
|
|
|
|
|
|
|
|
PyObject *args = PyTuple_New(arguments.size());
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
BOOST_FOREACH(const Value& argument, arguments) {
|
|
|
|
PyObject *arg = PythonLanguage::MarshalToPython(argument);
|
|
|
|
PyTuple_SetItem(args, i, arg);
|
|
|
|
}
|
2013-02-14 14:58:26 +01:00
|
|
|
|
2013-02-16 07:49:36 +01:00
|
|
|
PyObject *result = PyObject_CallObject(func, args);
|
|
|
|
|
|
|
|
Py_DECREF(args);
|
|
|
|
|
2013-02-16 08:08:51 +01:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
PyObject *ptype, *pvalue, *ptraceback;
|
|
|
|
|
|
|
|
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
|
|
|
|
|
2013-02-16 11:28:34 +01:00
|
|
|
String msg = m_Language->ExceptionInfoToString(ptype, pvalue, ptraceback);
|
2013-02-16 08:08:51 +01:00
|
|
|
|
2013-02-16 11:28:34 +01:00
|
|
|
Py_XDECREF(ptype);
|
|
|
|
Py_XDECREF(pvalue);
|
|
|
|
Py_XDECREF(ptraceback);
|
2013-02-16 08:08:51 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Error in Python script: " + msg));
|
2013-02-16 07:49:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value vresult = PythonLanguage::MarshalFromPython(result);
|
|
|
|
Py_DECREF(result);
|
|
|
|
|
2013-03-27 07:27:44 +01:00
|
|
|
m_Language->SetCurrentInterpreter(interp);
|
|
|
|
PyEval_ReleaseThread(m_ThreadState);
|
|
|
|
|
|
|
|
return vresult;
|
2013-02-16 07:49:36 +01:00
|
|
|
} catch (...) {
|
2013-03-27 07:27:44 +01:00
|
|
|
m_Language->SetCurrentInterpreter(interp);
|
|
|
|
PyEval_ReleaseThread(m_ThreadState);
|
2013-02-16 07:49:36 +01:00
|
|
|
|
2013-03-27 07:27:44 +01:00
|
|
|
throw;
|
|
|
|
}
|
2013-02-14 14:58:26 +01:00
|
|
|
}
|