2009-01-31 04:08:54 +00:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2009 Alistair Riddoch
|
|
|
|
|
//
|
|
|
|
|
// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
2011-12-30 01:19:26 +00:00
|
|
|
#include <Python-ast.h>
|
2009-01-31 04:08:54 +00:00
|
|
|
|
|
|
|
|
#include "PythonContext.h"
|
|
|
|
|
|
2011-12-30 00:17:43 +00:00
|
|
|
#include "common/log.h"
|
|
|
|
|
|
2009-01-31 04:08:54 +00:00
|
|
|
PythonContext::PythonContext()
|
|
|
|
|
{
|
2015-04-06 21:41:43 +02:00
|
|
|
m_module = PyImport_AddModule("__main__");
|
|
|
|
|
if (m_module == NULL) {
|
2011-12-30 00:17:43 +00:00
|
|
|
log(ERROR, "Could not import __main__");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-04-06 21:41:43 +02:00
|
|
|
m_globals = PyModule_GetDict(m_module);
|
2011-12-30 00:17:43 +00:00
|
|
|
m_locals = PyDict_New();
|
2011-12-30 01:19:26 +00:00
|
|
|
m_arena = PyArena_New();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 21:41:43 +02:00
|
|
|
PythonContext::~PythonContext()
|
|
|
|
|
{
|
|
|
|
|
PyArena_Free(m_arena);
|
|
|
|
|
//PyObject_Free(m_locals);
|
|
|
|
|
//PyObject_Free(m_module);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-30 01:19:26 +00:00
|
|
|
static PyObject *
|
|
|
|
|
run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
|
|
|
|
|
PyCompilerFlags *flags, PyArena *arena)
|
|
|
|
|
{
|
|
|
|
|
PyCodeObject *co;
|
|
|
|
|
PyObject *v;
|
|
|
|
|
co = PyAST_Compile(mod, filename, flags, arena);
|
|
|
|
|
if (co == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
|
|
|
|
Py_DECREF(co);
|
|
|
|
|
return v;
|
2009-01-31 04:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string PythonContext::runCommand(const std::string & s)
|
|
|
|
|
{
|
2011-12-30 01:19:26 +00:00
|
|
|
// This is expanded from PyRun_SimpleString in the Python library
|
|
|
|
|
// so that we can report errors better at the parsing stage
|
|
|
|
|
PyObject *ret = NULL;
|
|
|
|
|
mod_ty mod;
|
|
|
|
|
|
|
|
|
|
mod = PyParser_ASTFromString(s.c_str(),
|
|
|
|
|
"<string>",
|
|
|
|
|
Py_single_input,
|
|
|
|
|
NULL,
|
|
|
|
|
m_arena);
|
|
|
|
|
if (mod == NULL) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
return "[parseerror]";
|
|
|
|
|
}
|
|
|
|
|
ret = run_mod(mod, "<string>", m_globals, m_locals, NULL, m_arena);
|
|
|
|
|
if (ret == NULL) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
return "ERROR";
|
|
|
|
|
}
|
|
|
|
|
PyObject * repr = PyObject_Repr(ret);
|
|
|
|
|
if (repr == 0) {
|
|
|
|
|
return "[undecodable]";
|
|
|
|
|
}
|
|
|
|
|
return PyString_AsString(repr);
|
|
|
|
|
|
|
|
|
|
#if 0
|
2011-12-30 00:17:43 +00:00
|
|
|
PyObject * res = PyRun_String(s.c_str(),
|
|
|
|
|
Py_single_input,
|
|
|
|
|
m_globals,
|
|
|
|
|
m_locals);
|
2009-01-31 04:08:54 +00:00
|
|
|
if (res == 0) {
|
2009-02-09 05:33:39 +00:00
|
|
|
if (PyErr_Occurred() == 0) {
|
|
|
|
|
return "WHAT";
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
return "ERROR";
|
|
|
|
|
}
|
2009-01-31 04:08:54 +00:00
|
|
|
}
|
|
|
|
|
PyObject * repr = PyObject_Repr(res);
|
|
|
|
|
if (repr == 0) {
|
|
|
|
|
return "[undecodable]";
|
|
|
|
|
}
|
|
|
|
|
return PyString_AsString(repr);
|
2011-12-30 01:19:26 +00:00
|
|
|
#endif
|
2009-01-31 04:08:54 +00:00
|
|
|
}
|