Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/doc/v2/Jun2002.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 9.0 KB
Line 
1<!-- Copyright David Abrahams 2006. Distributed under the Boost -->
2<!-- Software License, Version 1.0. (See accompanying -->
3<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
4<html>
5<head>
6<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
7<link rel="stylesheet" type="text/css" href="../boost.css">
8<title>Boost.Python - June 2002 Progress Report</title>
9</head>
10<body link="#0000ff" vlink="#800080">
11<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
12    "header">
13  <tr> 
14    <td valign="top" width="300"> 
15          <h3><a href="../../../../index.htm"><img height="86" width="277" alt=
16          "C++ Boost" src="../../../../boost.png" border="0"></a></h3>
17    </td>
18    <td valign="top"> 
19      <h1 align="center"><a href="../index.html">Boost.Python</a></h1>
20      <h2 align="center">June 2002 Progress Report</h2>
21    </td>
22  </tr>
23</table>
24<hr>
25<h2>Contents</h2>
26<dl class="index">
27  <dt><a href="#intro">Introduction</a></dt>
28  <dt><a href="#handle"><code>handle&lt;T&gt;</code></a></dt>
29  <dt><a href="#object"><code>object</code></a></dt>
30  <dl class="index">
31    <dt><a href="#operators"><code>object</code> operators</a></dt>
32    <dt><a href="#conversions"><code>object</code> conversions</a></dt>
33  </dl>
34  <dt><a href="#list"><code>list</code></a></dt>
35  <dt><a href="#numerics"><code>Numerics</code></a></dt>
36  <dt><a href="#community">Community</a></dt>
37  <dt><a href="#next">What's Next</a></dt>
38</dl>
39
40<h2><a name="intro">Introduction</a></h2>
41
42July was mostly focused on allowing expressive manipulation of
43individual Python objects, or what Ralf Grosse-Kunstleve calls
44&quot;Writing Python in C++&quot;. The work began with this <a
45href="http://mail.python.org/pipermail/c++-sig/2002-June/001311.html">posting</a>,
46which outlines the issues and intention.
47
48<h2><a name="handle"><code>handle&lt;T&gt;</code></a></h2>
49
50The most basic element needed was a replacement for the
51<code>reference&lt;&gt;</code> class template and the
52<code>ref</code> typedef from Boost.Python v1, a simple smart
53pointer to a Python object. The old v1 typedef
54&quot;<code>ref</code>&quot; (for
55<code>reference&lt;PyObject&gt;</code>) had to be retired because I
56thought it would be too confusing given the importance of <code><a
57href="../../../bind/ref.html">boost::ref</a>()</code> to this
58library. I began a <a
59href="http://mail.python.org/pipermail/c++-sig/2002-June/001311.html">discussion</a>of
60possible names, and it was eventually <a
61href="http://mail.python.org/pipermail/c++-sig/2002-June/001337.html">decided</a>
62to rename <code>reference</code> to <code>handle</code> and supply a
63default argument so that <code>ref</code> could be spelled
64<code>handle&lt;&gt;</code> without an additional typedef. There
65were also some interface changes to make it safer and more-efficient
66to interface with the raw
67<code>PyObject*</code>s forced on us by Python's 'C' API. A
68discussion of those protocols can be found <a
69href="http://mail.python.org/pipermail/c++-sig/2002-June/001401.html">here</a>.
70
71<h2><a name="handle"><code>object</code></a></h2>
72
73It is intended that users will seldom need or want to work with
74<code>handle&lt;&gt;</code>; its major distinguishing features are
75that it gives direct access to the underlying object representation
76through <code>operator*</code> and <code>operator-&gt;</code>, and
77that can be <code>NULL</code>, both sources of danger. Instead the
78library provides a class called <code>object</code>, which
79encapsulates a valid Python object and provides a similar interface to
80Python's.
81
82<h3><a name="operators"><code>object</code> operators</a></h3>
83
84The first challenge was to provide support for object manipulations
85using a Python-like syntax, mostly in the form of operator overloads:
86
87<table border="1">
88<tr><th>Python <th>C++
89
90<tr>
91  <td><code>y = x.foo</code>  <td><code>y = x.attr(&quot;foo&quot;);
92<tr>
93  <td><code>x.foo = 1</code>  <td><code>x.attr(&quot;foo&quot;) = 1;
94
95<tr>
96  <td><code>y = x[z]</code>  <td><code>y = x[z];
97<tr>
98  <td><code>x[z] = 1</code>  <td><code>x[z] = 1;
99
100<tr>
101  <td><code>y = x[3:-1]</code>  <td><code>y = x.slice(3,-1);
102
103<tr>
104  <td><code>y = x[3:]</code>  <td><code>y = x.slice(3,_);
105
106<tr>
107  <td><code>y = x[:-2]</code>  <td><code>y = x.slice(_,-2);
108
109<tr>
110  <td><code>z = x(1, y)</code>  <td><code>z = x(1, y);
111<tr>
112  <td><code>z = x.f(1, y)</code>  <td><code>z = x.attr(&quot;f&quot;)(1, y);
113
114<tr>
115  <td><code>not x</code>  <td><code>!x
116
117<tr>
118  <td><code>x and y</code>  <td><code>x and y
119</table>
120
121I'm still a unsatisfied with the interface for attribute access. There
122original proposal  used a syntax like this one:
123<pre>
124y = x._(&quot;foo&quot;);
125x._(&quot;foo&quot;) = 1;
126</pre>
127
128which was only marginally better than what we've got. Niki Spahiev
129then <a
130href="http://mail.python.org/pipermail/c++-sig/2002-June/001447.html">pointed
131out</a> a potential conflict with the macro which GNU Gettext <a
132href="http://www.gnu.org/manual/gettext/html_mono/gettext.html#SEC6">suggests</a>
133people define. This unfortunate state of affairs forced us into using
134<code>attr</code> instead. I'd still like to find a better interface,
135but the lack of overloadable C++ operators which aren't already used
136in Python is an obstacle. The comma operator is still a possibility,
137but it has the wrong precedence:
138<pre>
139y = x,&quot;foo&quot;    // error
140x,&quot;foo&quot; = 1;   // error
141
142y = (x,&quot;foo&quot;); // ok
143(x,&quot;foo&quot;) = 1; // ok
144</pre>
145
146Well, I guess we could consider adding that to the interface without
147removing <code>attr()</code>, to see how it plays out...
148
149<h3><a name="operators"><code>object</code> conversions</a></h3>
150
151The <code>object</code> class also provided an opportunity to replace
152Boost.Python v1's <code>to_python()</code> as a user-level
153interface. Instead, <code>object</code> has a templated constructor
154which can be used to convert any C++ object to Python using the same
155underlying mechanisms used for the arguments to <code><a
156href="call.html">call</a>&lt;&gt;</code>.
157
158<p>Incidentally, the implementation of operator and conversion support
159for object uncovered an inordinate number of compiler bugs in our
160targeted platforms. It was a lot more &quot;interesting&quot; than it
161should have been.
162
163<h2><a name="list"><code>list</code></a></h2>
164
165With <code>object</code> implemented, it was time to begin replacing
166the ad-hoc implementations of <code>list</code>, <code>string</code>,
167and <code>dictionary</code> supplied by Boost.Python v1 with something
168more robust. I started with <code>list</code> as an example. Because
169<code>object</code> already provides all of the requisite operators,
170publicly deriving <code>list</code> from object seemed like a good
171choice. The remaining issues were what do do about the one-argument
172list constructor (which in Python attempts to convert its argument to
173a list), and how to deal converting with <code>list</code> arguments
174to wrapped functions. Some of the issues are laid out in <a
175href="http://mail.python.org/pipermail/c++-sig/2002-June/001551.html">this
176thread</a>. Ultimately, it was decided that <code>list(x)</code>
177should do the same thing in C++ as in Python (conversion), while
178<code>list</code> arguments should only match Python
179<code>list</code>s (and <code>list</code> subclasses). The
180implementation worked well, and provided a <a
181href="http://mail.python.org/pipermail/c++-sig/2002-June/001586.html">roadmap</a>
182for the protocol to be used for implementation of the other built-in
183types.
184
185<h2><a name="numerics">Numerics</a></h2>
186
187Support for C++ <code>long long</code> and <code>unsigned long
188long</code>
189(and <code>__int64</code> on MSVC) to/from python conversions was
190added this month. We also improved handling of numeric overflows when
191converting, e.g., a Python int to a type with a more limited range of
192representation.
193
194<h2><a name="community">Community</a></h2>
195
196<ul>
197<li>Ralf W. Grosse-Kunstleve and Nick Sauter have implemented
198<a href="http://cci.lbl.gov/boost/">multiplatform nightly
199build-and-test</a> runs for Boost.Python V2 at LBL.
200
201<li>Dave Hawkes has made significant progress on generating the
202Python <a
203href="http://mail.python.org/pipermail/c++-sig/2002-June/001503.html">built-in
204function and API wrappers</a>
205
206<li>Achim Domma has agreed to take up the job of implementing the
207<code>str</code>, <code>dict</code>, and <code>tuple</code> classes.
208</ul>
209
210Deep thanks to all the Boost.Python contributors! This project
211wouldn't be possible without your participation.
212
213  <h2><a name="next">What's Next</a></h2>
214
215As I write this we are already well into the month of July, so I
216suggest you consult the <a
217href="http://mail.python.org/pipermail/c++-sig/2002-July/">Mailing
218List Archive</a> if you want to know what's been happening. Otherwise
219you'll just have to wait till next month (hopefully the beginning).
220
221<p>Revised
222  <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
223  13 November, 2002
224  <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
225</p>
226<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> 
227  2002. </i></p>
228</body>
229</html>
Note: See TracBrowser for help on using the repository browser.