4
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
5
<title>Writing Python Extensions in C++</title>
7
H1, H2, H3, H4 {color: #000099;
8
background-color: lightskyblue}
9
h3 {position: relative; left: 20px}
11
p {position: relative; left: 20px; margin-right: 20px}
12
pre {color: #0000cc; background-color: #eeeeee; position: relative; left: 40px; margin-right: 80px;
13
border-style: solid; border-color: black; border-width: thin}
15
p cite, ol cite, ul cite {font-family: monospace; font-style: normal; font-size: normal}
16
li var, pre var, p var, kbd var {color: #009900; font-style: italic}
17
li samp, pre samp, p samp, kbd samp {color: #009900; font-weight: bold}
18
li p {position: relative; left: 0}
19
table { position: relative; left: 20px; border: solid #888888 1px; background-color: #eeeeee}
20
table th {border: solid #888888 1px; background-color: #88dd88; color: black}
21
table td {border: solid #888888 1px}
22
table td.code {border: solid #888888 1px;font-family: monospace; font-style: normal; font-size: normal}
23
p.param {background-color: #eeeeee; border-top: lightskyblue solid 4}
28
<body bgcolor="#FFFFFF">
30
<h1 ALIGN="center">Writing Python Extensions in C++</h1>
32
<p ALIGN="CENTER">Barry Scott<br>
33
Reading, Berkshire, England<br>
34
<a href="mailto:barry@barrys-emacs.org">barry@barrys-emacs.org</a><br>
37
<p ALIGN="CENTER">Paul F. Dubois, <a href="mailto:dubois1@llnl.gov">dubois1@llnl.gov</a><br>
38
Lawrence Livermore National Laboratory<br>
39
Livermore, California, U.S.A.</p>
42
<p>PyCXX is designed to make it easier to extend Python with C++</p>
45
<p>PyCXX is a set of C++ facilities to make it easier to write Python extensions.
46
The chief way in which PyCXX makes it easier to write Python extensions is that it greatly
47
increases the probability that your program will not make a reference-counting error and
48
will not have to continually check error returns from the Python C API. PyCXX
49
integrates Python with C++ in these ways: </p>
52
<li>C++ exception handling is relied on to detect errors and clean up. In a complicated
53
function this is often a tremendous problem when writing in C. With PyCXX, we let the
54
compiler keep track of what objects need to be dereferenced when an error occurs.
55
<li>The Standard Template Library (STL) and its many algorithms plug and play with Python
56
containers such as lists and tuples.
57
<li>The optional CXX_Extensions facility allows you to replace the clumsy C tables with
58
objects and method calls that define your modules and extension objects.
61
<h3>Download and Installation</h3>
63
<p>Download PyCXX from <a href="http://sourceforge.net/projects/cxx/">http://sourceforge.net/projects/cxx/</a>.</p>
65
<p>The distribution layout is:</p>
67
<tr><th>Directory</th><th>Description</th></tr>
68
<tr><td class=code>.</td><td>Makefile for Unix and Windows, Release documentation</td>
69
<tr><td class=code>./CXX</td><td>Header files</td>
70
<tr><td class=code>./Src</td><td>Source files</td>
71
<tr><td class=code>./Doc</td><td>Documentation</td>
72
<tr><td class=code>./Demo</td><td>Testing and Demonstartion files</td>
75
<p>To use PyCXX you use its include files and add its source routines to your module.</p>
79
<li>Install the PyCXX files into a directory of your choice. For example:<br>
80
Windows: <cite>C:\PyCXX</cite><br>
81
Unix: <cite>/usr/local/PyCXX</cite>
82
<li>Tell your compiler where the PyCXX header files are:<br>
83
Windows: <cite>cl /I=C:\PyCXX ...</cite><br>
84
Unix: <cite>g++ -I/usr/local/PyCXX ...</cite>
85
<li>Include PyCXX headers files in your code using the CXX prefix:<br>
86
<cite>#include "CXX/Object.hxx"</cite>
89
<p>The header file CXX/config.h may need to be adjusted for the
90
compiler you use. As of this writing, only a fairly obscure reference to part of the
91
standard library needs this adjustment. Unlike prior releases, PyCXX now assumes namespace
92
support and a standard C++ library. </p>
94
<h3>Use of namespaces</h3>
96
<p>All PyCXX assets are in namespace "Py". You need to include
97
the Py:: prefix when referring to them, or include the statement:</p>
99
<p>using namespace Py;</p>
101
<h2>Wrappers for standard objects: CXX_Objects.h</h2>
103
<p>Header file CXX_Objects.h requires adding file Src/cxxsupport.cxx to
104
your module sources. CXX_Objects provides a set of wrapper classes that allow you access
105
to most of the Python C API using a C++ notation that closely resembles Python. For
106
example, this Python:</p>
114
<p>Can be written in C++:</p>
121
std::cout << alist << std::endl;
124
<p>You can optionally use the CXX/Extensions.hxx facility described later
125
to define Python extension modules and extension objects.</p>
127
<h3>We avoid programming with Python object pointers</h3>
129
<p>The essential idea is that we avoid, as much as possible, programming with pointers to
130
Python objects, that is, variables of type <cite>PyObject*</cite>. Instead,
131
we use instances of a family of C++ classes that represent the
132
usual Python objects. This family is easily extendible to include new kinds of Python
135
<p>For example, consider the case in which we wish to write a method, taking a single
136
integer argument, that will create a Python <cite>dict</cite>
137
and insert into it that the integer plus one under the key <cite>value</cite>.
138
In C we might do that as follows:</p>
140
<pre>static PyObject* mymodule_addvalue (PyObject* self, PyObject* args)
145
PyArgs_ParseTuple(args, "i", &k);
153
Py_DECREF(d); /* have to get rid of d first */
156
if(PyDict_SetItemString(d, "value", f) == -1)
166
<p>If you have written a significant Python extension, this tedium looks all too familiar.
167
The vast bulk of the coding is error checking and cleanup. Now compare the same thing
168
written in C++ using CXX/Objects.hxx. The things with Python-like names (Int, Dict, Tuple) are
169
from CXX/Objects.hxx.</p>
171
<pre>static PyObject* mymodule_addvalue (PyObject* self, PyObject* pargs)
176
args.verify_length(1);
180
d["value"] = k + 1;
182
return new_reference_to(d);
184
catch (const PyException&)
190
<p>If there are not the right number of arguments or the argument is not an
191
integer, an exception is thrown. In this case we choose to catch it and convert it into a
192
Python exception. The C++ exception handling mechanism takes care all the cleanup.</p>
194
<p>Note that the creation of the <cite>Int k</cite> got the first argument <em>and</em> verified
195
that it is an <cite>Int</cite>.</p>
197
<p>Just to peek ahead, if you wrote this method in an
198
ExtensionModule-derived module of your own, it would be a method and it could be written
199
even more simply:</p>
202
Object addvalue (Object & self, const Tuple & args)
204
args.verify_length(1);
212
<h2>The basic concept is to wrap Python pointers</h2>
215
<p>The basic concept of CXX/Objects.hxx is to create a wrapper around
216
each <cite>PyObject *</cite> so that the reference counting can be
217
done automatically, thus eliminating the most frequent source of errors. In addition, we
218
can then add methods and operators so that Python objects can be manipulated in C++
219
much like you would in Python.</p>
221
<p>Each <cite>Object</cite> contains a <cite>PyObject *</cite>
222
to which it owns a reference. When an <cite>Object</cite> is destroyed, it releases its ownership on
223
the pointer. Since C++ calls the destructors on objects that are about to go out of scope,
224
we are guaranteed that we will keep the reference counts right even if we unexpectedly
225
leave a routine with an exception.</p>
227
<p>As a matter of philosophy, CXX/Objects.hxx prevents the creation of instances of its
228
classes unless the instance will be a valid instance of its class. When an attempt is made
229
to create an object that will not be valid, an exception is thrown.</p>
231
<p>Class <cite>Object</cite> represents the most general kind of Python object. The rest of the classes
232
that represent Python objects inherit from it.</p>
241
Sequence -> SeqBase<T>
245
Mapping -> MapBase<T>
250
<p>There are several constructors for each of these classes. For example, you can create
251
an <cite>Int</cite> from an integer as in</p>
255
<p>However, you can also create an instance of one of these classes using any <cite>PyObject*</cite> or
256
another <cite>Object</cite>. If the corresponding Python object does not actually have the type
257
desired, an exception is thrown. This is accomplished as follows. Class <cite>Object</cite> defines a
258
virtual function <cite>accepts</cite>:</p>
260
<pre>virtual bool accepts(PyObject* p)</pre>
262
<p>The base class version of <cite>accepts</cite> returns true for any pointer p except 0. This means
263
we can create an Object using any <cite>PyObject *</cite>, or from any other
264
<cite>Object</cite>. However, if we attempt to create an <cite>Int</cite> from a <cite>PyObject *</cite>,
265
the overridding version
266
of <cite>accepts</cite> in class <cite>Int</cite> will only accept pointers that correspond to Python ints.
267
Therefore if we have a <cite>Tuple t</cite> and we wish to get the first element and be sure it is an
268
<cite>Int</cite>, we do</p>
270
<pre>Int first_element = t[0]</pre>
272
<p>This will not only accomplish the goal of extracting the first element of the <cite>Tuple t</cite>,
273
but it will ensure that the result is an <cite>Int</cite>. If not, an exception is thrown. The
274
exception mechanism is discussed later.</p>
276
<h2>Class Object</h2>
278
<p>Class <cite>Object</cite> serves as the base class for the other classes. Its default constructor
279
constructs a <cite>Py_None</cite>, the unique object of Python type <cite>None</cite>. The interface to <cite>Object</cite>
280
consists of a large number of methods corresponding to the operations that are defined for
281
every Python object. In each case, the methods throw an exception if anything goes
284
<p>There is no method corresponding to <cite>PyObject_SetItem</cite> with an arbitrary Python object
285
as a key. Instead, create an instance of a more specific child of <cite>Object</cite> and use the
286
appropriate facilities.</p>
288
<p>The comparison operators use the Python comparison function to compare values. The
289
method <cite>is</cite> is available to test for absolute identity.</p>
291
<p>A conversion to standard library string type <cite>std::string</cite> is supplied using method
292
<cite>as_string</cite>. Stream output of PyCXX <cite>Object</cite> instances uses this conversion,
293
which in turn uses the Python object's str() representation.</p>
295
<p>All the numeric operators are defined on all possible combinations of <cite>Object</cite>,
296
<cite>long</cite>, and <cite>double</cite>. These use the corresponding Python operators,
297
and should the operation fail for some reason, an exception is thrown.</p>
299
<h3>Dealing with pointers returned by the Python C API</h3>
301
<p>Often, <cite>PyObject *</cite> pointers are acquired from some function,
302
particularly functions in the Python C API. If you wish to make an object from the pointer
303
returned by such a function, you need to know if the function returns you an <i>owned</i>
304
or <i>unowned</i> reference. Unowned references are unusual but there are some cases where
305
unowned references are returned.</p>
307
<p>Usually, <cite>Object</cite> and its children acquire a new reference when constructed from a
308
<cite>PyObject *</cite>. This is usually not the right behavior if the reference comes from one
309
of the Python C API calls.</p>
311
<p>If p is an owned reference, you can add the boolean <cite>true</cite> as an extra
312
argument in the creation routine, <cite>Object(p, true)</cite>, or use the function <cite>asObject(p)</cite> which
313
returns an <cite>Object</cite> created using the owned reference. For example, the routine
314
<cite>PyString_FromString</cite> returns an owned reference to a Python string object. You could write:</p>
316
<pre>Object w = asObject( PyString_FromString("my string") );</pre>
318
<p>or using the constructor,</p>
320
<pre>Object w( PyString_FromString("my string"), true );</pre>
322
<p>In fact, you would never do this, since PyCXX has a class String and you can just say: </p>
324
<pre>String w( "my string" );</pre>
326
<p>Indeed, since most of the Python C API is similarly embodied in <cite>Object</cite>
327
and its descendents, you probably will not use asObject all that often.</p>
328
<h3>Table 1: Class Object</h3>
330
<table cellspacing=0 cellpadding=3px width="95%">
333
<th>Name(signature)</th>
337
<td colspan="3"><p align="center"><strong>Basic Methods</strong></td>
340
<td class=code>explicit </td>
341
<td class=code>Object (PyObject* pyob=Py_None, bool owned=false) </td>
342
<td>Construct from pointer. </td>
345
<td class=code>explicit</td>
346
<td class=code>Object (const Object& ob)</td>
347
<td>Copycons; acquires an owned reference.</td>
350
<td class=code>Object&</td>
351
<td class=code>operator= (const Object& rhs) </td>
352
<td>Acquires an owned reference.</td>
355
<td class=code>Object&</td>
356
<td class=code>operator= (PyObject* rhsp) </td>
357
<td>Acquires an owned reference.</td>
360
<td class=code>virtual</td>
361
<td class=code>~Object () </td>
362
<td>Releases the reference.</td>
365
<td class=code>void</td>
366
<td class=code>increment_reference_count() </td>
367
<td>Explicitly increment the count</td>
370
<td class=code>void</td>
371
<td class=code>decrement_reference_count()</td>
372
<td>Explicitly decrement count but not to zero</td>
375
<td class=code>PyObject*</td>
376
<td class=code>operator* () const</td>
377
<td>Lends the pointer</td>
380
<td class=code>PyObject*</td>
381
<td class=code>ptr () const</td>
382
<td>Lends the pointer</td>
385
<td class=code>virtual bool</td>
386
<td class=code>accepts (PyObject *pyob) const</td>
387
<td>Would assignment of pyob to this object succeed?</td>
390
<td class=code>std::string</td>
391
<td class=code>as_string() const</td>
392
<td>str() representation</td>
395
<td colspan="3" align="center"><strong>Python API Interface</strong></td>
398
<td class=code>int</td>
399
<td class=code>reference_count () const </td>
400
<td>reference count</td>
403
<td class=code>Type</td>
404
<td class=code>type () const</td>
405
<td>associated type object</td>
408
<td class=code>String</td>
409
<td class=code>str () const</td>
410
<td>str() representation</td>
413
<td class=code>String</td>
414
<td class=code>repr () const</td>
415
<td>repr () representation</td>
418
<td class=code>bool</td>
419
<td class=code>hasAttr (const std::string& s) const</td>
420
<td>hasattr(this, s)</td>
423
<td class=code>Object</td>
424
<td class=code>getAttr (const std::string& s) const</td>
425
<td>getattr(this, s)</td>
428
<td class=code>Object</td>
429
<td class=code>getItem (const Object& key) const</td>
430
<td>getitem(this, key)</td>
433
<td class=code>long</td>
434
<td class=code>hashValue () const</td>
438
<td class=code>void</td>
439
<td class=code>setAttr (const std::string& s,<br>const Object& value)</td>
440
<td>this.s = value</td>
443
<td class=code>void</td>
444
<td class=code>delAttr (const std::string& s) </td>
448
<td class=code>void</td>
449
<td class=code>delItem (const Object& key) </td>
450
<td>del this[key]</td>
453
<td class=code>bool</td>
454
<td class=code>isCallable () const</td>
455
<td>does this have callable behavior?</td>
458
<td class=code>bool</td>
459
<td class=code>isList () const</td>
460
<td>is this a Python list?</td>
463
<td class=code>bool</td>
464
<td class=code>isMapping () const</td>
465
<td>does this have mapping behaviors?</td>
468
<td class=code>bool</td>
469
<td class=code>isNumeric () const</td>
470
<td>does this have numeric behaviors?</td>
473
<td class=code>bool</td>
474
<td class=code>isSequence () const </td>
475
<td>does this have sequence behaviors?</td>
478
<td class=code>bool</td>
479
<td class=code>isTrue () const</td>
480
<td>is this true in the Python sense?</td>
483
<td class=code>bool</td>
484
<td class=code>isType (const Type& t) const</td>
485
<td>is type(this) == t?</td>
488
<td class=code>bool</td>
489
<td class=code>isTuple() const</td>
490
<td>is this a Python tuple?</td>
493
<td class=code>bool</td>
494
<td class=code>isString() const</td>
495
<td>is this a Python string?</td>
498
<td class=code>bool</td>
499
<td class=code>isUnicode() const</td>
500
<td>is this a Python Unicode string?</td>
503
<td class=code>bool</td>
504
<td class=code>isDict() const</td>
505
<td>is this a Python dictionary?</td>
508
<td colspan="3" align="center"><strong>Comparison Operators</strong></td>
511
<td class=code>bool</td>
512
<td class=code>is(PyObject* pother) const</td>
513
<td>test for identity</td>
516
<td class=code>bool</td>
517
<td class=code>is(const Object& other) const</td>
518
<td>test for identity</td>
521
<td class=code>bool </td>
522
<td class=code>operator==(const Object& o2) const</td>
523
<td>Comparisons use Python cmp</td>
526
<td class=code>bool</td>
527
<td class=code>operator!=(const Object& o2) const</td>
528
<td>Comparisons use Python cmp</td>
531
<td class=code>bool</td>
532
<td class=code>operator>=(const Object& o2) const</td>
533
<td>Comparisons use Python cmp</td>
536
<td class=code>bool</td>
537
<td class=code>operator<=(const Object& o2) const </td>
538
<td>Comparisons use Python cmp</td>
541
<td class=code>bool</td>
542
<td class=code>operator<(const Object& o2) const</td>
543
<td>Comparisons use Python cmp</td>
546
<td class=code>bool</td>
547
<td class=code>operator>(const Object& o2) const</td>
548
<td>Comparisons use Python cmp</td>
552
<h1>The Basic Types</h1>
554
<p>Corresponding to each of the basic Python types is a class that inherits from Object.
555
Here are the interfaces for those types. Each of them inherits from Object and therefore
556
has all of the inherited methods listed for Object. Where a virtual function is overridden
557
in a class, the name is underlined. </p>
561
<p>Class Type corresponds to Python type objects. There is no default constructor.</p>
563
<h3>Table 2: class Type</h3>
565
<table cellspacing=0 cellpadding=3px width="95%">
568
<th>Name and Signature</th>
572
<td class=code>explicit</td>
573
<td class=code>Type (PyObject* pyob, bool owned = false)</td>
577
<td class=code>explicit</td>
578
<td class=code>Type (const Object& ob)</td>
582
<td class=code>explicit</td>
583
<td class=code>Type(const Type& t)</td>
587
<td class=code>Type&</td>
588
<td class=code>operator= (const Object& rhs) </td>
592
<td class=code>Type&</td>
593
<td class=code>operator= (PyObject* rhsp) </td>
597
<td class=code>virtual bool</td>
598
<td class=code><u>accepts</u> (PyObject *pyob) const</td>
599
<td>Uses PyType_Check</td>
605
<p>Class Int, derived publically from Object, corresponds to Python ints. Note that the
606
latter correspond to C long ints. Class Int has an implicit user-defined conversion to
607
long int. All constructors, on the other hand, are explicit. The default constructor
608
creates a Python int zero.</p>
610
<h3>Table 3: class Int</h3>
613
<table cellspacing=0 cellpadding=3px width="95%">
616
<th>Name and Signature</td>
620
<td class=code>explicit</td>
621
<td class=code>Int (PyObject *pyob, bool owned= false, bool owned = false)</td>
625
<td class=code>explicit</td>
626
<td class=code>Int (const Int& ob)</td>
630
<td class=code>explicit</td>
631
<td class=code>Int (long v = 0L)</td>
632
<td>Construct from long</td>
635
<td class=code>explicit</td>
636
<td class=code>Int (int v)</td>
637
<td>Contruct from int</td>
640
<td class=code>explicit</td>
641
<td class=code>Int (const Object& ob)</td>
645
<td class=code>Int&</td>
646
<td class=code>operator= (const Object& rhs)</td>
650
<td class=code>Int&</td>
651
<td class=code>operator= (PyObject* rhsp)</td>
655
<td class=code>virtual bool </td>
656
<td class=code> (PyObject *pyob) const </td>
657
<td>Based on PyInt_Check</td>
660
<td class=code>long</td>
661
<td class=code>operator long() const </td>
662
<td><em>Implicit</em> conversion to long int</td>
665
<td class=code>Int&</td>
666
<td class=code>operator= (int v)</td>
667
<td>Assign from int</td>
670
<td class=code>Int&</td>
671
<td class=code>operator= (long v) </td>
672
<td>Assign from long</td>
680
<p>Class Long, derived publically from Object, corresponds to Python type long. In Python,
681
a long is an integer type of unlimited size, and is usually used for applications such as
682
cryptography, not as a normal integer. Implicit conversions to both double and long are
683
provided, although the latter may of course fail if the number is actually too big. All
684
constructors are explicit. The default constructor produces a Python long zero.</p>
686
<h3>Table 4: Class Long</h3>
688
<table cellspacing=0 cellpadding=3px width="95%">
691
<th>Name and Signature</td>
695
<td class=code>explicit</td>
696
<td class=code>Long (PyObject *pyob</a>, bool owned = false)</td>
700
<td class=code>explicit</td>
701
<td class=code>Long (const Int& ob)</td>
705
<td class=code>explicit</td>
706
<td class=code>Long (long v = 0L)</td>
707
<td>Construct from long</td>
710
<td class=code>explicit</td>
711
<td class=code>Long (int v)</td>
712
<td>Contruct from int</td>
715
<td class=code>explicit</td>
716
<td class=code>Long (const Object& ob)</td>
720
<td class=code>Long&</td>
721
<td class=code>operator= (const Object& rhs)</td>
725
<td class=code>Long&</td>
726
<td class=code>operator= (PyObject* rhsp)</td>
730
<td class=code>virtual bool</td>
731
<td class=code>(PyObject *pyob) const </td>
732
<td>Based on PyLong_Check</td>
735
<td class=code>double</td>
736
<td class=code>operator double() const </td>
737
<td><em>Implicit</em> conversion to double</td>
740
<td class=code>long</td>
741
<td class=code>operator long() const</td>
742
<td><em>Implicit</em> conversion to long</td>
745
<td class=code>Long&</td>
746
<td class=code>operator= (int v)</td>
747
<td>Assign from int</td>
750
<td class=code>Long&</td>
751
<td class=code>operator= (long v) </td>
752
<td>Assign from long</td>
758
<p>Class Float corresponds to Python floats, which in turn correspond to C double. The
759
default constructor produces the Python float 0.0. </p>
761
<h3>Table 5: Class Float</h3>
763
<table cellspacing=0 cellpadding=3px width="95%">
766
<th>Name and Signature</td>
770
<td class=code>explicit</td>
771
<td class=code>Float (PyObject *pyob</a>, bool owned = false)
777
<td class=code>Float (const Float& f) </td>
778
<td>Construct from float</td>
781
<td class=code>explicit</td>
782
<td class=code>Float (double v=0.0)</td>
783
<td>Construct from double</td>
786
<td class=code>explicit</td>
787
<td class=code>Float (const Object& ob)</td>
791
<td class=code>Float&</td>
792
<td class=code>operator= (const Object& rhs)</td>
796
<td class=code>Float&</td>
797
<td class=code>operator= (PyObject* rhsp)</td>
801
<td class=code>virtual bool </td>
802
<td class=code>accepts (PyObject *pyob) const</td>
803
<td>Based on PyFloat_Check</td>
806
<td class=code>double </td>
807
<td class=code>operator double () const</td>
808
<td><em>Implicit</em> conversion to double</td>
811
<td class=code>Float& </td>
812
<td class=code>operator= (double v)</td>
813
<td>Assign from double</td>
816
<td class=code>Float& </td>
817
<td class=code>operator= (int v)</td>
818
<td>Assign from int</td>
821
<td class=code>Float& </td>
822
<td class=code>operator= (long v)</td>
823
<td>Assign from long</td>
826
<td class=code>Float& </td>
827
<td class=code>operator= (const Int& iob)</td>
828
<td>Assign from Int</td>
834
<p>PyCXX implements a quite sophisticated wrapper class for Python sequences. While every
835
effort has been made to disguise the sophistication, it may pop up in the form of obscure
836
compiler error messages, so in this documentation we will first detail normal usage and
837
then discuss what is under the hood.</p>
839
<p>The basic idea is that we would like the subscript operator [] to work properly, and to
840
be able to use STL-style iterators and STL algorithms across the elements of the sequence.</p>
842
<p>Sequences are implemented in terms of a templated base class, SeqBase<T>. The
843
parameter T is the answer to the question, sequence of what? For Lists, for example, T is
844
Object, because the most specific thing we know about an element of a List is simply that
845
it is an Object. (Class List is defined below; it is a descendent of Object that holds a
846
pointer to a Python list). For strings, T is Char, which is a wrapper in turn of Python
847
strings whose length is one.</p>
849
<p>For convenience, the word <strong>Sequence</strong> is a typedef of SeqBase<Object>.</p>
851
<h2>General sequences</h2>
853
<p>Suppose you are writing an extension module method that expects the first argument to
854
be any kind of Python sequence, and you wish to return the length of that sequence. You
857
<pre>static PyObject*
858
my_module_seqlen (PyObject *self, PyObject* args) {
861
Tuple t(args); // set up a Tuple pointing to the arguments.
863
throw PyException("Incorrect number of arguments to seqlen.");
864
Sequence s = t[0]; // get argument and be sure it is a sequence
865
return new_reference_to(Int(s.length()));
867
catch(const PyException&)
873
<p>As we will explain later, the try/catch structure converts any errors, such as the
874
first argument not being a sequence, into a Python exception.</p>
876
<h3>Subscripting</h3>
878
<p>When a sequence is subscripted, the value returned is a special kind of object which
879
serves as a proxy object. The general idea of proxy objects is discussed in Scott Meyers'
880
book, "More Effective C++". Proxy objects are necessary because when one
881
subscripts a sequence it is not clear whether the value is to be used or the location
882
assigned to. Our proxy object is even more complicated than normal because a sequence
883
reference such as s[i] is not a direct reference to the i'th object of s. </p>
885
<p>In normal use, you are not supposed to notice this magic going on behind your back. You
890
s[2] = t + s[1]</pre>
892
<p>and here is what happens: s[1] returns a proxy object. Since there is no addition
893
operator in Object that takes a proxy as an argument, the compiler decides to invoke an
894
automatic conversion of the proxy to an Object, which returns the desired component of s.
895
The addition takes place, and then there is an assignment operator in the proxy class
896
created by the s[2], and that assignment operator stuffs the result into the 2 component
899
<p>It is possible to fool this mechanism and end up with a compiler failing to admit that
900
a s[i] is an Object. If that happens, you can work around it by writing Object(s[i]),
901
which makes the desired implicit conversion, explicit.</p>
905
<p>Each sequence class provides the following interface. The class seqref<T> is the
906
proxy class. We omit the details of the iterator, const_iterator, and seqref<T>
907
here. See CXX_Objects.h if necessary. The purpose of most of this interface is to satisfy
908
requirements of the STL.</p>
910
<h3>The SeqBase<T> Interface</h3>
912
<p>SeqBase<T> inherits from Object.</p>
914
<table cellspacing=0 cellpadding=3px width="95%">
920
<td class=code>typedef int </td>
921
<td class=code>size_type</td>
924
<td class=code>typedef seqref<T></td>
925
<td class=code>reference</td>
928
<td class=code>typedef T </td>
929
<td class=code>const_reference</td>
932
<td class=code>typedef seqref<T>*</td>
933
<td class=code>pointer</td>
936
<td class=code>typedef int </td>
937
<td class=code>difference_type</td>
940
<td class=code>virtual size_type</td>
941
<td class=code>max_size() const</td>
944
<td class=code>virtual size_type </td>
945
<td class=code>capacity() const;</td>
948
<td class=code>virtual void </td>
949
<td class=code>swap(SeqBase<T>& c);</td>
952
<td class=code>virtual size_type </td>
953
<td class=code>size () const;</td>
956
<td class=code>explicit </td>
957
<td class=code>SeqBase<T> ();</td>
960
<td class=code>explicit </td>
961
<td class=code>SeqBase<T> (PyObject* pyob, bool owned = false);</td>
964
<td class=code>explicit </td>
965
<td class=code>SeqBase<T> (const Object& ob);</td>
968
<td class=code>SeqBase<T>& </td>
969
<td class=code>operator= (const Object& rhs);</td>
972
<td class=code>SeqBase<T>& </td>
973
<td class=code>operator= (PyObject* rhsp);</td>
976
<td class=code>virtual bool </td>
977
<td class=code>accepts (PyObject *pyob) const;</td>
980
<td class=code>size_type </td>
981
<td class=code>length () const ;</td>
984
<td class=code>const T </td>
985
<td class=code>operator[](size_type index) const; </td>
988
<td class=code>seqref<T> </td>
989
<td class=code>operator[](size_type index); </td>
992
<td class=code>virtual T </td>
993
<td class=code>getItem (size_type i) const;</td>
996
<td class=code>virtual void </td>
997
<td class=code>setItem (size_type i, const T& ob);</td>
1000
<td class=code>SeqBase<T> </td>
1001
<td class=code>repeat (int count) const;</td>
1004
<td class=code>SeqBase<T> </td>
1005
<td class=code>concat (const SeqBase<T>& other) const ;</td>
1008
<td class=code>const T </td>
1009
<td class=code>front () const;</td>
1012
<td class=code>seqref<T> </td>
1013
<td class=code>front();</td>
1016
<td class=code>const T </td>
1017
<td class=code>back () const;</td>
1020
<td class=code>seqref<T> </td>
1021
<td class=code>back(); </td>
1024
<td class=code>void </td>
1025
<td class=code>verify_length(size_type required_size);</td>
1028
<td class=code>void </td>
1029
<td class=code>verify_length(size_type min_size, size_type max_size);</td>
1032
<td class=code>class</td>
1033
<td class=code>iterator;</td>
1036
<td class=code>iterator </td>
1037
<td class=code>begin (); </td>
1040
<td class=code>iterator </td>
1041
<td class=code>end ();</td>
1044
<td class=code>class </td>
1045
<td class=code>const_iterator;</td>
1048
<td class=code>const_iterator </td>
1049
<td class=code>begin () const;</td>
1052
<td class=code>const_iterator </td>
1053
<td class=code>end () const;</td>
1057
<p>Any heir of class Object that has a sequence behavior should inherit from class
1058
SeqBase<T>, where T is specified as the type of object that represents the
1059
individual elements of the sequence. The requirements on T are that it has a constructor
1060
that takes a PyObject* as an argument, that it has a default constructor, a copy
1061
constructor, and an assignment operator. In short, any properly defined heir of Object
1064
<h2>Classes Char and String</h2>
1066
<p>Python strings are unusual in that they are immutable sequences of characters. However,
1067
there is no character type per se; rather, when subscripted strings return a string of
1068
length one. To simulate this, we define two classes Char and String. The Char class
1069
represents a Python string object of length one. The String class represents a Python
1070
string, and its elements make up a sequence of Char's.</p>
1072
<p>The user interface for Char is limited. Unlike String, for example, it is not a
1075
<h3>The Char interface</h3>
1077
<p>Char inherits from Object.</p>
1079
<table cellspacing=0 cellpadding=3px width="95%">
1085
<td class=code>explicit</td>
1086
<td class=code>Char (PyObject *pyob, bool owned = false)</td>
1089
<td class=code></td>
1090
<td class=code>Char (const Object& ob) </td>
1093
<td class=code></td>
1094
<td class=code>Char (const std::string& v = "") </td>
1097
<td class=code></td>
1098
<td class=code>Char (char v)</td>
1101
<td class=code></td>
1102
<td class=code>Char (Py_UNICODE v)</td>
1105
<td class=code>Char&</td>
1106
<td class=code>operator= (const std::string& v)</td>
1109
<td class=code>Char&</td>
1110
<td class=code>operator= (char v) </td>
1113
<td class=code>Char&</td>
1114
<td class=code>operator= (Py_UNICODE v) </td>
1117
<td class=code>Char&</td>
1118
<td class=code>operator= (std::basic_string<Py_UNICODE> v) </td>
1121
<td class=code></td>
1122
<td class=code>operator String() const</td>
1125
<td class=code></td>
1126
<td class=code>operator std::string () const </td>
1130
<h3>The String Interface</h3>
1132
<p>String inherits from SeqBase<Char>.</p>
1134
<table cellspacing=0 cellpadding=3px width="95%">
1140
<td class=code>explicit </td>
1141
<td class=code>String (PyObject *pyob, bool owned = false)</td>
1144
<td class=code> </td>
1145
<td class=code>String (const Object& ob)</td>
1148
<td class=code> </td>
1149
<td class=code>String (const std::string& v = "")</td>
1152
<td class=code> </td>
1153
<td class=code>String (const std::string& v, const char *encoding, const char *error="strict")</td>
1156
<td class=code> </td>
1157
<td class=code>String (const char *s, const char *encoding, const char *error="strict")</td>
1160
<td class=code> </td>
1161
<td class=code>String (const char *s, int len, const char *encoding, const char *error="strict")</td>
1164
<td class=code> </td>
1165
<td class=code>String (const std::string& v, std::string::size_type vsize)</td>
1168
<td class=code> </td>
1169
<td class=code>String (const char* v)</td>
1172
<td class=code>String&</td>
1173
<td class=code>operator= (const std::string& v) </td>
1176
<td class=code>std::string</td>
1177
<td class=code>operator std::string () const</td>
1180
<td class=code>String</td>
1181
<td class=code>encode( const char *encoding, const char *error="strict" )</td>
1184
<td class=code>String</td>
1185
<td class=code>decode( const char *encoding, const char *error="strict" )</td>
1188
<td class=code>std::string</td>
1189
<td class=code>as_std_string() const</td>
1192
<td class=code>unicodestring</td>
1193
<td class=code>as_unicodestring() const</td>
1197
<h2>Class Tuple</h2>
1199
<p>Class Tuple represents Python tuples. A Tuple is a Sequence. There are two kinds of
1200
constructors: one takes a PyObject* as usual, the other takes an integer number as an
1201
argument and returns a Tuple of that length, each component initialized to Py_None. The
1202
default constructor produces an empty Tuple. </p>
1204
<p>Tuples are not immutable, but attempts to assign to their components will fail if the
1205
reference count is not 1. That is, it is safe to set the elements of a Tuple you have just
1206
made, but not thereafter.</p>
1208
<p>Example: create a Tuple containing (1, 2, 4)</p>
1215
<p>Example: create a Tuple from a list:</p>
1219
Tuple t(d.keys())</pre>
1221
<h3>The Tuple Interface</h3>
1223
<p>Tuple inherits from Sequence.. Special run-time checks prevent modification if the
1224
reference count is greater than one.</p>
1226
<table cellspacing=0 cellpadding=3px width="95%">
1233
<td class=code>virtual void</td>
1234
<td class=code>setItem (int offset, const Object&ob) </td>
1235
<td>setItem is overriden to handle tuples properly. </td>
1238
<td class=code>explicit</td>
1239
<td class=code>Tuple (PyObject *pyob, bool owned = false)</td>
1243
<td class=code> </td>
1244
<td class=code>Tuple (const Object& ob)</td>
1248
<td class=code>explicit</td>
1249
<td class=code>Tuple (int size = 0)</td>
1250
<td>Create a tuple of the given size. Items initialize to Py_None. Default is an empty
1254
<td class=code>explicit</td>
1255
<td class=code>Tuple (const Sequence& s)</td>
1256
<td>Create a tuple from any sequence.</td>
1259
<td class=code>Tuple&</td>
1260
<td class=code>operator= (const Object& rhs)</td>
1264
<td class=code>Tuple&</td>
1265
<td class=code>operator= (PyObject* rhsp)</td>
1269
<td class=code>Tuple</td>
1270
<td class=code>getSlice (int i, int j) const </td>
1271
<td>Equivalent to python's t[i:j]</td>
1277
<p>Class List represents a Python list, and the methods available faithfully reproduce the
1278
Python API for lists. A List is a Sequence.</p>
1280
<h3>The List Interface</h3>
1282
<p>List inherits from Sequence.</p>
1284
<table cellspacing=0 cellpadding=3px width="95%">
1291
<td class=code>explicit</td>
1292
<td class=code>List (PyObject *pyob, bool owned = false)</td>
1296
<td class=code> </td>
1297
<td class=code>List (const Object& ob)</td>
1301
<td class=code> </td>
1302
<td class=code>List (int size = 0)</td>
1303
<td>Create a list of the given size. Items initialized to Py_None. Default is an empty list.</td>
1306
<td class=code> </td>
1307
<td class=code>List (const Sequence& s)</td>
1308
<td>Create a list from any sequence.</td>
1311
<td class=code>List&</td>
1312
<td class=code>operator= (const Object& rhs)</td>
1316
<td class=code>List&</td>
1317
<td class=code>operator= (PyObject* rhsp)</td>
1321
<td class=code>List</td>
1322
<td class=code>getSlice (int i, int j) const</td>
1326
<td class=code>void</td>
1327
<td class=code>setSlice (int i, int j, const Object& v) </td>
1331
<td class=code>void</td>
1332
<td class=code>append (const Object& ob)</td>
1336
<td class=code>void</td>
1337
<td class=code>insert (int i, const Object& ob)</td>
1341
<td class=code>void</td>
1342
<td class=code>sort ()</td>
1343
<td>Sorts the list in place, using Python's member function. You can also use
1344
the STL sort function on any List instance.</td>
1347
<td class=code>void</td>
1348
<td class=code>reverse ()</td>
1349
<td>Reverses the list in place, using Python's member function.</td>
1355
<p>A class MapBase<T> is used as the base class for Python objects with a mapping
1356
behavior. The key behavior of this class is the ability to set and use items by
1357
subscripting with strings. A proxy class mapref<T> is defined to produce the correct
1358
behavior for both use and assignment.</p>
1360
<p>For convenience, <cite>Mapping</cite> is a typedef for <cite>MapBase<Object></cite>.</p>
1362
<h3>The MapBase<T> interface</h3>
1364
<p>MapBase<T> inherits from Object. T should be chosen to reflect the kind of
1365
element returned by the mapping.</p>
1367
<table cellspacing=0 cellpadding=3px width="95%">
1374
<td class=code>T</td>
1375
<td class=code>operator[](const std::string& key) const</td>
1379
<td class=code>mapref<T> </td>
1380
<td class=code>operator[](const std::string& key)</td>
1384
<td class=code>int</td>
1385
<td class=code>length () const</td>
1386
<td>Number of entries.</td>
1389
<td class=code>int</td>
1390
<td class=code>hasKey (const std::string& s) const </td>
1391
<td>Is m[s] defined?</td>
1394
<td class=code>T</td>
1395
<td class=code>getItem (const std::string& s) const</td>
1399
<td class=code>virtual void</td>
1400
<td class=code>setItem (const std::string& s, const Object& ob)</td>
1404
<td class=code>void</td>
1405
<td class=code>delItem (const std::string& s)</td>
1409
<td class=code>void</td>
1410
<td class=code>delItem (const Object& s)</td>
1414
<td class=code>List</td>
1415
<td class=code>keys () const</td>
1416
<td>A list of the keys.</td>
1419
<td class=code>List</td>
1420
<td class=code>values () const</td>
1421
<td>A list of the values.</td>
1424
<td class=code>List</td>
1425
<td class=code>items () const</td>
1426
<td>Each item is a key-value pair.</td>
1432
<p>Class Dict represents Python dictionarys. A Dict is a Mapping. Assignment to
1433
subscripts can be used to set the components.</p>
1436
d["Paul Dubois"] = "(925)-422-5426"</pre>
1438
<h3>Interface for Class Dict</h3>
1440
<p>Dict inherits from MapBase<Object>.</p>
1442
<table cellspacing=0 cellpadding=3px width="95%">
1449
<td class=code>explicit</td>
1450
<td class=code>Dict (PyObject *pyob</a>, bool owned = false)</td>
1454
<td class=code> </td>
1455
<td class=code>Dict (const Dict& ob)</td>
1459
<td class=code> </td>
1460
<td class=code>Dict () </td>
1461
<td>Creates an empty dictionary</td>
1464
<td class=code>Dict&</td>
1465
<td class=code>operator= (const Object& rhs)</td>
1469
<td class=code>Dict&</td>
1470
<td class=code>operator= (PyObject* rhsp)</td>
1475
<h1>Other classes and facilities.</h1>
1477
<p>Class Callable provides an interface to those Python objects that support a call
1478
method. Class Module holds a pointer to a module. If you want to create an extension
1479
module, however, see the extension facility. There is a large set of numeric operators.</p>
1481
<h3>Interface to class Callable</h3>
1483
<table cellspacing=0 cellpadding=3px width="95%">
1490
<td class=code>explicit</td>
1491
<td class=code>Callable (PyObject *pyob</a>, bool owned = false)</td>
1495
<td class=code>Callable& </td>
1496
<td class=code>operator= (const Object& rhs)</td>
1500
<td class=code>Callable& </td>
1501
<td class=code>operator= (PyObject* rhsp)</td>
1505
<td class=code>Object</td>
1506
<td class=code>apply(const Tuple& args) const</td>
1507
<td>Call the object with the given arguments</td>
1510
<td class=code>Object</td>
1511
<td class=code>apply(PyObject* pargs = 0) const </td>
1512
<td>Call the object with args as the arguments. Checks that pargs is a tuple.</td>
1516
<h3>Interface to class Module</h3>
1518
<table cellspacing=0 cellpadding=3px width="95%">
1525
<td class=code>explicit</td>
1526
<td class=code>Module (PyObject* pyob, bool owned = false)</td>
1530
<td class=code>explicit</td>
1531
<td class=code>Module (const std::string name)</td>
1532
<td>Construct from name of module; does the import if needed.</td>
1535
<td class=code></td>
1536
<td class=code>Module (const Module& ob) </td>
1537
<td>Copy constructor</td>
1540
<td class=code>Module&</td>
1541
<td class=code>operator= (const Object& rhs) </td>
1545
<td class=code>Module&</td>
1546
<td class=code>operator= (PyObject* rhsp) </td>
1551
<h3>Numeric interface</h3>
1553
<p>Unary operators for plus and minus, and binary operators +, -, *, /, and % are defined
1554
for pairs of objects and for objects with scalar integers or doubles (in either
1555
order). Functions abs(ob) and coerce(o1, o2) are also defined. </p>
1557
<p>The signature for coerce is:</p>
1559
<pre>inline std::pair<Object,Object> coerce(const Object& a, const Object& b)</pre>
1561
<p>Unlike the C API function, this simply returns the pair after coercion.</p>
1565
<p>Any object can be printed using stream I/O, using std::ostream& operator<<
1566
(std::ostream& os, const Object& ob). The object's str() representation is
1567
converted to a standard string which is passed to std::ostream& operator<<
1568
(std::ostream& os, const std::string&).</p>
1572
<p>The Python exception facility and the C++ exception facility can be merged via the use
1573
of try/catch blocks in the bodies of extension objects and module functions.</p>
1575
<h3>Class Exception and its children</h3>
1577
<p>A set of classes is provided. Each is derived from class Exception, and represents a
1578
particular sort of Python exception, such as IndexError, RuntimeError, ValueError. Each of
1579
them (other than Exception) has a constructor which takes an explanatory string as an
1580
argument, and is used in a throw statement such as:</p>
1582
<pre>throw IndexError("Index too large in MyObject access.");</pre>
1584
<p>If in using a routine from the Python API, you discover that it has returned a NULL
1585
indicating an error, then Python has already set the error message. In that case you
1586
merely throw Exception.</p>
1588
<h3>List of Exceptions</h3>
1590
<p>The exception hierarchy mirrors the Python exception hierarchy. The concrete exception
1591
classes are shown here.</p>
1593
<table cellspacing=0 cellpadding=3px width="95%">
1596
<th>Interface for class Exception</th>
1599
<td class=code>explicit </td>
1600
<td class=code>Exception()</td>
1603
<td class=code> </td>
1604
<td class=code>Exception (const std::string& reason) </td>
1607
<td class=code> </td>
1608
<td class=code>Exception (PyObject* exception, const std::string& reason) </td>
1611
<td class=code>void </td>
1612
<td class=code>clear()</td>
1615
<td class=code></td>
1616
<td>Constructors for other children of class Exception</td>
1619
<td class=code> </td>
1620
<td class=code>TypeError (const std::string& reason)</td>
1623
<td class=code> </td>
1624
<td class=code>IndexError (const std::string& reason)</td>
1627
<td class=code> </td>
1628
<td class=code>AttributeError (const std::string& reason)</td>
1631
<td class=code> </td>
1632
<td class=code>NameError (const std::string& reason)</td>
1635
<td class=code> </td>
1636
<td class=code>RuntimeError (const std::string& reason)</td>
1639
<td class=code> </td>
1640
<td class=code>SystemError (const std::string& reason)</td>
1643
<td class=code> </td>
1644
<td class=code>KeyError (const std::string& reason)</td>
1647
<td class=code> </td>
1648
<td class=code>ValueError (const std::string& reason)</td>
1651
<td class=code> </td>
1652
<td class=code>OverflowError (const std::string& reason)</td>
1655
<td class=code> </td>
1656
<td class=code>ZeroDivisionError (const std::string& reason)</td>
1659
<td class=code> </td>
1660
<td class=code>MemoryError (const std::string& reason)</td>
1663
<td class=code> </td>
1664
<td class=code>SystemExit (const std::string& reason)</td>
1668
<h2>Using Exceptions in extension methods</h2>
1670
<p>The exception facility allows you to integrate the C++ and Python exception mechanisms.
1671
To do this, you must use the style described below when writing module methods in the old
1674
<p>Note: If using the ExtensionModule or PythonExtension mechanisms described below, the
1675
method handlers include exception handling so that you only need to use exceptions
1676
explicitly in unusual cases.</p>
1678
<h3>Catching Exceptions from the Python API or PyCXX.</h3>
1680
<p>When writing an extension module method, you can use the following boilerplate. Any
1681
exceptions caused by the Python API or PyCXX itself will be converted into a Python
1682
exception. Note that Exception is the most general of the exceptions listed above, and
1683
therefore this one catch clause will serve to catch all of them. You may wish to catch
1684
other exceptions, not in the Exception family, in the same way. If so, you need to make
1685
sure you set the error in Python before returning.</p>
1687
<pre>static PyObject *
1688
some_module_method(PyObject* self, PyObject* args)
1690
Tuple a( args ); // we know args is a Tuple
1693
...calculate something from a...
1694
return ...something, usually of the form new_reference_to(some Object);
1696
catch( const Exception& )
1698
//Exception caught, passing it on to Python
1704
<h3>How to clear an Exception</h3>
1706
<p>If you anticipate that an Exception may be thrown and wish to recover from it, change
1707
the catch phrase to set a reference to an Exception, and use the method clear() from class
1708
Exception to clear it.:</p>
1710
<pre>catch( Exception& e )
1713
...now decide what to do about it...
1717
<h2>Extension Facilities</h2>
1719
<p>CXX/Extensions.hxx provides facilities for:
1722
<li>Creating a Python extension module</li>
1723
<li>Creating new Python extension types</li>
1726
<p>These facilities use CXX/Objects.hxx and its support file cxxsupport.cxx.</p>
1728
<p>If you use CXX/Extensions.hxx you must also include source files cxxextensions.c and
1729
cxx_extensions.cxx</p>
1731
<h3>Creating an Python extension module</h3>
1733
<p>The usual method of creating a Python extension module is to declare and initialize its
1734
method table in C. This requires knowledge of the correct form for the table and the order
1735
in which entries are to be made into it, and requires casts to get everything to compile
1736
without warning. The PyCXX header file CXX/Extensions.h offers a simpler method. Here is a
1737
sample usage, in which a module named "example" is created. Note that two
1738
details are necessary:
1741
<li>The initialization function must be declared to have external C linkage and to have the
1742
expected name. This is a requirement imposed by Python</li>
1743
<li>The ExtensionModule object must have a storage class that survives the call to the
1744
initialization function. This is most easily accomplished by using a static local inside
1745
the initialization function, as in initexample below.</li>
1748
<p>To create an extension module, you inherit from class ExtensionModule templated on
1749
yourself: In the constructor, you make calls to register methods of this class with Python
1750
as extension module methods. In this example, two methods are added (this is a simplified
1751
form of the example in Demo/example.cxx):</p>
1753
<pre>class example_module : public ExtensionModule<example_module>
1757
: ExtensionModule<example_module>( "example" )
1759
add_varargs_method("sum", &example_module::ex_sum, "sum(arglist) = sum of arguments");
1760
add_varargs_method("test", &example_module::ex_test, "test(arglist) runs a test suite");
1762
initialize( "documentation for the example module" );
1765
virtual ~example_module() {}
1768
Object ex_sum(const Tuple &a) { ... }
1769
Object ex_test(const Tuple &a) { ... }
1773
<p>To initialize the extension, you just instantiate one static instance (static so it
1774
does not destroy itself!):</p>
1776
<pre>void initexample()
1778
static example_module* example = new example_module;
1781
<p>The methods can be written to take Tuples as arguments and return Objects. If
1782
exceptions occur they are trapped for you and a Python exception is generated. So, for
1783
example, the implementation of ex_sum might be:</p>
1785
<pre>Object ex_sum (const Tuple &a)
1788
for( int i = 0; i < a.length(); i++ )
1796
<p>class ExtensionModule contains methods to return itself as a Module object, or to
1797
return its dictionary.</p>
1799
<h3>Interface to class ExtensionModule</h3>
1801
<table cellspacing=0 cellpadding=3px width="95%">
1808
<td class=code>explicit</td>
1809
<td class=code>ExtensionModule (char* name) </td>
1810
<td>Create an extension module named "name"</td>
1813
<td class=code>virtual </td>
1814
<td class=code>~ExtensionModule () </td>
1818
<td class=code>Dict</td>
1819
<td class=code>moduleDictionary() const</td>
1820
<td>Returns the module dictionary; module must be initialized.</td>
1823
<td class=code>Module</td>
1824
<td class=code>module() const</td>
1825
<td>This module as a Module.</td>
1828
<td class=code>void </td>
1829
<td class=code>add_varargs_method (char *name, method_varargs_function_t method, char *documentation="")</td>
1830
<td>Add a method to the module.</td>
1833
<td class=code>void </td>
1834
<td class=code>add_keyword_method (char *name, method_keyword_function_t method, char *documentation=""</td>
1835
<td>Add a method that takes keywords</td>
1838
<td class=code>void</td>
1839
<td class=code>initialize() (protected, call from constructor)</td>
1840
<td>Initialize the module once all methods have been added. </td>
1844
<p>The signatures above are:</p>
1846
<pre>typedef Object (T::*method_varargs_function_t)( const Tuple &args );
1847
typedef Object (T::*method_keyword_function_t)( const Tuple &args, const Dict &kws
1850
<p>That is, the methods take a Tuple or a Tuple and a Dict, and return an Object. The
1851
example below has an & in front of the name of the method; we found one compiler that
1854
<h2>Creating a Python extension type</h2>
1856
<p>One of the great things about Python is the way you can create your own object types
1857
and have Python welcome them as first-class citizens. Unfortunately, part of the way you
1858
have to do this is not great. Key to the process is the creation of a Python "type
1859
object". All instances of this type must share a reference to this one unique type
1860
object. The type object itself has a multitude of "slots" into which the
1861
addresses of functions can be added in order to give the object the desired behavior. </p>
1863
<p>Creating extension objects is of course harder since you must specify
1864
how the object behaves and give it methods. This is shown in some detail in the example
1865
range.h and range.cxx, with the test routine rangetest.cxx, in directory Demo. If you have never
1866
created a Python extension before, you should read the Extension manual first and be very
1867
familiar with Python's "special class methods". Then what follows will make more
1870
<p>The basic idea is to inherit from PythonExtension templated on your self</p>
1872
<pre>class MyObject: public PythonExtension<MyObject> {...}</pre>
1874
<p>As a consequence:
1877
<li>MyObject is a child of PyObject, so that a MyObject* is-a PyObject*. </li>
1878
<li>A static method <cite>check(PyObject*)</cite> is created in class MyObject. This function
1879
returns a boolean, testing whether or not the argument is in fact a pointer to an instance
1881
<li>The user can connect methods of MyObject to Python so that they are methods on MyObject
1882
objects. Each such method has the signature:<br>
1883
Object method_name (const Tuple& args).</li>
1884
<li>The user can override virtual methods of PythonExtension in order to set behaviors.</li>
1885
<li>A method is created to handle the deletion of an instance if and when its reference
1886
count goes to zero. This method ensures the calling of the class destructor ~MyObject(),
1887
if any, and releases the memory (see below).</li>
1888
<li>Both automatic and heap-based instances of MyObject can be created.</li>
1891
<h3>Sample usage of PythonExtension</h3>
1893
<p>Here is a brief overview. You create a class that inherits from PythonExtension
1894
templated upon itself. You override various methods from PythonExtension to implement
1895
behaviors, such as getattr, sequence_item, etc. You can also add methods to the object
1896
that are usable from Python using a similar scheme as for module methods above. </p>
1898
<p>One of the consequences of inheriting from PythonExtension is that you are inheriting
1899
from PyObject itself. So your class is-a PyObject and instances of it can be passed to the
1900
Python C API. Note: this example uses the namespace feature of PyCXX.</p>
1902
<p>Hint: You can avoid needing to specify the Py:: prefix if you include the C++ statement
1903
<cite>using Py;</cite> at the top of your files.</p>
1905
<pre>class range: public Py::PythonExtension<range>
1908
... constructors, data, etc.
1909
... methods not callable from Python
1910
// initializer, see below
1911
static void init_type();
1912
// override functions from PythonExtension
1913
virtual Py::Object repr();
1914
virtual Py::Object getattr( const char *name );
1916
virtual int sequence_length();
1917
virtual Py::Object sequence_item( int i );
1918
virtual Py::Object sequence_concat( const Py::Object &j );
1919
virtual Py::Object sequence_slice( int i, int j );
1921
// define python methods of this object
1922
Py::Object amethod (const Py::Tuple& args);
1923
Py::Object value (const Py::Tuple& args);
1924
Py::Object assign (const Py::Tuple& args);
1928
To initialize the type we provide a static method that we can call from some module's
1929
initializer. We set the name, doc string, and indicate which behaviors range objects
1930
support. Then we adds the methods.</p>
1932
<pre>void range::init_type()
1934
behaviors().name("range");
1935
behaviors().doc("range objects: start, stop, step");
1936
behaviors().supportRepr();
1937
behaviors().supportGetattr();
1938
behaviors().supportSequenceType();
1940
add_varargs_method("amethod", &range::amethod,
1941
"demonstrate how to document amethod");
1942
add_varargs_method("assign", &range::assign);
1943
add_varargs_method("value", &range::value);
1947
<p>Do not forget to add the call range::init_type() to some module's init function. You will want
1948
a method in some module that can create range objects, too.</p>
1950
<h3>Interface to PythonExtension <T></h3>
1952
<p>Your extension class T inherits PythonExtension<T>.</p>
1954
<table cellspacing=0 cellpadding=3px width="95%">
1961
<td class=code>virtual </td>
1962
<td class=code>~PythonExtension<T>() </td>
1966
<td class=code>PyTypeObject* </td>
1967
<td class=code>type_object() const</td>
1968
<td>Returns the object type object.</td>
1971
<td class=code>int</td>
1972
<td class=code>check (PyObject* p)</td>
1976
<td colspan="3"><strong>Protected </strong></td>
1979
<td class=code>void </td>
1980
<td class=code>add_varargs_method (char *name, method_keyword_function_t method, char *documentation=""</td>
1981
<td>Add a method that takes arguments</td>
1984
<td class=code>void </td>
1985
<td class=code>add_keyword_method (char *name, method_keyword_function_t method, char *documentation=""</td>
1986
<td>Add a method that takes keywords</td>
1989
<td class=code>static PythonType&</td>
1990
<td class=code>behaviors()</td>
1991
<td>The type object</td>
1994
<td class=code>void</td>
1995
<td class=code>initialize() (protected, call from constructor)</td>
1996
<td>Initialize the module once all methods have been added. </td>
2000
<p>As before the signatures for the methods are Object mymethod(const Tuple&
2001
args) and Object mykeywordmethod (const Tuple& args, const Dict& keys). In this
2002
case, the methods must be methods of T.</p>
2004
<p>To set the behaviors of the object you override some or all of these methods from
2005
PythonExtension<T>:</p>
2007
<pre>virtual int print( FILE *, int );
2008
virtual Object getattr( const char * );
2009
virtual int setattr( const char *, const Object & );
2010
virtual Object getattro( const Object & );
2011
virtual int setattro( const Object &, const Object & );
2012
virtual int compare( const Object & );
2013
virtual Object repr();
2014
virtual Object str();
2015
virtual long hash();
2016
virtual Object call( const Object &, const Object & );
2019
virtual int sequence_length();
2020
virtual Object sequence_concat( const Object & );
2021
virtual Object sequence_repeat( int );
2022
virtual Object sequence_item( int );
2023
virtual Object sequence_slice( int, int );
2024
virtual int sequence_ass_item( int, const Object & );
2025
virtual int sequence_ass_slice( int, int, const Object & );
2028
virtual int mapping_length();
2029
virtual Object mapping_subscript( const Object & );
2030
virtual int mapping_ass_subscript( const Object &, const Object & );
2033
virtual int number_nonzero();
2034
virtual Object number_negative();
2035
virtual Object number_positive();
2036
virtual Object number_absolute();
2037
virtual Object number_invert();
2038
virtual Object number_int();
2039
virtual Object number_float();
2040
virtual Object number_long();
2041
virtual Object number_oct();
2042
virtual Object number_hex();
2043
virtual Object number_add( const Object & );
2044
virtual Object number_subtract( const Object & );
2045
virtual Object number_multiply( const Object & );
2046
virtual Object number_divide( const Object & );
2047
virtual Object number_remainder( const Object & );
2048
virtual Object number_divmod( const Object & );
2049
virtual Object number_lshift( const Object & );
2050
virtual Object number_rshift( const Object & );
2051
virtual Object number_and( const Object & );
2052
virtual Object number_xor( const Object & );
2053
virtual Object number_or( const Object & );
2054
virtual Object number_power( const Object &, const Object & );
2057
virtual int buffer_getreadbuffer( int, void** );
2058
virtual int buffer_getwritebuffer( int, void** );
2059
virtual int buffer_getsegcount( int* );</pre>
2061
<p>Note that dealloc is not one of the functions you can override. That is what your
2062
destructor is for. As noted below, dealloc behavior is provided for you by
2063
PythonExtension.</p>
2065
<h3>Type initialization</h3>
2067
<p>To initialize your type, supply a static public member function that can be called
2068
from the extension module. In that function, obtain the PythonType object by calling
2069
behaviors() and apply appropriate "support" methods from PythonType to turn on
2070
the support for that behavior or set of behaviors.</p>
2072
<pre>void supportPrint(void);
2073
void supportGetattr(void);
2074
void supportSetattr(void);
2075
void supportGetattro(void);
2076
void supportSetattro(void);
2077
void supportCompare(void);
2078
void supportRepr(void);
2079
void supportStr(void);
2080
void supportHash(void);
2081
void supportCall(void);
2083
void supportSequenceType(void);
2084
void supportMappingType(void);
2085
void supportNumberType(void);
2086
void supportBufferType(void);</pre>
2088
<p>Then call add_varargs_method or add_keyword_method to add any methods desired to the
2091
<h3>Notes on memory management and extension objects</h3>
2093
<p>Normal Python objects exist only on the heap. That is unfortunate, as object creation
2094
and destruction can be relatively expensive. Class PythonExtension allows creation of both
2095
local and heap-based objects.</p>
2097
<p>If an extension object is created using operator new, as in:</p>
2099
<pre>range* my_r_ref = new range(1, 20, 3)</pre>
2101
<p>then the entity my_r_ref can be thought of as "owning" the reference created
2102
in the new object. Thus, the object will never have a reference count of zero. If the
2103
creator wishes to delete this object, they should either make sure the reference count is
2104
1 and then do delete my_r_ref, or decrement the reference with Py_DECREF(my_r_ref).</p>
2106
<p>Should my_r_ref give up ownership by being used in an Object constructor, all will
2107
still be well. When the Object goes out of scope its destructor will be called, and that
2108
will decrement the reference count, which in turn will trigger the special dealloc routine
2109
that calls the destructor and deletes the pointer.</p>
2111
<p>If the object is created with automatic scope, as in:</p>
2113
<pre>range my_r(1, 20, 3)</pre>
2115
<p>then my_r can be thought of as owning the reference, and when my_r goes out of scope
2116
the object will be destroyed. Of course, care must be taken not to have kept any permanent
2117
reference to this object. Fortunately, in the case of an exception, the C++ exception
2118
facility will call the destructor of my_r. Naturally, care must be taken not to end up
2119
with a dangling reference, but such objects can be created and destroyed more efficiently
2120
than heap-based PyObjects.</p>
2122
<h2>Putting it all together</h2>
2124
<p>The Demo directory of the distribution contains an extensive example of how to use many
2125
of the facilities in PyCXX. It also serves as a test routine. This test is not completely
2126
exhaustive but does excercise much of the facility.</p>
2128
<h2>Acknowledgment</h2>
2130
<p>Thank you to Geoffrey Furnish for patiently teaching me the finer points of C++ and its
2131
template facility, and his critique of PyCXX in particular. With version 4 I welcome Barry
2132
Scott as co-author. -- Paul Dubois</p>