• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>boost/python/module.hpp</title>
5<link rel="stylesheet" href="../../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
8<link rel="up" href="../high_level_components.html" title="Chapter 2. High Level Components">
9<link rel="prev" href="boost_python_iterator_hpp.html" title="boost/python/iterator.hpp">
10<link rel="next" href="boost_python_operators_hpp.html" title="boost/python/operators.hpp">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
14<hr>
15<div class="spirit-nav">
16<a accesskey="p" href="boost_python_iterator_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_operators_hpp.html"><img src="../../images/next.png" alt="Next"></a>
17</div>
18<div class="section">
19<div class="titlepage"><div><div><h2 class="title" style="clear: both">
20<a name="high_level_components.boost_python_module_hpp"></a><a class="link" href="boost_python_module_hpp.html" title="boost/python/module.hpp">boost/python/module.hpp</a>
21</h2></div></div></div>
22<div class="toc"><dl class="toc">
23<dt><span class="section"><a href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.introduction">Introduction</a></span></dt>
24<dt><span class="section"><a href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.macros">Macros</a></span></dt>
25<dt><span class="section"><a href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.examples">Examples</a></span></dt>
26</dl></div>
27<div class="section">
28<div class="titlepage"><div><div><h3 class="title">
29<a name="high_level_components.boost_python_module_hpp.introduction"></a><a class="link" href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.introduction" title="Introduction">Introduction</a>
30</h3></div></div></div>
31<p>
32          This header provides the basic facilities needed to create a Boost.Python
33          extension module.
34        </p>
35</div>
36<div class="section">
37<div class="titlepage"><div><div><h3 class="title">
38<a name="high_level_components.boost_python_module_hpp.macros"></a><a class="link" href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.macros" title="Macros">Macros</a>
39</h3></div></div></div>
40<p>
41          <code class="computeroutput"><span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">name</span><span class="special">)</span></code>
42          is used to declare Python <a href="http://www.python.org/doc/2.2/ext/methodTable.html#SECTION003400000000000000000" target="_top">module
43          initialization functions</a>. The name argument must exactly match
44          the name of the module to be initialized, and must conform to Python's
45          <a href="http://www.python.org/doc/2.2/ref/identifiers.html" target="_top">identifier
46          naming rules</a>. Where you would normally write
47        </p>
48<pre class="programlisting"><span class="keyword">extern</span> <span class="string">"C"</span> <span class="keyword">void</span> <span class="identifier">initname</span><span class="special">()</span>
49<span class="special">{</span>
50   <span class="special">...</span>
51<span class="special">}</span>
52</pre>
53<p>
54          Boost.Python modules should be initialized with
55        </p>
56<pre class="programlisting"><span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">name</span><span class="special">)</span>
57<span class="special">{</span>
58   <span class="special">...</span>
59<span class="special">}</span>
60</pre>
61<p>
62          This macro generates two functions in the scope where it is used: <code class="computeroutput"><span class="keyword">extern</span> <span class="string">"C"</span>
63          <span class="keyword">void</span> <span class="identifier">initname</span><span class="special">()</span></code>, and <code class="computeroutput"><span class="keyword">void</span>
64          <span class="identifier">init_module_name</span><span class="special">()</span></code>,
65          whose body must follow the macro invocation. <code class="computeroutput"><span class="identifier">init_name</span></code>
66          passes <code class="computeroutput"><span class="identifier">init_module_name</span></code>
67          to <a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.functions" title="Functions">handle_exception()</a>
68          so that any C++ exceptions generated are safely processeed. During the
69          body of <code class="computeroutput"><span class="identifier">init_name</span></code>, the
70          <a class="link" href="boost_python_scope_hpp.html" title="boost/python/scope.hpp">current scope</a>
71          refers to the module being initialized.
72        </p>
73</div>
74<div class="section">
75<div class="titlepage"><div><div><h3 class="title">
76<a name="high_level_components.boost_python_module_hpp.examples"></a><a class="link" href="boost_python_module_hpp.html#high_level_components.boost_python_module_hpp.examples" title="Examples">Examples</a>
77</h3></div></div></div>
78<p>
79          C++ module definition:
80        </p>
81<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
82
83<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">xxx</span><span class="special">)</span>
84<span class="special">{</span>
85    <span class="keyword">throw</span> <span class="string">"something bad happened"</span>
86<span class="special">}</span>
87</pre>
88<p>
89          Interactive Python:
90        </p>
91<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">import</span> <span class="identifier">xxx</span>
92<span class="identifier">Traceback</span> <span class="special">(</span><span class="identifier">most</span> <span class="identifier">recent</span> <span class="identifier">call</span> <span class="identifier">last</span><span class="special">):</span>
93  <span class="identifier">File</span> <span class="string">""</span><span class="special">,</span> <span class="identifier">line</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">in</span> <span class="special">?</span>
94<span class="identifier">RuntimeError</span><span class="special">:</span> <span class="identifier">Unidentifiable</span> <span class="identifier">C</span><span class="special">++</span> <span class="identifier">Exception</span>
95</pre>
96</div>
97</div>
98<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
99<td align="left"></td>
100<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
101        Distributed under the Boost Software License, Version 1.0. (See accompanying
102        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
103      </p>
104</div></td>
105</tr></table>
106<hr>
107<div class="spirit-nav">
108<a accesskey="p" href="boost_python_iterator_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_operators_hpp.html"><img src="../../images/next.png" alt="Next"></a>
109</div>
110</body>
111</html>
112