2009-11-15 21:51:34 +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
|
|
|
|
|
|
|
|
|
|
|
2011-02-15 09:30:46 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-11-15 21:51:34 +00:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
#include "python_testers.h"
|
|
|
|
|
|
2009-11-15 21:51:34 +00:00
|
|
|
#include "rulesets/PythonArithmeticScript.h"
|
|
|
|
|
|
2010-09-01 18:00:26 +01:00
|
|
|
#include "common/compose.hpp"
|
|
|
|
|
#include "common/log.h"
|
|
|
|
|
|
2009-11-15 21:51:34 +00:00
|
|
|
#include <cassert>
|
2018-05-04 19:17:55 +02:00
|
|
|
#include <rulesets/Python_Script_Utils.h>
|
2009-11-15 21:51:34 +00:00
|
|
|
|
|
|
|
|
static PyMethodDef no_methods[] = {
|
2018-01-30 16:54:49 +01:00
|
|
|
{nullptr, nullptr} /* Sentinel */
|
2009-11-15 21:51:34 +00:00
|
|
|
};
|
|
|
|
|
|
2010-09-01 18:00:26 +01:00
|
|
|
PyObject * Get_PyClass(PyObject * module,
|
|
|
|
|
const std::string & package,
|
|
|
|
|
const std::string & type);
|
|
|
|
|
|
2018-05-04 19:17:55 +02:00
|
|
|
PyObject * Get_PyModule(const std::string & package)
|
|
|
|
|
{
|
|
|
|
|
PyObject * package_name = PyUnicode_FromString((char *)package.c_str());
|
|
|
|
|
PyObject * module = PyImport_Import(package_name);
|
|
|
|
|
Py_DECREF(package_name);
|
|
|
|
|
return module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyObject* init_testmod() {
|
|
|
|
|
static struct PyModuleDef def = {
|
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
|
"testmod",
|
|
|
|
|
nullptr,
|
|
|
|
|
0,
|
|
|
|
|
no_methods,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return PyModule_Create(&def);
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-15 21:51:34 +00:00
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
{
|
2018-05-04 19:17:55 +02:00
|
|
|
PyImport_AppendInittab("testmod", &init_testmod);
|
|
|
|
|
Py_Initialize();
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
run_python_string("import testmod");
|
|
|
|
|
run_python_string("class TestArithmeticScript(object):\n"
|
|
|
|
|
" def __init__(self):\n"
|
|
|
|
|
" self.foo=1\n"
|
|
|
|
|
" self.bar=1.1\n"
|
|
|
|
|
" self.baz=None\n"
|
|
|
|
|
" self.qux='1'\n"
|
|
|
|
|
);
|
|
|
|
|
run_python_string("testmod.TestArithmeticScript=TestArithmeticScript");
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2018-05-04 19:17:55 +02:00
|
|
|
auto testmod = Get_PyModule("testmod");
|
|
|
|
|
|
|
|
|
|
assert(testmod);
|
|
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
PyObject * clss = Get_PyClass(testmod, "testmod", "TestArithmeticScript");
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
assert(clss != 0);
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
PyObject * instance = PyEval_CallFunction(clss,"()");
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
assert(instance != 0);
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
PythonArithmeticScript pas(instance);
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
float val;
|
|
|
|
|
pas.attribute("foo", val);
|
|
|
|
|
pas.attribute("bar", val);
|
|
|
|
|
pas.attribute("baz", val);
|
|
|
|
|
pas.attribute("qux", val);
|
|
|
|
|
pas.attribute("nonexistent", val);
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2014-04-27 20:56:16 +02:00
|
|
|
pas.set("foo", 1.0f);
|
|
|
|
|
pas.set("mim", 1.0f);
|
|
|
|
|
}
|
2009-11-15 21:51:34 +00:00
|
|
|
|
2010-09-01 18:00:26 +01:00
|
|
|
Py_Finalize();
|
2009-11-15 21:51:34 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2010-09-01 18:00:26 +01:00
|
|
|
|
|
|
|
|
// stubs
|
|
|
|
|
|
|
|
|
|
PyObject * Get_PyClass(PyObject * module,
|
|
|
|
|
const std::string & package,
|
|
|
|
|
const std::string & type)
|
|
|
|
|
{
|
|
|
|
|
PyObject * py_class = PyObject_GetAttrString(module, (char *)type.c_str());
|
2018-01-30 16:54:49 +01:00
|
|
|
if (py_class == nullptr) {
|
2010-09-01 18:00:26 +01:00
|
|
|
log(ERROR, String::compose("Could not find python class \"%1.%2\"",
|
|
|
|
|
package, type));
|
|
|
|
|
PyErr_Print();
|
2018-01-30 16:54:49 +01:00
|
|
|
return nullptr;
|
2010-09-01 18:00:26 +01:00
|
|
|
}
|
|
|
|
|
if (PyCallable_Check(py_class) == 0) {
|
|
|
|
|
log(ERROR, String::compose("Could not instance python class \"%1.%2\"",
|
|
|
|
|
package, type));
|
|
|
|
|
Py_DECREF(py_class);
|
2018-01-30 16:54:49 +01:00
|
|
|
return nullptr;
|
2010-09-01 18:00:26 +01:00
|
|
|
}
|
|
|
|
|
if (PyType_Check(py_class) == 0) {
|
|
|
|
|
log(ERROR, String::compose("PyCallable_Check returned true, "
|
|
|
|
|
"but PyType_Check returned false \"%1.%2\"",
|
|
|
|
|
package, type));
|
|
|
|
|
Py_DECREF(py_class);
|
2018-01-30 16:54:49 +01:00
|
|
|
return nullptr;
|
2010-09-01 18:00:26 +01:00
|
|
|
}
|
|
|
|
|
return py_class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArithmeticScript::~ArithmeticScript()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void log(LogLevel lvl, const std::string & msg)
|
|
|
|
|
{
|
|
|
|
|
}
|