2
0
mirror of https://github.com/boostorg/python.git synced 2026-02-02 21:12:15 +00:00
Files
python/doc/html/reference/concepts/objectwrapper/caveat.html
2015-08-05 07:21:49 -04:00

73 lines
6.0 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Caveat</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../objectwrapper.html" title="ObjectWrapper">
<link rel="prev" href="typewrapper_concept_requirements.html" title="TypeWrapper Concept Requirements">
<link rel="next" href="../../high_level_components.html" title="Chapter&#160;2.&#160;High Level Components">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../images/boost.png"></td>
<td align="center"><a href="../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="typewrapper_concept_requirements.html"><img src="../../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../objectwrapper.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="../../high_level_components.html"><img src="../../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="concepts.objectwrapper.caveat"></a><a class="link" href="caveat.html" title="Caveat">Caveat</a>
</h3></div></div></div>
<p>
The upshot of the special member function invocation rules when the return
type is a TypeWrapper is that it is possible for the returned object to
manage a Python object of an inappropriate type. This is not usually a
serious problem; the worst-case result is that errors will be detected
at runtime a little later than they might otherwise be. For an example
of how this can occur, note that the <a class="link" href="../../object_wrappers.html#object_wrappers.boost_python_dict_hpp.class_dict" title="Class dict">dict</a>
member function <code class="computeroutput"><span class="identifier">items</span></code> returns
an object of type <a class="link" href="../../object_wrappers/boost_python_list_hpp/class_list.html" title="Class list">list</a>.
Now suppose the user defines this <code class="computeroutput"><span class="identifier">dict</span></code>
subclass in Python:
</p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">class</span> <span class="identifier">mydict</span><span class="special">(</span><span class="identifier">dict</span><span class="special">):</span>
<span class="special">...</span> <span class="identifier">def</span> <span class="identifier">items</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
<span class="special">...</span> <span class="keyword">return</span> <span class="identifier">tuple</span><span class="special">(</span><span class="identifier">dict</span><span class="special">.</span><span class="identifier">items</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span> <span class="special">#</span> <span class="keyword">return</span> <span class="identifier">a</span> <span class="identifier">tuple</span>
</pre>
<p>
Since an instance of <code class="computeroutput"><span class="identifier">mydict</span></code>
is also an instance of <code class="computeroutput"><span class="identifier">dict</span></code>,
when used as an argument to a wrapped C++ function, <a class="link" href="../../object_wrappers.html#object_wrappers.boost_python_dict_hpp.class_dict" title="Class dict">boost::python::dict</a>
can accept objects of Python type <code class="computeroutput"><span class="identifier">mydict</span></code>.
Invoking <code class="computeroutput"><span class="identifier">items</span><span class="special">()</span></code>
on this object can result in an instance of <a class="link" href="../../object_wrappers/boost_python_list_hpp/class_list.html" title="Class list">boost::python::list</a>
which actually holds a Python <code class="computeroutput"><span class="identifier">tuple</span></code>.
Subsequent attempts to use <code class="computeroutput"><span class="identifier">list</span></code>
methods (e.g. <code class="computeroutput"><span class="identifier">append</span></code>, or
any other mutating operation) on this object will raise the same exception
that would occur if you tried to do it from Python.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="typewrapper_concept_requirements.html"><img src="../../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../objectwrapper.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="../../high_level_components.html"><img src="../../../images/next.png" alt="Next"></a>
</div>
</body>
</html>