C++ Boost

Boost.Python

Header <boost/python/module.hpp>


Contents

Introduction
Macros
BOOST_PYTHON_MODULE
Example(s)

Introduction

This header provides the basic facilities needed to create a Boost.Python extension module.

Macros

BOOST_PYTHON_MODULE(name) is used to declare Python module initialization functions. The name argument must exactly match the name of the module to be initialized, and must conform to Python's identifier naming rules. Where you would normally write

extern "C" void initname()
{
   ...
}
Boost.Python modules should be initialized with
BOOST_PYTHON_MODULE(name)
{
   ...
}
This macro generates two functions in the scope where it is used: extern "C" void initname(), and void init_module_name(), whose body must follow the macro invocation. init_name passes init_module_name to handle_exception() so that any C++ exceptions generated are safely processeed. During the body of init_name, the current scope refers to the module being initialized.

Example(s)

C++ module definition:

#include <boost/python/module.hpp>

BOOST_PYTHON_MODULE(xxx)
{
    throw "something bad happened"
}
Interactive Python:
>>> import xxx
Traceback (most recent call last):
  File "", line 1, in ?
RuntimeError: Unidentifiable C++ Exception

Revised 13 November, 2002

© Copyright Dave Abrahams 2002. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)