469,610 Members | 2,055 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Extending an embeded Python

I'm trying to embed Python in a Windows exe, and extend it with some
functions in the same program. So far I only add one function:
<file.h>
static PyObject* py_print( PyObject* self, PyObject* args ) {
const char* msg;
if( !PyArg_ParseTuple( args, "s", &msg ) )
return 0;
EventLog::log( msg );
Py_INCREF( Py_None );
return Py_None;
}
static PyMethodDef StoneAgeMethods[] = {
{ "log", py_print, METH_VARARGS, "Print a message to the log" },
{ 0, 0, 0, 0 } /* sentinel */
};
<file.cpp>
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int ) {
EventLog::init( "con_dump.txt" );
Py_Initialize();
EventLog::log( "Python version '%s'", Py_GetVersion() );
PyObject* res0 = Py_InitModule( "stoneage", StoneAgeMethods );
res1 = PyRun_SimpleString( "stoneage.log( \"test\" )\n" ); // FAIL
res2 = PyRun_SimpleString( "log( \"test\" )\n" ); // FAIL
res3 = PyRun_SimpleString( "print \"test\"\n" ); // OK
Py_Finalize();
}
This compiles without problems, but when I run it I can't use the "log"
function. Result res0 is a non-null object.
As far as I can understand the "Extending and Embedding the Python
Interpreter" doc, res1 should work but it doesn't!
I'm a total newbie to Python, so any help appreciated :)
Jul 18 '05 #1
1 1363
Mikin von Flap wrote:
PyObject* res0 = Py_InitModule( "stoneage", StoneAgeMethods );
res1 = PyRun_SimpleString( "stoneage.log( \"test\" )\n" ); // FAIL
res2 = PyRun_SimpleString( "log( \"test\" )\n" ); // FAIL
res3 = PyRun_SimpleString( "print \"test\"\n" ); // OK
Py_Finalize(); This compiles without problems, but when I run it I can't use the "log"
function. Result res0 is a non-null object.
As far as I can understand the "Extending and Embedding the Python
Interpreter" doc, res1 should work but it doesn't!


compare and contrast:

C:\Python24>python
Python 2.4 (#60, Nov 30 2004, 11:49:19)
stoneage.log("hello") Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'stoneage' is not defined sys.platform Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'sys' is not defined import sys
sys.platform

'win32'

</F>

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jens Thiede | last post: by
4 posts views Thread by Alicia Haumann | last post: by
1 post views Thread by Richard Townsend | last post: by
3 posts views Thread by Marco Meoni | last post: by
5 posts views Thread by vbgunz | last post: by
1 post views Thread by jeremito | last post: by
reply views Thread by fabien.lyon | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.