2012-01-03 11:40:09 +00:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2011 Alistair Riddoch
|
2012-01-03 14:17:10 +00:00
|
|
|
// 2001-2011 Python Software Foundation
|
2012-01-03 11:40:09 +00: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 Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
2012-01-03 14:17:10 +00:00
|
|
|
// Portions of this file have been copied from Python 2.6.7 and so this file
|
|
|
|
|
// is a derivative work. The derivative is distributed under the GPL as
|
|
|
|
|
// permitted by the license used by Python Software Foundation.
|
|
|
|
|
|
2012-01-03 11:40:09 +00:00
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
#include <Python-ast.h>
|
|
|
|
|
|
|
|
|
|
#include "python_testers.h"
|
|
|
|
|
|
2012-01-03 19:09:41 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2012-01-03 14:17:10 +00:00
|
|
|
// This function is directly lifted from Python 2.6.7, pythonrun.c. It
|
|
|
|
|
// forms part of the implementation of PyRun_SimpleStringFlags() as replicated
|
|
|
|
|
// below, with modifications to treat parse errors differently.
|
2012-01-03 11:40:09 +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);
|
2018-01-30 16:54:49 +01:00
|
|
|
if (co == nullptr)
|
|
|
|
|
return nullptr;
|
2018-05-04 19:17:55 +02:00
|
|
|
v = PyEval_EvalCode((PyObject*)co, globals, locals);
|
2012-01-03 11:40:09 +00:00
|
|
|
Py_DECREF(co);
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function is expanded out from the Python library function
|
|
|
|
|
// PyRun_SimpleStringFlags(). We do this so we can tell the difference,
|
|
|
|
|
// and fail in the case of a parse error in the code text. The unit tests
|
|
|
|
|
// do contain code which should fail, but never any code that fails because
|
|
|
|
|
// it won't parse.
|
2012-01-03 19:09:41 +00:00
|
|
|
int CyPyRun_SimpleString(const char * command, PyObject * exception)
|
2012-01-03 11:40:09 +00:00
|
|
|
{
|
2018-01-30 16:54:49 +01:00
|
|
|
PyCompilerFlags *flags = nullptr;
|
2012-01-03 11:40:09 +00:00
|
|
|
PyObject * m = PyImport_AddModule("__main__");
|
2018-01-30 16:54:49 +01:00
|
|
|
if (m == nullptr)
|
2012-01-03 11:40:09 +00:00
|
|
|
return -1;
|
|
|
|
|
PyObject * d = PyModule_GetDict(m);
|
|
|
|
|
// v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
|
|
|
|
|
PyArena *arena = PyArena_New();
|
2018-01-30 16:54:49 +01:00
|
|
|
if (arena == nullptr)
|
2012-02-11 13:45:36 +00:00
|
|
|
return -1;
|
2012-01-03 11:40:09 +00:00
|
|
|
|
|
|
|
|
mod_ty mod = PyParser_ASTFromString(command, "<string>", Py_file_input, flags, arena);
|
2018-01-30 16:54:49 +01:00
|
|
|
if (mod == nullptr) {
|
2012-01-03 11:40:09 +00:00
|
|
|
PyArena_Free(arena);
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyObject *ret = run_mod(mod, "<string>", d, d, flags, arena);
|
|
|
|
|
PyArena_Free(arena);
|
|
|
|
|
|
2018-01-30 16:54:49 +01:00
|
|
|
if (ret == nullptr) {
|
2012-01-03 19:09:41 +00:00
|
|
|
int errcode = -1;
|
|
|
|
|
if (exception != 0) {
|
|
|
|
|
if (PyErr_ExceptionMatches(exception)) {
|
|
|
|
|
errcode = -3;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-03 11:40:09 +00:00
|
|
|
PyErr_Print();
|
2012-01-03 19:09:41 +00:00
|
|
|
return errcode;
|
2012-01-03 11:40:09 +00:00
|
|
|
}
|
|
|
|
|
Py_DECREF(ret);
|
2018-05-04 19:17:55 +02:00
|
|
|
|
|
|
|
|
PyObject *f = PySys_GetObject("stdout");
|
|
|
|
|
if (PyFile_WriteString("\n", f))
|
2012-01-03 11:40:09 +00:00
|
|
|
PyErr_Clear();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|