[Tutor] Embedding Python

Dj Gilcrease digitalxero at gmail.com
Sat Feb 24 11:50:41 CET 2007


I am attempting to embed Python in a C++ app and have a question
before I get too far into it.

Is is possible to to dynamically create an extension module?
eg, I want a module name spam and I have a stuct that contains all my
method names, and a pointer to the proper c++ function to call

### Suto Code ###
typedef struct _COMMAND {
	CHAR Name[64];
	fCommand Function;
	struct _COMMAND* pLast;
	struct _COMMAND* pNext;
} COMMAND, *PCOMMAND;
### End Sudo Code ###

What I would like to do is something like

### Sudo Code ###
class MyClass
{
        MyClass(PCOMMAND pCommand ) {pComd = pCommand;}
	static PyObject* spam_Func(PyObject *self, PyObject *args)
	{
		PCHAR sLine = NULL;
		if (!PyArg_ParseTuple(args, "z", &sLine))
			return NULL;
		pCmd->Function(sLine);
		Py_RETURN_NONE;
	}
        static PCOMMAND pCmd;
};

static PyMethodDef Methods[] = {0}

PCOMMAND pCommand = pCommands;
while(pCommand)
{
        MyClass* cmd = new MyClass(pCommand);
        Methods[pCommand->Name, cmd->spam_Func, METH_VARARGS, ""];
        pCommand = pCommand->pNext;
}

Methods[NULL, NULL, 0, NULL];

PyMODINIT_FUNC initspam(void)
{
    (void) Py_InitModule("spam", Methods);
}
### End Sudo Code ###

Now I know my Sudo code wont work or I wouldnt be asking, but is it
possible to do something like that. The reason I ask is there are
about 150 commands, plus plugins can add and remove commands when they
get loaded or unloaded so I will need to add / remove their commands
from the spam module as well.


More information about the Tutor mailing list