In two previous posts we showed how to call C functions from other modules and Python methods from within a Boost.Python module. Here we’ll show that the techniques are easily combined and demonstrate the atexit module which we’ll use to prevent Python interpreter crashes upon exit.
Monthly Archives: July 2011
Simple Timer using Python’s With Statement
Python’s with statement, available since Python 2.5, does not seem to be widely used despite being very useful.
Here we describe how to use the with
statement to create a simple timer with the syntax:
>>> with TicToc(): ... some_slow_operations ... Elapsed time is 2.000073 seconds.
Readers familiar with MATLAB will recognize the similarity to the familiar timing mechanism:
> tic ; some_slow_operations; toc Elapsed time is 10.020349 seconds.
Calling Python from C++
In a previous post we already covered calling a C function from one Boost.Python module in another module.
What if instead we want to call a Python function directly from C? We’ll stick with the same Bird / Quacker metaphor as in the previous post, but we’ll now implement our duck class in PyDuck.py:
""" PyDuck.py """ def quack(): print "Python Quack"
Now how can we call Python from C++? It turns out that Boost.Python defines operator() for the object class, so calling Python is relatively easy. But things become more difficult if we want to maintain a default implementation in C.
Python Capsules
Python Capsules are useful for passing C pointers between different Python modules. In particular, one can encapsulate a C function pointer in one module and unpack and call it in another module.
To begin, suppose we have two Boost.Python modules:
- Bird, which has methods setQuack() and callQuack().
- CppDuck, which has a method getQuack().
Our goal will be to get a function pointer with CppDuck.getQuack(), set it in another module in Bird.setQuack(), and finally call it with Bird.callQuack().
Boost.Python Submodules
I’ve been using Boost.Python to wrap a C++ library for use in Python for the past few months, and recently needed to have one compiled .so (or .pyd) file behave as a package instead of as a module. That is, I wanted the one file to behave as if it was actually a directory of Python files:
/application __init__.py submodule.py