473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1444
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jens Thiede | last post by:
I want to use python heavily on my embeded linux. What would the best way be to slim down Python. Only keep those modules you would acctually need, and not imparing Python's capabilities? ...
4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
1
by: Richard Townsend | last post by:
In the "Extending and Embedding" part of the Python documentation: section 5.4 "Extending Embedded Python" - it describes how to use a Python extension module from Python that is embedded in a C...
3
by: Marco Meoni | last post by:
Hi all! I've a problem with a C++ class that has to be included in a python application. One way to do it is Extending and Embedding the Python Interpreter Now i have 2 questions 1) Is there a...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
1
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples...
0
by: fabien.lyon | last post by:
hello, The C++ application uses a python module which wraps commands set for CVS management: checkout, checkin and tag. We used python2.5.1 and Visual C++ 6.0 The problem we get is: After a...
0
by: fabien.lyon | last post by:
-----Message d'origine----- De : fabien.lyon Envoyé : mercredi 30 mai 2007 20:16 À : 'python-list@python.org' Objet : RE: embeded python progam into visual C++ application crash 2.5.1 is...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.