Extending via C++, adding a module throws exception

All other Source.Python topics and issues.
Chrisber
Junior Member
Posts: 21
Joined: Thu Jul 19, 2012 10:25 pm

Postby Chrisber » Thu Oct 30, 2014 3:50 pm

Of course. I commented the SDK_OnLoad function.

Syntax: Select all

static int numargs = 0;

static PyObject* pawnbridge_numargs(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":numargs"))
return NULL;
return PyLong_FromLong(numargs);
}

static PyMethodDef PawnBridgeMethods[] = {
{ "numargs", pawnbridge_numargs, METH_VARARGS,
"Return the number of arguments received by the process." },
{ NULL, NULL, 0, NULL }
};

static PyModuleDef PawnBridgeModule = {
PyModuleDef_HEAD_INIT, "pawnbridge", NULL, -1, PawnBridgeMethods,
NULL, NULL, NULL, NULL
};

PyMODINIT_FUNC PyInit_pawnbridge(void)
{
return PyModule_Create(&PawnBridgeModule);
}



/**
* @brief This is called after the initial loading sequence has been processed.
*
* @param error Error message buffer.
* @param maxlength Size of error message buffer.
* @param late Whether or not the module was loaded after map load.
* @return True to succeed loading, false to fail.
*/
bool PawnBridge::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
numargs = 42;

// also doesn't work when I uncomment it
//PyImport_AddModule("pawnbridge");

// test to make sure that dll is loaded; return value is a valid handle (also works when commenting out)
smutils->LogMessage(myself, "LoadLibrary: %d", LoadLibrary("D:\\C++\\Source\\SrcDS\\games\\css\\cstrike\\addons\\source-python\\Python3\\plat-win\\python34.dll"));

// try to add the dll to the sys.path, no effect
PyRun_SimpleString("sys.path.insert(1, r'D:\\C++\\Source\\SrcDS\\games\\css\\cstrike\\addons\\sourcemod\\extensions')");

// print out a simple thing. works.
PyRun_SimpleString("print('lolxd')");

// works. includes also source-python paths
PyRun_SimpleString("print(str(sys.path))");

// try again to add the path. no effect
std::wstring x = Py_GetPath();
x += L";D:\\C++\\Source\\SrcDS\\games\\css\\cstrike\\addons\\sourcemod\\extensions";
Py_SetPath(x.c_str());

// try using this, out of documentation. when removing this, "no module named pawnbridge" appears, otherwise "pawnbridge is no built-in module"
smutils->LogMessage(myself, "PyImport_AppendInittab: %d", PyImport_AppendInittab("pawnbridge", &PyInit_pawnbridge));

// try additionally to call the init func manually, no effect
PyModule_Create(&PawnBridgeModule);

// try to import it inside the dll itself. fails.
PyImport_ImportModule("pawnbridge");

// initialize
Py_Initialize();

// try to output something with source-python. works
PyRun_SimpleString("from core import echo_console; echo_console('loltest');");

return true;
}


Test script:

Syntax: Select all

import pawnbridge
from core import echo_console

print(dir(pawnbridge))

echo_console('This is a test: {:d}'.format(pawnbridge.numargs()))
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Thu Oct 30, 2014 4:00 pm

You really confuse me! I thought you were using Boost.Python now?!

Try this:

Syntax: Select all

char const* greet()
{
return "hello, world";
}

void PyInit_pawnbridge()
{
using namespace boost::python;
def("greet", greet);
}

bool PawnBridge::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
// Create a new empty module
object newmodule(borrowed(PyImport_AddModule("pawnbridge")));

// Set it as the current scope
scope moduleScope = newmodule;

// Initialize the module
PyInit_pawnbridge()

return true;
}
Chrisber
Junior Member
Posts: 21
Joined: Thu Jul 19, 2012 10:25 pm

Postby Chrisber » Thu Oct 30, 2014 4:09 pm

Hi, sorry! boost.python didn't even get contact to Source.Pythons python instance. So I firstly tried to use the plain API.
Holy sh........ it works.
Time of life wasted: four hours.

Thank you...
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Thu Oct 30, 2014 4:13 pm

No, that's not wasted time. That's four hours of new expirience. :D

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 63 guests