[C++-sig] Re: boost:python:embedding

Dirk Gerrits dirk at gerrits.homeip.net
Sun May 4 13:15:42 CEST 2003

David Abrahams wrote:
> Dirk Gerrits <dirk at gerrits.homeip.net> writes:
>>David Abrahams wrote:
>>>Dirk Gerrits <dirk at gerrits.homeip.net> writes:
>>You'll have to teach me how to write the reference material,
>>though. That is, I assume it's not hand-written HTML?
> Sadly, no.  One day we'll use BoostBook generation, but 'till then,
> you just copy one of the templates
> (e.g. http://www.boost.org/libs/python/doc/v2/header.html) and go
> from there.

Okay, will do.

>>For the tests I can just use
[code snipped]
 >>as in embedding.cpp right?
> Seems likely, but I'm not certain what you need to do for the tests
> you want to run.

Well nothing too fancy I think.

>>That shouldn't be too hard.
>>I'd like to stress again though, that my current solution is pretty
>>basic. I really have no use for multiple interpreters
> Most people don't, it turns out.

Well then, I'll get my code boostified. :)

>>, or multiple Python-using threads at the moment. If anyone else
>>does, and wants to see it addressed in Boost.Python, I encourage
>>them to post their code and extra testcases here.
> That's the spirit!

Boost.Spirit? :P

Seriously though, I have some more questions.

When you are embedding Python in a C++ program, your Python code may 
need to call C++ code in the embedding program. (At least, that's what I 
currently need to do.) To do this, you can't simply create a 
module_name.so or module_name.pyd as you'd do when extending; the BPL 
wrapper code must be in the C++ program itself. To make the module 
available to Python, you must call a function like 
PyImport_AppendInittab (or interpreter::add_module which I just whipped 
up ;)). The parameters to this call are the module name and the init 
function. Herein lies the problem. If the module is in a different 
compilation unit than the code using the interpreter one must forward 
declare the init function. Is there currently a way to do this safely? 
Perhaps a macro like the following?

#if (defined(_WIN32) || defined(__CYGWIN__)) && 

extern "C" __declspec(dllexport) void init##name();


extern "C" void init##name()


I have some issues with this though. For starters, the macro name is 
much too long for my taste. ;) Furthermore, I doubt if it makes sense to 
do this sort of embedding without defining BOOST_PYTHON_STATIC_MODULE. 
Which brings me to my last question.

There used to be a bug which caused a 'Fatal Python error' upon 
Py_Finalize when doing this kind of embedding. But when I ran into that, 
there was no BOOST_PYTHON_STATIC_MODULE. Now, with 
BOOST_PYTHON_STATIC_MODULE I haven't been able to replicate it. Has it 
been fixed then?

Dirk Gerrits

More information about the Cplusplus-sig mailing list