• 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/errors.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_enum_hpp.html" title="boost/python/enum.hpp">
10<link rel="next" href="boost_python_exception_translato.html" title="boost/python/exception_translator.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_enum_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_exception_translato.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_errors_hpp"></a><a class="link" href="boost_python_errors_hpp.html" title="boost/python/errors.hpp">boost/python/errors.hpp</a>
21</h2></div></div></div>
22<div class="toc"><dl class="toc">
23<dt><span class="section"><a href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.introduction">Introduction</a></span></dt>
24<dt><span class="section"><a href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.class_error_already_set">Class
25        <code class="computeroutput"><span class="identifier">error_already_set</span></code></a></span></dt>
26<dt><span class="section"><a href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.functions">Functions</a></span></dt>
27<dt><span class="section"><a href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.example">Example</a></span></dt>
28</dl></div>
29<div class="section">
30<div class="titlepage"><div><div><h3 class="title">
31<a name="high_level_components.boost_python_errors_hpp.introduction"></a><a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.introduction" title="Introduction">Introduction</a>
32</h3></div></div></div>
33<p>
34          &lt;boost/python/errors.hpp&gt; provides types and functions for managing
35          and translating between Python and C++ exceptions. This is relatively low-level
36          functionality that is mostly used internally by Boost.Python. Users should
37          seldom need it.
38        </p>
39</div>
40<div class="section">
41<div class="titlepage"><div><div><h3 class="title">
42<a name="high_level_components.boost_python_errors_hpp.class_error_already_set"></a><a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.class_error_already_set" title="Class error_already_set">Class
43        <code class="computeroutput"><span class="identifier">error_already_set</span></code></a>
44</h3></div></div></div>
45<p>
46          error_already_set is an exception type which can be thrown to indicate
47          that a Python error has occurred. If thrown, the precondition is that
48          <a href="http://www.python.org/doc/2.2/api/exceptionHandling.html#l2h-71" target="_top">PyErr_Occurred()</a>
49          returns a value convertible to <code class="computeroutput"><span class="keyword">true</span></code>.
50          Portable code shouldn't throw this exception type directly, but should
51          instead use <a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.functions" title="Functions">throw_error_already_set()</a>,
52          below.
53        </p>
54<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
55<span class="special">{</span>
56    <span class="keyword">class</span> <span class="identifier">error_already_set</span> <span class="special">{};</span>
57<span class="special">}}</span>
58</pre>
59</div>
60<div class="section">
61<div class="titlepage"><div><div><h3 class="title">
62<a name="high_level_components.boost_python_errors_hpp.functions"></a><a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.functions" title="Functions">Functions</a>
63</h3></div></div></div>
64<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">bool</span> <span class="identifier">handle_exception</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">f</span><span class="special">)</span> <span class="keyword">throw</span><span class="special">();</span>
65<span class="keyword">void</span> <span class="identifier">handle_exception</span><span class="special">()</span> <span class="keyword">throw</span><span class="special">();</span>
66</pre>
67<div class="variablelist">
68<p class="title"><b></b></p>
69<dl class="variablelist">
70<dt><span class="term">Requires</span></dt>
71<dd><p>
72                The first form requires that the expression function0&lt;void&gt;(f)
73                is valid. The second form requires that a C++ exception is currently
74                being handled (see section 15.1 in the C++ standard).
75              </p></dd>
76<dt><span class="term">Effects</span></dt>
77<dd><p>
78                The first form calls f() inside a try block which first attempts
79                to use all registered <a class="link" href="boost_python_exception_translato.html" title="boost/python/exception_translator.hpp">exception
80                translators</a>. If none of those translates the exception, the
81                catch clauses then set an appropriate Python exception for the C++
82                exception caught, returning true if an exception was thrown, false
83                otherwise. The second form passes a function which rethrows the exception
84                currently being handled to the first form.
85              </p></dd>
86<dt><span class="term">Postconditions</span></dt>
87<dd><p>
88                No exception is being handled
89              </p></dd>
90<dt><span class="term">Throws</span></dt>
91<dd><p>
92                nothing
93              </p></dd>
94<dt><span class="term">Rationale</span></dt>
95<dd><p>
96                At inter-language boundaries it is important to ensure that no C++
97                exceptions escape, since the calling language usually doesn't have
98                the equipment necessary to properly unwind the stack. Use handle_exception
99                to manage exception translation whenever your C++ code is called
100                directly from the Python API. This is done for you automatically
101                by the usual function wrapping facilities: <a class="link" href="../function_invocation_and_creation/boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.functions" title="Functions">make_function()</a>,
102                <a class="link" href="../function_invocation_and_creation/boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.functions" title="Functions">make_constructor()</a>,
103                <a class="link" href="boost_python_def_hpp.html#high_level_components.boost_python_def_hpp.functions" title="Functions">def()</a>
104                and <a class="link" href="../high_level_components.html#high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel.class_template_class_modifier_fu" title="Class template class_ modifier functions">class_::def()</a>.
105                The second form can be more convenient to use (see the example below),
106                but various compilers have problems when exceptions are rethrown
107                from within an enclosing try block.
108              </p></dd>
109</dl>
110</div>
111<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">T</span><span class="special">*</span> <span class="identifier">expect_non_null</span><span class="special">(</span><span class="identifier">T</span><span class="special">*</span> <span class="identifier">x</span><span class="special">);</span></pre>
112<div class="variablelist">
113<p class="title"><b></b></p>
114<dl class="variablelist">
115<dt><span class="term">Returns</span></dt>
116<dd><p>
117                x
118              </p></dd>
119<dt><span class="term">Throws</span></dt>
120<dd><p>
121                error_already_set() iff x == 0.
122              </p></dd>
123<dt><span class="term">Rationale</span></dt>
124<dd><p>
125                Simplifies error-handling when calling functions in the Python/C
126                API which return 0 on error.
127              </p></dd>
128</dl>
129</div>
130<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">throw_error_already_set</span><span class="special">();</span></pre>
131<div class="variablelist">
132<p class="title"><b></b></p>
133<dl class="variablelist">
134<dt><span class="term">Effects</span></dt>
135<dd><p>
136                throw error_already_set();
137              </p></dd>
138<dt><span class="term">Rationale</span></dt>
139<dd><p>
140                Simplifies error-handling when calling functions in the Python/C
141                API which return 0 on error.
142              </p></dd>
143</dl>
144</div>
145<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">throw_error_already_set</span><span class="special">();</span></pre>
146<div class="variablelist">
147<p class="title"><b></b></p>
148<dl class="variablelist">
149<dt><span class="term">Effects</span></dt>
150<dd><p>
151                throw error_already_set();
152              </p></dd>
153<dt><span class="term">Rationale</span></dt>
154<dd><p>
155                Many platforms and compilers are not able to consistently catch exceptions
156                thrown across shared library boundaries. Using this function from
157                the Boost.Python library ensures that the appropriate catch block
158                in handle_exception() can catch the exception.
159              </p></dd>
160</dl>
161</div>
162</div>
163<div class="section">
164<div class="titlepage"><div><div><h3 class="title">
165<a name="high_level_components.boost_python_errors_hpp.example"></a><a class="link" href="boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.example" title="Example">Example</a>
166</h3></div></div></div>
167<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
168<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">errors</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
169<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">object</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
170<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">handle</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
171
172<span class="comment">// Returns a std::string which has the same value as obj's "__name__"</span>
173<span class="comment">// attribute.</span>
174<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">obj</span><span class="special">)</span>
175<span class="special">{</span>
176   <span class="comment">// throws if there's no __name__ attribute</span>
177   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">expect_non_null</span><span class="special">(</span>
178      <span class="identifier">PyObject_GetAttrString</span><span class="special">(</span><span class="identifier">obj</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">(),</span> <span class="string">"__name__"</span><span class="special">));</span>
179
180   <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">s</span> <span class="special">=</span> <span class="identifier">PyString_AsString</span><span class="special">(</span><span class="identifier">p</span><span class="special">);</span>
181   <span class="keyword">if</span> <span class="special">(</span><span class="identifier">s</span> <span class="special">!=</span> <span class="number">0</span><span class="special">)</span>
182        <span class="identifier">Py_DECREF</span><span class="special">(</span><span class="identifier">p</span><span class="special">);</span>
183
184   <span class="comment">// throws if it's not a Python string</span>
185   <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">result</span><span class="special">(</span>
186      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">expect_non_null</span><span class="special">(</span>
187         <span class="identifier">PyString_AsString</span><span class="special">(</span><span class="identifier">p</span><span class="special">)));</span>
188
189   <span class="identifier">Py_DECREF</span><span class="special">(</span><span class="identifier">p</span><span class="special">);</span> <span class="comment">// Done with p</span>
190
191   <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
192<span class="special">}</span>
193
194<span class="comment">//</span>
195<span class="comment">// Demonstrate form 1 of handle_exception</span>
196<span class="comment">//</span>
197
198<span class="comment">// Place into result a Python Int object whose value is 1 if a and b have</span>
199<span class="comment">// identical "__name__" attributes, 0 otherwise.</span>
200<span class="keyword">void</span> <span class="identifier">same_name_impl</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*&amp;</span> <span class="identifier">result</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">b</span><span class="special">)</span>
201<span class="special">{</span>
202   <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">PyInt_FromLong</span><span class="special">(</span>
203      <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">a2</span><span class="special">));</span>
204<span class="special">}</span>
205
206<span class="identifier">object</span> <span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">p</span><span class="special">)</span>
207<span class="special">{</span>
208   <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span><span class="special">(</span>
209        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">handle</span><span class="special">&lt;&gt;(</span>
210             <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">borrowed</span><span class="special">(</span><span class="identifier">a1</span><span class="special">)));</span>
211<span class="special">}</span>
212
213<span class="comment">// This is an example Python 'C' API interface function</span>
214<span class="keyword">extern</span> <span class="string">"C"</span> <span class="identifier">PyObject</span><span class="special">*</span>
215<span class="identifier">same_name</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">args</span><span class="special">,</span> <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">keywords</span><span class="special">)</span>
216<span class="special">{</span>
217   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">a1</span><span class="special">;</span>
218   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">a2</span><span class="special">;</span>
219   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">result</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
220
221   <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">PyArg_ParseTuple</span><span class="special">(</span><span class="identifier">args</span><span class="special">,</span> <span class="keyword">const_cast</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">*&gt;(</span><span class="string">"OO"</span><span class="special">),</span> <span class="special">&amp;</span><span class="identifier">a1</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">a2</span><span class="special">))</span>
222      <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
223
224   <span class="comment">// Use boost::bind to make an object compatible with</span>
225   <span class="comment">// boost::Function0&lt;void&gt;</span>
226   <span class="keyword">if</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">handle_exception</span><span class="special">(</span>
227         <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;(</span><span class="identifier">same_name_impl</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">result</span><span class="special">),</span> <span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">a1</span><span class="special">),</span> <span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">a2</span><span class="special">))))</span>
228   <span class="special">{</span>
229      <span class="comment">// an exception was thrown; the Python error was set by</span>
230      <span class="comment">// handle_exception()</span>
231      <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
232   <span class="special">}</span>
233
234   <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span>
235<span class="special">}</span>
236
237<span class="comment">//</span>
238<span class="comment">// Demonstrate form 2 of handle_exception. Not well-supported by all</span>
239<span class="comment">// compilers.</span>
240<span class="comment">//</span>
241<span class="keyword">extern</span> <span class="string">"C"</span> <span class="identifier">PyObject</span><span class="special">*</span>
242<span class="identifier">same_name2</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">args</span><span class="special">,</span> <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">keywords</span><span class="special">)</span>
243<span class="special">{</span>
244   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">a1</span><span class="special">;</span>
245   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">a2</span><span class="special">;</span>
246   <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">result</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
247
248   <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">PyArg_ParseTuple</span><span class="special">(</span><span class="identifier">args</span><span class="special">,</span> <span class="keyword">const_cast</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">*&gt;(</span><span class="string">"OO"</span><span class="special">),</span> <span class="special">&amp;</span><span class="identifier">a1</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">a2</span><span class="special">))</span>
249      <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
250
251   <span class="keyword">try</span> <span class="special">{</span>
252      <span class="keyword">return</span> <span class="identifier">PyInt_FromLong</span><span class="special">(</span>
253         <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">a1</span><span class="special">))</span> <span class="special">==</span> <span class="identifier">get_name</span><span class="special">(</span><span class="identifier">borrowed_object</span><span class="special">(</span><span class="identifier">a2</span><span class="special">)));</span>
254   <span class="special">}</span>
255   <span class="keyword">catch</span><span class="special">(...)</span>
256   <span class="special">{</span>
257      <span class="comment">// If an exception was thrown, translate it to Python</span>
258      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">handle_exception</span><span class="special">();</span>
259      <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
260   <span class="special">}</span>
261<span class="special">}</span>
262</pre>
263</div>
264</div>
265<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
266<td align="left"></td>
267<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
268        Distributed under the Boost Software License, Version 1.0. (See accompanying
269        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>
270      </p>
271</div></td>
272</tr></table>
273<hr>
274<div class="spirit-nav">
275<a accesskey="p" href="boost_python_enum_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_exception_translato.html"><img src="../../images/next.png" alt="Next"></a>
276</div>
277</body>
278</html>
279