October 17th, 2005


17
Oct 05

Exposing Functions to Embedded Python

So far, instead of exposing functions to Python, we have (the reverse I guess) embedded the Python interpreter in our process using Boost::Python. So, running Python from within our process is all good and well but we want the Python code to interact with our C++ code as well.

We’ll add to the previous example:

const char* greet()
{
	return "Hello World from C++";
}

BOOST_PYTHON_MODULE(embed_test)
{
	def("greet", greet);
}

Here we have defined a python module embed_test which we will now expose to our embedded Python interpreter. The module has one function, “greet” in the Python module calls the C++ greet function.

Now we add to the main function:

if(PyImport_AppendInittab("embed_test", initembed_test) == -1)
		throw std::runtime_error("Failed to add test module to builtins");

The initembed_test reference is the function name generated by the BOOST_PYTHON_MODULE macro. Once we have made Python aware of this built-in module, we can modify the python code we run in the example to print out the result of the greet function:

handle<> ignored((PyRun_String(
	"import embed_test\n"
	"print embed_test.greet()"

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

The techniques used here are all based around the basic functionality available in the Python C API. However, the Boost::Python library does make it easier to expose your own code to the Python interpreter. Next we’ll look at exposing classes and then move on to the use of SWIG.


17
Oct 05

WordPress Naming

I wrote a while back about WordPress using a similar naming scheme to that used for PN2 releases, there’s an excellent write-up of some of the release names chosen so far over on Ryan Boren’s blog.

Ever since the third release of PN 2 (2.0.3) PN releases have been named after my favorite jazz musicians. It started with coltrane (one of the candidates for the next wordpress release) for, obviously, the legend John Coltrane.

Next up was 0.4 with “miles” for Miles Davis, one of the best known and perhaps most accessible jazz artists. WordPress has already been here! 0.5 ushered in the era of “mingus” – another wordpress release name. Ryan’s write-up of mingus is fantastic, every new bit of mingus I listen to provides something new – what an immense talent but less accessible than Davis.

Half way through the numerous 0.5 releases saw PN move from mingus to “herbie”. Herbie Hancock is one of my favorite jazz artists – a hugely influential modern jazz pianist. Head Hunters was a landmark album and both Chameleon and Watermelon man are pure jazz genius. Herbie Hancock was the artist that re-lit my interest in jazz and has had me looking back for old favorites and rediscovering the whole scene.

This brings us nicely to 0.6 and “ella”. I discussed Ella Fitzgerald in the release post for 0.6 and note that she has also been suggested as the name for the next wordpress release.

Coming up on the PN 2 roadmap are Count Basie, Dizzy Gillespie and Duke Ellington. More to come about them with the releases!

As for WordPress, go for Thelonius Monk. I just bought a couple of Monk CDs to experiment and they are fantastic – 0.6 “Monk”.