[C++-sig] Providing a Package from a single Extension?

Ult Mundane ult at mundane.org
Mon Jul 18 20:52:38 CEST 2005


For the record:

Here was a helpful URL:

http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/f929d6d8221c6ec7/f3ca352ae523a163?q=Py_InitModule+submodule&rnum=1&hl=en#f3ca352ae523a163

And here's a modification of my earlier code, which demonstrates creating a 
package using Boost.Python, statically linked into an application which embeds 
python. The static embedding part is not necessary for the example, however; a 
regular Python extension should be able to do the same thing.

Ult

---

#include <Python.h>
#include <boost/python.hpp>

#include <iostream>

using namespace boost::python;

void func(char const * str)
{
   std::cout << "@@@ " << str << std::endl;
}

BOOST_PYTHON_MODULE(mymodule)
{
   def("func", func);
}

void init_mypackage_mymodule()
{
   boost::python::detail::init_module("mypackage.mymodule", initmymodule);
}

BOOST_PYTHON_MODULE(mypackage)
{
   def("func", func);
   scope().attr("__path__") = "mypackage";

   init_mypackage_mymodule();

   object mymodule((
       handle<>(borrowed(PyImport_AddModule("mymodule")))));

   scope().attr("mymodule") = mymodule;
}

int main(int argc, char * argv[])
{
   PyImport_AppendInittab("mypackage", initmypackage);

   Py_Initialize();

   Py_Main(argc, argv);

   Py_Finalize();
}



More information about the Cplusplus-sig mailing list