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
|
|
|
|
|
|
2021-05-27 17:38:58 +02:00
|
|
|
#include "pycxx/CXX/Objects.hxx"
|
|
|
|
|
|
2012-01-03 11:40:09 +00:00
|
|
|
|
|
|
|
|
#include "python_testers.h"
|
|
|
|
|
|
2012-01-03 19:09:41 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2021-05-27 17:38:58 +02:00
|
|
|
#include <Python.h>
|
2012-01-03 11:40:09 +00:00
|
|
|
|
|
|
|
|
// 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.
|
2021-09-28 23:19:10 +02:00
|
|
|
int CyPyRun_SimpleString(const char* command, PyObject* exception)
|
2012-01-03 11:40:09 +00:00
|
|
|
{
|
2021-09-28 23:19:10 +02:00
|
|
|
Py::Module m("__main__");
|
|
|
|
|
if (m.isNull()) {
|
2012-01-03 11:40:09 +00:00
|
|
|
return -1;
|
2021-09-28 23:19:10 +02:00
|
|
|
}
|
2021-05-27 17:38:58 +02:00
|
|
|
Py::Dict d = m.getDict();
|
2012-01-03 11:40:09 +00:00
|
|
|
|
2021-09-28 23:19:10 +02:00
|
|
|
Py::String filename("<string>");
|
|
|
|
|
Py::Object parsed(Py_CompileStringObject(command, *filename, Py_file_input, nullptr, 0), true);
|
|
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
2012-01-03 11:40:09 +00:00
|
|
|
PyErr_Print();
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
2021-09-28 23:19:10 +02:00
|
|
|
Py::Object ret(PyEval_EvalCode(*parsed, *d, *d), true);
|
2012-01-03 11:40:09 +00:00
|
|
|
|
2021-09-28 23:19:10 +02:00
|
|
|
if (ret.isNull()) {
|
2012-01-03 19:09:41 +00:00
|
|
|
int errcode = -1;
|
2018-07-23 16:42:22 +02:00
|
|
|
if (exception != nullptr) {
|
2012-01-03 19:09:41 +00:00
|
|
|
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
|
|
|
}
|
2018-05-04 19:17:55 +02:00
|
|
|
|
2021-09-28 23:19:10 +02:00
|
|
|
PyObject* f = PySys_GetObject("stdout");
|
|
|
|
|
if (PyFile_WriteString((std::string("Exec: ") + command + "\n").c_str(), f)) {
|
2012-01-03 11:40:09 +00:00
|
|
|
PyErr_Clear();
|
2021-09-28 23:19:10 +02:00
|
|
|
}
|
2012-01-03 11:40:09 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|