[C++-sig] Re: Problem with embedding python with boost.python

Dirk Gerrits dirk at gerrits.homeip.net
Mon Jul 7 12:10:26 CEST 2003

Pierre Barbier de Reuille wrote:

> I copied it just because the PyRun_String command take a "char *" and 
> that latin1 returns a "const char*" ... si you have to copy it !

Well PyRun_String doesn't actually modify your string. In fact, in the 
Python CVS it takes a const char* as an argument. For now, you can just 
use const_cast<char*>(command.latin1()) as an argument, no copying 

> For my other problem, I just solved it ... the solution is to replace 
> the AppendInittab by :
> PyImport_AppendInittab("editor", initeditor)
> and ti define initeditor with :
> extern "C" void initeditor();
> ... when testing I forgot the 'extern "C"' !
> Now it works and I'm working on your recommandations to retrieve the 
> output !
> Tanks !

You're welcome.

By the way, there are plans to make embedding with Boost.Python 
significantly easier. I'm not sure when they will be fully implemented, 
but hopefully before the end of this summer. Any new developments will 
be posted here on the C++ SIG.

> Dirk Gerrits wrote:
>> Dirk Gerrits wrote:
>>> command.latin1() is not null-terminated I think, which would make the 
>>> strcpy fail. (Not sure, I don't use Qt.) Wouldn't it be easier and 
>>> safer to do:
>>> QString cmd = command + '\0';
>>> ...
>>> PyObject *obj = PyRun_String(cmd.latin1(), Py_file_input,
>>>     main_namespace.ptr(), main_namespace.ptr());
>>> ?
>> Ah I just read that latin1() DOES provide a null-terminated string. 
>> Sorry.
>> But then I don't understand why you were copying it in the first 
>> place. Care to elaborate?
>> Regards,
>> Dirk Gerrits

More information about the Cplusplus-sig mailing list