2011-11-08 20:43:13 +00:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2011 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-12-15 11:28:36 +00:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2011-12-19 21:41:18 +00:00
|
|
|
#include "python_testers.h"
|
|
|
|
|
|
2011-11-08 22:51:25 +00:00
|
|
|
#include "rulesets/PythonClass.h"
|
2018-07-24 21:18:50 +02:00
|
|
|
#include "external/pycxx/CXX/Extensions.hxx"
|
2011-11-08 20:43:13 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
Py::Object* stub_Get_PyClass_return = nullptr;
|
|
|
|
|
Py::Module* stub_Get_PyModule_return = nullptr;
|
|
|
|
|
|
2012-03-04 18:05:17 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
class TestPythonClass : public PythonClass
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TestPythonClass(const std::string& p, const std::string& t) :
|
|
|
|
|
PythonClass(p, t)
|
|
|
|
|
{}
|
2011-12-19 21:41:18 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
virtual ~TestPythonClass()
|
|
|
|
|
{}
|
2011-11-08 22:51:25 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
virtual int check() const
|
|
|
|
|
{ return 0; }
|
2011-11-08 22:51:25 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
int test_getClass(Py::Module o)
|
|
|
|
|
{ return getClass(o); }
|
2011-11-08 22:51:25 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
int test_load()
|
|
|
|
|
{ return load(); }
|
2011-11-08 22:51:25 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
int test_refresh()
|
|
|
|
|
{ return refresh(); }
|
2011-11-08 22:51:25 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
const std::string& access_package()
|
|
|
|
|
{ return m_package; }
|
2011-11-08 22:51:25 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
const std::string& access_type()
|
|
|
|
|
{ return m_type; }
|
|
|
|
|
|
|
|
|
|
Py::Module& access_module()
|
|
|
|
|
{ return m_module; }
|
|
|
|
|
|
|
|
|
|
boost::optional<Py::Callable>& access_class()
|
|
|
|
|
{ return m_class; }
|
2011-12-19 21:41:18 +00:00
|
|
|
};
|
|
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
struct TestMod : public Py::ExtensionModule<TestMod>
|
|
|
|
|
{
|
|
|
|
|
TestMod() : ExtensionModule("testmod")
|
|
|
|
|
{
|
|
|
|
|
initialize("testmod");
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-05-04 19:17:55 +02:00
|
|
|
|
|
|
|
|
|
2011-11-08 20:43:13 +00:00
|
|
|
int main()
|
|
|
|
|
{
|
2018-07-24 21:18:50 +02:00
|
|
|
PyImport_AppendInittab("testmod", []() {
|
|
|
|
|
auto module = new TestMod();
|
|
|
|
|
return module->module().ptr();
|
|
|
|
|
});
|
2018-05-04 19:17:55 +02:00
|
|
|
|
2011-12-19 21:41:18 +00:00
|
|
|
Py_Initialize();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
run_python_string("import testmod");
|
|
|
|
|
|
|
|
|
|
run_python_string("class BadClass:\n"
|
|
|
|
|
" pass\n");
|
|
|
|
|
run_python_string("class GoodClass(object):\n"
|
|
|
|
|
" pass\n");
|
|
|
|
|
run_python_string("testmod.BadClass=BadClass");
|
2012-03-04 18:05:17 +00:00
|
|
|
run_python_string("testmod.GoodClass=GoodClass");
|
|
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
Py::Module testmod("testmod");
|
|
|
|
|
auto good_class = Py::Callable(testmod.getAttr("GoodClass"));
|
2011-12-19 21:41:18 +00:00
|
|
|
|
2011-11-08 22:51:25 +00:00
|
|
|
{
|
2018-07-24 21:18:50 +02:00
|
|
|
const char* package = "acfd44fd-dccb-4a63-98c3-6facd580ca5f";
|
|
|
|
|
const char* type = "3265e96a-28a0-417c-ad30-2970c1777c50";
|
2011-11-08 22:51:25 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
TestPythonClass* pc = new TestPythonClass(package, type);
|
2011-11-08 22:51:25 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
assert(pc != nullptr);
|
2011-11-08 22:51:25 +00:00
|
|
|
|
|
|
|
|
assert(pc->access_package() == package);
|
|
|
|
|
assert(pc->access_type() == type);
|
|
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
assert(pc->access_module().isNull());
|
|
|
|
|
assert(!pc->access_class());
|
2011-11-08 22:51:25 +00:00
|
|
|
|
|
|
|
|
delete pc;
|
|
|
|
|
}
|
2011-12-19 21:41:18 +00:00
|
|
|
|
|
|
|
|
{
|
2018-07-24 21:18:50 +02:00
|
|
|
const char* package = "acfd44fd-dccb-4a63-98c3-6facd580ca5f";
|
|
|
|
|
const char* type = "3265e96a-28a0-417c-ad30-2970c1777c50";
|
2011-12-19 21:41:18 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
TestPythonClass* pc = new TestPythonClass(package, type);
|
2011-12-19 21:41:18 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
stub_Get_PyModule_return = nullptr;
|
|
|
|
|
stub_Get_PyClass_return = nullptr;
|
2011-12-19 21:41:18 +00:00
|
|
|
|
|
|
|
|
int ret = pc->test_load();
|
|
|
|
|
|
|
|
|
|
assert(ret == -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2018-07-24 21:18:50 +02:00
|
|
|
const char* package = "acfd44fd-dccb-4a63-98c3-6facd580ca5f";
|
|
|
|
|
const char* type = "3265e96a-28a0-417c-ad30-2970c1777c50";
|
2011-12-19 21:41:18 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
TestPythonClass* pc = new TestPythonClass(package, type);
|
2011-12-19 21:41:18 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
stub_Get_PyModule_return = &testmod;
|
|
|
|
|
stub_Get_PyClass_return = nullptr;
|
2011-12-19 21:41:18 +00:00
|
|
|
|
|
|
|
|
int ret = pc->test_load();
|
|
|
|
|
|
|
|
|
|
assert(ret == -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2018-07-24 21:18:50 +02:00
|
|
|
const char* package = "acfd44fd-dccb-4a63-98c3-6facd580ca5f";
|
|
|
|
|
const char* type = "3265e96a-28a0-417c-ad30-2970c1777c50";
|
2011-12-19 21:41:18 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
TestPythonClass* pc = new TestPythonClass(package, type);
|
2011-12-19 21:41:18 +00:00
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
stub_Get_PyModule_return = &testmod;
|
|
|
|
|
stub_Get_PyClass_return = &good_class;
|
2011-12-19 21:41:18 +00:00
|
|
|
|
|
|
|
|
int ret = pc->test_load();
|
|
|
|
|
|
|
|
|
|
assert(ret == 0);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-08 20:43:13 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stubs
|
|
|
|
|
|
|
|
|
|
#include "common/log.h"
|
|
|
|
|
|
|
|
|
|
#include "rulesets/Python_Script_Utils.h"
|
|
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
void log(LogLevel lvl, const std::string& msg)
|
2011-11-08 20:43:13 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
Py::Object Get_PyClass(const Py::Module& module,
|
|
|
|
|
const std::string & package,
|
|
|
|
|
const std::string & type)
|
2011-11-08 20:43:13 +00:00
|
|
|
{
|
2011-12-19 21:41:18 +00:00
|
|
|
if (stub_Get_PyClass_return) {
|
2018-07-24 21:18:50 +02:00
|
|
|
return *stub_Get_PyClass_return;
|
2011-12-19 21:41:18 +00:00
|
|
|
}
|
2018-07-24 21:18:50 +02:00
|
|
|
return Py::Null();
|
2011-11-08 20:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-24 21:18:50 +02:00
|
|
|
Py::Module Get_PyModule(const std::string& package)
|
2011-11-08 20:43:13 +00:00
|
|
|
{
|
2011-12-19 21:41:18 +00:00
|
|
|
if (stub_Get_PyModule_return) {
|
2018-07-24 21:18:50 +02:00
|
|
|
return *stub_Get_PyModule_return;
|
2011-12-19 21:41:18 +00:00
|
|
|
}
|
2018-07-24 21:18:50 +02:00
|
|
|
return Py::Module(nullptr);
|
2011-11-08 20:43:13 +00:00
|
|
|
}
|