Hello World from Boost::Python

I just created a “Hello World” application in Boost::Python. I’m comparing the Boost and SWIG options for embedding Python and making PN code available to it so this is the first step for the Boost implementation.

Here’s the code:

#include <boost /python.hpp>

int main(int argc, char* argv[])
{
  Py_Initialize();

  object main_module((
    handle<>(borrowed(PyImport_AddModule("__main__")))
  ));

  object main_namespace = main_module.attr("__dict__");

  // Run some python!

  handle<> ignored((PyRun_String(

    "print \"Hello World from Python!\""

    , Py_file_input
    , main_namespace.ptr()
    , main_namespace.ptr())
  ));

  Py_Finalize();

  return 0;
}

Not very much code at all to get going! boost_python.dll (required to use Boost::Python) comes in at 180kb. The initial test app (as coded above) in Release configuration takes 40kb.

The differences between this and the SWIG embedding version will be minimal, boost doesn’t hide much of the embedding code for you. The real differences will be in creating wrappers for functions and classes. With boost I have to write wrappers, SWIG will automatically generate them. The interesting bit will be the size of the code created.