[C++-sig] A question about PyImport_AppendInittab

Stefan Seefeld seefeld at sympatico.ca
Sat Aug 26 03:04:45 CEST 2006


Qun Cao wrote:
> Hi Everyone,
> 
> I am adopting the code in boost/libs/python/test/embedding.cpp for my
> project.  Here is my question, in
> embedding.cpp we have:
> 
>  if (PyImport_AppendInittab("embedded_hello", initembedded_hello) == -1)
>         throw std::runtime_error("Failed to add embedded_hello to the
> interpreter's "
>                                  "builtin modules");
> 
> I noticed #define BOOST_PYTHON_MODULE BOOST_PYTHON_MODULE_INIT in
> module.hpp file, so I guess "initembedded_hello" here is a pointer to
> the module "embedd_hello".

Correct. More specifically, 'initembedded_hello' is the symbol the python
interpreter will look for when it tries to import the 'embedded_hello'
module.

> However, in my project,
> BOOST_PYTHON_MODULE{} definition code sits
> in a different class class_wrapper.cpp than main.cpp, does that mean i
> need to include class_wrapper.cpp in main.cpp?  That gives me wierd
> problem in compilation though.

main.cpp typically contains the entry point for your application (i.e.
the int main(int, char **) function). It should be linked with the
python interpreter (i.e. libpython.lib or equivalent).
The 'embedded_hello' module will physically reside in a different
executable. However, for the module lookup mechanism to work you
have to follow some naming conventions. A module 'embedded_hello'
for example will be identified by the python interpreter as a shared
library named 'embedded_hello.pyd' or 'embedded_hello.dll', and it
should contain an 'initembedded_hello' function. (The latter
is best generated with the BOOST_PYTHON_MODULE macro.)

> Also, generally, why main.cpp needs to know how a C++ wrapper module
> is created?

It doesn't.

> Isn't it enough to tell it that there exists a python
> module in class.dll and the embedded python interpreter will just
> understand everything from there?

Yep. The embedding.cpp code you are looking at is a unit test that
lumps multiple things into a single file to make testing simple.
Real python extension modules won't be linked with the main
application, as that would be pretty pointless.

HTH,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list