1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Directives — SIP 4.10.5 Reference Guide</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '#',
VERSION: '4.10.5',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="SIP 4.10.5 Reference Guide" href="index.html" />
<link rel="next" title="Annotations" href="annotations.html" />
<link rel="prev" title="SIP Specification Files" href="specification_files.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="annotations.html" title="Annotations"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="specification_files.html" title="SIP Specification Files"
accesskey="P">previous</a> |</li>
<li><a href="index.html">SIP 4.10.5 Reference Guide</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="directives">
<h1>Directives<a class="headerlink" href="#directives" title="Permalink to this headline">¶</a></h1>
<p>In this section we describe each of the directives that can be used in
specification files. All directives begin with <tt class="docutils literal"><span class="pre">%</span></tt> as the first
non-whitespace character in a line.</p>
<p>Some directives have arguments or contain blocks of code or documentation. In
the following descriptions these are shown in <em>italics</em>. Optional arguments
are enclosed in [<em>brackets</em>].</p>
<p>Some directives are used to specify handwritten code. Handwritten code must
not define names that start with the prefix <tt class="docutils literal"><span class="pre">sip</span></tt>.</p>
<dl class="directive">
<dt id="directive-%AccessCode">
<tt class="descname">%AccessCode</tt><a class="headerlink" href="#directive-%AccessCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%AccessCode
<em>code</em>
%End
</pre>
<p>This directive is used immediately after the declaration of an instance of a
wrapped class or structure, or a pointer to such an instance. You use it to
provide handwritten code that overrides the default behaviour.</p>
<p>For example:</p>
<div class="highlight-python"><pre>class Klass;
Klass *klassInstance;
%AccessCode
// In this contrived example the C++ library we are wrapping defines
// klassInstance as Klass ** (which SIP doesn't support) so we
// explicitly dereference it.
if (klassInstance && *klassInstance)
return *klassInstance;
// This will get converted to None.
return 0;
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%API">
<tt class="descname">%API</tt><a class="headerlink" href="#directive-%API" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<p>
<span class="versionmodified">New in version 4.9.</span></p>
<pre class="literal-block">
%API <em>name</em> <em>version</em>
</pre>
<p>This directive is used to define an API and set its default version number. A
version number must be greater than or equal to 1.</p>
<p>See <a class="reference external" href="using.html#ref-incompat-apis"><em>Managing Incompatible APIs</em></a> for more detail.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%API PyTQt4 1</pre>
</div>
<dl class="directive">
<dt id="directive-%BIGetBufferCode">
<tt class="descname">%BIGetBufferCode</tt><a class="headerlink" href="#directive-%BIGetBufferCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%BIGetBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <a class="reference internal" href="#directive-%BIReleaseBufferCode"><tt class="xref docutils literal"><span class="pre">%BIReleaseBufferCode</span></tt></a>) is used to
specify code that implements the buffer interface of Python v3. If Python v2
is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt>Py_buffer *sipBuffer</dt>
<dd>This is a pointer to the Python buffer structure that the handwritten code
must populate.</dd>
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>int sipFlags</dt>
<dd>These are the flags that specify what elements of the <tt class="docutils literal"><span class="pre">sipBuffer</span></tt>
structure must be populated.</dd>
<dt>int sipRes</dt>
<dd>The handwritten code should set this to 0 if there was no error or -1 if
there was an error.</dd>
<dt>PyObject *sipSelf</dt>
<dd>This is the Python object that wraps the structure or class instance, i.e.
<tt class="docutils literal"><span class="pre">self</span></tt>.</dd>
</dl>
<dl class="directive">
<dt id="directive-%BIGetCharBufferCode">
<tt class="descname">%BIGetCharBufferCode</tt><a class="headerlink" href="#directive-%BIGetCharBufferCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%BIGetCharBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <a class="reference internal" href="#directive-%BIGetReadBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetReadBufferCode</span></tt></a>,
<a class="reference internal" href="#directive-%BIGetSegCountCode"><tt class="xref docutils literal"><span class="pre">%BIGetSegCountCode</span></tt></a> and <a class="reference internal" href="#directive-%BIGetWriteBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetWriteBufferCode</span></tt></a>) is used
to specify code that implements the buffer interface of Python v2. If Python
v3 is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>void **sipPtrPtr</dt>
<dd>This is the pointer used to return the address of the character buffer.</dd>
<dt><a title="SIP_SSIZE_T" class="reference external" href="c_api.html#SIP_SSIZE_T"><tt class="xref docutils literal"><span class="pre">SIP_SSIZE_T</span></tt></a> sipRes</dt>
<dd>The handwritten code should set this to the length of the character buffer
or -1 if there was an error.</dd>
<dt><a title="SIP_SSIZE_T" class="reference external" href="c_api.html#SIP_SSIZE_T"><tt class="xref docutils literal"><span class="pre">SIP_SSIZE_T</span></tt></a> sipSegment</dt>
<dd>This is the number of the segment of the character buffer.</dd>
<dt>PyObject *sipSelf</dt>
<dd>This is the Python object that wraps the structure or class instance, i.e.
<tt class="docutils literal"><span class="pre">self</span></tt>.</dd>
</dl>
<dl class="directive">
<dt id="directive-%BIGetReadBufferCode">
<tt class="descname">%BIGetReadBufferCode</tt><a class="headerlink" href="#directive-%BIGetReadBufferCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%BIGetReadBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <a class="reference internal" href="#directive-%BIGetCharBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetCharBufferCode</span></tt></a>,
<a class="reference internal" href="#directive-%BIGetSegCountCode"><tt class="xref docutils literal"><span class="pre">%BIGetSegCountCode</span></tt></a> and <a class="reference internal" href="#directive-%BIGetWriteBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetWriteBufferCode</span></tt></a>) is used
to specify code that implements the buffer interface of Python v2. If
Python v3 is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>void **sipPtrPtr</dt>
<dd>This is the pointer used to return the address of the read buffer.</dd>
<dt><a title="SIP_SSIZE_T" class="reference external" href="c_api.html#SIP_SSIZE_T"><tt class="xref docutils literal"><span class="pre">SIP_SSIZE_T</span></tt></a> sipRes</dt>
<dd>The handwritten code should set this to the length of the read buffer or
-1 if there was an error.</dd>
<dt><a title="SIP_SSIZE_T" class="reference external" href="c_api.html#SIP_SSIZE_T"><tt class="xref docutils literal"><span class="pre">SIP_SSIZE_T</span></tt></a> sipSegment</dt>
<dd>This is the number of the segment of the read buffer.</dd>
<dt>PyObject *sipSelf</dt>
<dd>This is the Python object that wraps the structure or class instance, i.e.
<tt class="docutils literal"><span class="pre">self</span></tt>.</dd>
</dl>
<dl class="directive">
<dt id="directive-%BIGetSegCountCode">
<tt class="descname">%BIGetSegCountCode</tt><a class="headerlink" href="#directive-%BIGetSegCountCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%BIGetSegCountCode
<em>code</em>
%End
</pre>
<p>This directive (along with <a class="reference internal" href="#directive-%BIGetCharBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetCharBufferCode</span></tt></a>,
<a class="reference internal" href="#directive-%BIGetReadBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetReadBufferCode</span></tt></a> and <a class="reference internal" href="#directive-%BIGetWriteBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetWriteBufferCode</span></tt></a>) is
used to specify code that implements the buffer interface of Python v2. If
Python v3 is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt><a title="SIP_SSIZE_T" class="reference external" href="c_api.html#SIP_SSIZE_T"><tt class="xref docutils literal"><span class="pre">SIP_SSIZE_T</span></tt></a> *sipLenPtr</dt>
<dd>This is the pointer used to return the total length in bytes of all
segments of the buffer.</dd>
<dt><a title="SIP_SSIZE_T" class="reference external" href="c_api.html#SIP_SSIZE_T"><tt class="xref docutils literal"><span class="pre">SIP_SSIZE_T</span></tt></a> sipRes</dt>
<dd>The handwritten code should set this to the number of segments that make
up the buffer.</dd>
<dt>PyObject *sipSelf</dt>
<dd>This is the Python object that wraps the structure or class instance, i.e.
<tt class="docutils literal"><span class="pre">self</span></tt>.</dd>
</dl>
<dl class="directive">
<dt id="directive-%BIGetWriteBufferCode">
<tt class="descname">%BIGetWriteBufferCode</tt><a class="headerlink" href="#directive-%BIGetWriteBufferCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%BIGetWriteBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <a class="reference internal" href="#directive-%BIGetCharBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetCharBufferCode</span></tt></a>,
<a class="reference internal" href="#directive-%BIGetReadBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetReadBufferCode</span></tt></a> and <a class="reference internal" href="#directive-%BIGetSegCountCode"><tt class="xref docutils literal"><span class="pre">%BIGetSegCountCode</span></tt></a> is used
to specify code that implements the buffer interface of Python v2. If Python
v3 is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>void **sipPtrPtr</dt>
<dd>This is the pointer used to return the address of the write buffer.</dd>
<dt><a title="SIP_SSIZE_T" class="reference external" href="c_api.html#SIP_SSIZE_T"><tt class="xref docutils literal"><span class="pre">SIP_SSIZE_T</span></tt></a> sipRes</dt>
<dd>The handwritten code should set this to the length of the write buffer or
-1 if there was an error.</dd>
<dt><a title="SIP_SSIZE_T" class="reference external" href="c_api.html#SIP_SSIZE_T"><tt class="xref docutils literal"><span class="pre">SIP_SSIZE_T</span></tt></a> sipSegment</dt>
<dd>This is the number of the segment of the write buffer.</dd>
<dt>PyObject *sipSelf</dt>
<dd>This is the Python object that wraps the structure or class instance, i.e.
<tt class="docutils literal"><span class="pre">self</span></tt>.</dd>
</dl>
<dl class="directive">
<dt id="directive-%BIReleaseBufferCode">
<tt class="descname">%BIReleaseBufferCode</tt><a class="headerlink" href="#directive-%BIReleaseBufferCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%BIReleaseBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <a class="reference internal" href="#directive-%BIGetBufferCode"><tt class="xref docutils literal"><span class="pre">%BIGetBufferCode</span></tt></a>) is used to specify
code that implements the buffer interface of Python v3. If Python v2 is being
used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt>Py_buffer *sipBuffer</dt>
<dd>This is a pointer to the Python buffer structure.</dd>
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>PyObject *sipSelf</dt>
<dd>This is the Python object that wraps the structure or class instance, i.e.
<tt class="docutils literal"><span class="pre">self</span></tt>.</dd>
</dl>
<dl class="directive">
<dt id="directive-%CModule">
<tt class="descname">%CModule</tt><a class="headerlink" href="#directive-%CModule" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%CModule <em>name</em> [<em>version</em>]
</pre>
<p>This directive is used to identify that the library being wrapped is a C
library and to define the name of the module and it’s optional version number.</p>
<p>See the <a class="reference internal" href="#directive-%Module"><tt class="xref docutils literal"><span class="pre">%Module</span></tt></a> directive for an explanation of the version
number.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%CModule dbus 1</pre>
</div>
<dl class="directive">
<dt id="directive-%CompositeModule">
<tt class="descname">%CompositeModule</tt><a class="headerlink" href="#directive-%CompositeModule" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%CompositeModule <em>name</em>
</pre>
<p>A composite module is one that merges a number of related SIP generated
modules. For example, a module that merges the modules <tt class="docutils literal"><span class="pre">a_mod</span></tt>, <tt class="docutils literal"><span class="pre">b_mod</span></tt>
and <tt class="docutils literal"><span class="pre">c_mod</span></tt> is equivalent to the following pure Python module:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">a_mod</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">b_mod</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">c_mod</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>Clearly the individual modules should not define module-level objects with the
same name.</p>
<p>This directive is used to specify the name of a composite module. Any
subsequent <a class="reference internal" href="#directive-%CModule"><tt class="xref docutils literal"><span class="pre">%CModule</span></tt></a> or <a class="reference internal" href="#directive-%Module"><tt class="xref docutils literal"><span class="pre">%Module</span></tt></a> directive is
interpreted as defining a component module.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%CompositeModule PyTQt4.TQt
%Include TQtCore/TQtCoremod.sip
%Include TQtGui/TQtGuimod.sip</pre>
</div>
<p>The main purpose of a composite module is as a programmer convenience as they
don’t have to remember which which individual module an object is defined in.</p>
<dl class="directive">
<dt id="directive-%ConsolidatedModule">
<tt class="descname">%ConsolidatedModule</tt><a class="headerlink" href="#directive-%ConsolidatedModule" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%ConsolidatedModule <em>name</em>
</pre>
<p>A consolidated module is one that consolidates the wrapper code of a number of
SIP generated modules (refered to as component modules in this context).</p>
<p>This directive is used to specify the name of a consolidated module. Any
subsequent <a class="reference internal" href="#directive-%CModule"><tt class="xref docutils literal"><span class="pre">%CModule</span></tt></a> or <a class="reference internal" href="#directive-%Module"><tt class="xref docutils literal"><span class="pre">%Module</span></tt></a> directive is
interpreted as defining a component module.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%ConsolidatedModule PyTQt4._qt
%Include TQtCore/TQtCoremod.sip
%Include TQtGui/TQtGuimod.sip</pre>
</div>
<p>A consolidated module is not intended to be explicitly imported by an
application. Instead it is imported by its component modules when they
themselves are imported.</p>
<p>Normally the wrapper code is contained in the component module and is linked
against the corresponding C or C++ library. The advantage of a consolidated
module is that it allows all of the wrapped C or C++ libraries to be linked
against a single module. If the linking is done statically then deployment of
generated modules can be greatly simplified.</p>
<p>It follows that a component module can be built in one of two ways, as a
normal standalone module, or as a component of a consolidated module. When
building as a component the <tt class="docutils literal"><span class="pre">-p</span></tt> command line option should be used to
specify the name of the consolidated module.</p>
<dl class="directive">
<dt id="directive-%ConvertFromTypeCode">
<tt class="descname">%ConvertFromTypeCode</tt><a class="headerlink" href="#directive-%ConvertFromTypeCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%ConvertFromTypeCode
<em>code</em>
%End
</pre>
<p>This directive is used as part of the <a class="reference internal" href="#directive-%MappedType"><tt class="xref docutils literal"><span class="pre">%MappedType</span></tt></a> directive to
specify the handwritten code that converts an instance of a mapped type to a
Python object.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the instance of the mapped type to be converted. It
will never be zero as the conversion from zero to <tt class="docutils literal"><span class="pre">Py_None</span></tt> is handled
before the handwritten code is called.</dd>
<dt>PyObject *sipTransferObj</dt>
<dd>This specifies any desired ownership changes to the returned object. If it
is <tt class="docutils literal"><span class="pre">NULL</span></tt> then the ownership should be left unchanged. If it is
<tt class="docutils literal"><span class="pre">Py_None</span></tt> then ownership should be transferred to Python. Otherwise
ownership should be transferred to C/C++ and the returned object associated
with <em>sipTransferObj</em>. The code can choose to interpret these changes in
any way. For example, if the code is converting a C++ container of wrapped
classes to a Python list it is likely that the ownership changes should be
made to each element of the list.</dd>
</dl>
<p>The handwritten code must explicitly return a <tt class="docutils literal"><span class="pre">PyObject</span> <span class="pre">*</span></tt>. If there was an
error then a Python exception must be raised and <tt class="docutils literal"><span class="pre">NULL</span></tt> returned.</p>
<p>The following example converts a <tt class="docutils literal"><span class="pre">TQList<TQWidget</span> <span class="pre">*></span></tt> instance to a Python
list of <tt class="docutils literal"><span class="pre">TQWidget</span></tt> instances:</p>
<div class="highlight-python"><pre>%ConvertFromTypeCode
PyObject *l;
// Create the Python list of the correct length.
if ((l = PyList_New(sipCpp->size())) == NULL)
return NULL;
// Go through each element in the C++ instance and convert it to a
// wrapped TQWidget.
for (int i = 0; i < sipCpp->size(); ++i)
{
TQWidget *w = sipCpp->at(i);
PyObject *wobj;
// Get the Python wrapper for the TQWidget instance, creating a new
// one if necessary, and handle any ownership transfer.
if ((wobj = sipConvertFromType(w, sipType_TQWidget, sipTransferObj)) == NULL)
{
// There was an error so garbage collect the Python list.
Py_DECREF(l);
return NULL;
}
// Add the wrapper to the list.
PyList_SET_ITEM(l, i, wobj);
}
// Return the Python list.
return l;
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%ConvertToSubClassCode">
<tt class="descname">%ConvertToSubClassCode</tt><a class="headerlink" href="#directive-%ConvertToSubClassCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%ConvertToSubClassCode
<em>code</em>
%End
</pre>
<p>When SIP needs to wrap a C++ class instance it first checks to make sure it
hasn’t already done so. If it has then it just returns a new reference to the
corresponding Python object. Otherwise it creates a new Python object of the
appropriate type. In C++ a function may be defined to return an instance of a
certain class, but can often return a sub-class instead.</p>
<p>This directive is used to specify handwritten code that exploits any available
real-time type information (RTTI) to see if there is a more specific Python
type that can be used when wrapping the C++ instance. The RTTI may be
provided by the compiler or by the C++ instance itself.</p>
<p>The directive is included in the specification of one of the classes that the
handwritten code handles the type conversion for. It doesn’t matter which
one, but a sensible choice would be the one at the root of that class
hierarchy in the module.</p>
<p>Note that if a class hierarchy extends over a number of modules then this
directive should be used in each of those modules to handle the part of the
hierarchy defined in that module. SIP will ensure that the different pieces
of code are called in the right order to determine the most specific Python
type to use.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the C++ class instance.</dd>
<dt>void **sipCppRet</dt>
<dd>When the sub-class is derived from more than one super-class then it is
possible that the C++ address of the instance as the sub-class is
different to that of the super-class. If so, then this must be set to the
C++ address of the instance when cast (usually using <tt class="docutils literal"><span class="pre">static_cast</span></tt>)
from the super-class to the sub-class.</dd>
<dt>const sipTypeDef *sipType</dt>
<dd>The handwritten code must set this to the SIP generated type structure
that corresponds to the class instance. (The type structure for class
<tt class="docutils literal"><span class="pre">Klass</span></tt> is <tt class="docutils literal"><span class="pre">sipType_Klass</span></tt>.) If the RTTI of the class instance isn’t
recognised then <tt class="docutils literal"><span class="pre">sipType</span></tt> must be set to <tt class="docutils literal"><span class="pre">NULL</span></tt>. The code doesn’t
have to recognise the exact class, only the most specific sub-class that
it can.</dd>
<dt>sipWrapperType *sipClass</dt>
<dd><p class="first">The handwritten code must set this to the SIP generated Python type object
that corresponds to the class instance. (The type object for class
<tt class="docutils literal"><span class="pre">Klass</span></tt> is <tt class="docutils literal"><span class="pre">sipClass_Klass</span></tt>.) If the RTTI of the class instance isn’t
recognised then <tt class="docutils literal"><span class="pre">sipClass</span></tt> must be set to <tt class="docutils literal"><span class="pre">NULL</span></tt>. The code doesn’t
have to recognise the exact class, only the most specific sub-class that
it can.</p>
<p class="last">This is deprecated from SIP v4.8. Instead you should use <tt class="docutils literal"><span class="pre">sipType</span></tt>.</p>
</dd>
</dl>
<p>The handwritten code must not explicitly return.</p>
<p>The following example shows the sub-class conversion code for <tt class="docutils literal"><span class="pre">TQEvent</span></tt> based
class hierarchy in PyTQt:</p>
<div class="highlight-python"><pre>class TQEvent
{
%ConvertToSubClassCode
// TQEvent sub-classes provide a unique type ID.
switch (sipCpp->type())
{
case TQEvent::Timer:
sipType = sipType_TQTimerEvent;
break;
case TQEvent::KeyPress:
case TQEvent::KeyRelease:
sipType = sipType_TQKeyEvent;
break;
// Skip the remaining event types to keep the example short.
default:
// We don't recognise the type.
sipType = NULL;
}
%End
// The rest of the class specification.
};</pre>
</div>
<dl class="directive">
<dt id="directive-%ConvertToTypeCode">
<tt class="descname">%ConvertToTypeCode</tt><a class="headerlink" href="#directive-%ConvertToTypeCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%ConvertToTypeCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify the handwritten code that converts a Python
object to a mapped type instance and to handle any ownership transfers. It is
used as part of the <a class="reference internal" href="#directive-%MappedType"><tt class="xref docutils literal"><span class="pre">%MappedType</span></tt></a> directive and as part of a class
specification. The code is also called to determine if the Python object is of
the correct type prior to conversion.</p>
<p>When used as part of a class specification it can automatically convert
additional types of Python object. For example, PyTQt uses it in the
specification of the <tt class="docutils literal"><span class="pre">TQString</span></tt> class to allow Python string objects and
tqunicode objects to be used wherever <tt class="docutils literal"><span class="pre">TQString</span></tt> instances are expected.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt>int *sipIsErr</dt>
<dd>If this is <tt class="docutils literal"><span class="pre">NULL</span></tt> then the code is being asked to check the type of the
Python object. The check must not have any side effects. Otherwise the
code is being asked to convert the Python object and a non-zero value
should be returned through this pointer if an error occurred during the
conversion.</dd>
<dt>PyObject *sipPy</dt>
<dd>This is the Python object to be converted.</dd>
<dt><em>type</em> **sipCppPtr</dt>
<dd>This is a pointer through which the address of the mapped type instance (or
zero if appropriate) is returned. Its value is undefined if <tt class="docutils literal"><span class="pre">sipIsErr</span></tt>
is <tt class="docutils literal"><span class="pre">NULL</span></tt>.</dd>
<dt>PyObject *sipTransferObj</dt>
<dd>This specifies any desired ownership changes to <em>sipPy</em>. If it is <tt class="docutils literal"><span class="pre">NULL</span></tt>
then the ownership should be left unchanged. If it is <tt class="docutils literal"><span class="pre">Py_None</span></tt> then
ownership should be transferred to Python. Otherwise ownership should be
transferred to C/C++ and <em>sipPy</em> associated with <em>sipTransferObj</em>. The
code can choose to interpret these changes in any way.</dd>
</dl>
<p>The handwritten code must explicitly return an <tt class="docutils literal"><span class="pre">int</span></tt> the meaning of which
depends on the value of <tt class="docutils literal"><span class="pre">sipIsErr</span></tt>.</p>
<p>If <tt class="docutils literal"><span class="pre">sipIsErr</span></tt> is <tt class="docutils literal"><span class="pre">NULL</span></tt> then a non-zero value is returned if the Python
object has a type that can be converted to the mapped type. Otherwise zero is
returned.</p>
<p>If <tt class="docutils literal"><span class="pre">sipIsErr</span></tt> is not <tt class="docutils literal"><span class="pre">NULL</span></tt> then a combination of the following flags is
returned.</p>
<blockquote>
<ul class="simple">
<li><tt class="xref docutils literal"><span class="pre">SIP_TEMPORARY</span></tt> is set to indicate that the returned instance
is a temporary and should be released to avoid a memory leak.</li>
<li><tt class="xref docutils literal"><span class="pre">SIP_DERIVED_CLASS</span></tt> is set to indicate that the type of the
returned instance is a derived class. See
<a class="reference external" href="c_api.html#ref-derived-classes"><em>Generated Derived Classes</em></a>.</li>
</ul>
</blockquote>
<p>The following example converts a Python list of <tt class="docutils literal"><span class="pre">TQPoint</span></tt> instances to a
<tt class="docutils literal"><span class="pre">TQList<TQPoint></span></tt> instance:</p>
<div class="highlight-python"><pre>%ConvertToTypeCode
// See if we are just being asked to check the type of the Python
// object.
if (!sipIsErr)
{
// Checking whether or not None has been passed instead of a list
// has already been done.
if (!PyList_Check(sipPy))
return 0;
// Check the type of each element. We specify SIP_NOT_NONE to
// disallow None because it is a list of TQPoint, not of a pointer
// to a TQPoint, so None isn't appropriate.
for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
if (!sipCanConvertToType(PyList_GET_ITEM(sipPy, i),
sipType_TQPoint, SIP_NOT_NONE))
return 0;
// The type is valid.
return 1;
}
// Create the instance on the heap.
TQList<TQPoint> *ql = new TQList<TQPoint>;
for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
{
TQPoint *qp;
int state;
// Get the address of the element's C++ instance. Note that, in
// this case, we don't apply any ownership changes to the list
// elements, only to the list itself.
qp = reinterpret_cast<TQPoint *>(sipConvertToType(
PyList_GET_ITEM(sipPy, i),
sipType_TQPoint, 0,
SIP_NOT_NONE,
&state, sipIsErr));
// Deal with any errors.
if (*sipIsErr)
{
sipReleaseType(qp, sipType_TQPoint, state);
// Tidy up.
delete ql;
// There is no temporary instance.
return 0;
}
ql->append(*qp);
// A copy of the TQPoint was appended to the list so we no longer
// need it. It may be a temporary instance that should be
// destroyed, or a wrapped instance that should not be destroyed.
// sipReleaseType() will do the right thing.
sipReleaseType(qp, sipType_TQPoint, state);
}
// Return the instance.
*sipCppPtr = ql;
// The instance should be regarded as temporary (and be destroyed as
// soon as it has been used) unless it has been transferred from
// Python. sipGetState() is a convenience function that implements
// this common transfer behaviour.
return sipGetState(sipTransferObj);
%End</pre>
</div>
<p>When used in a class specification the handwritten code replaces the code that
would normally be automatically generated. This means that the handwritten
code must also handle instances of the class itself and not just the additional
types that are being supported. This should be done by making calls to
<a title="sipCanConvertToType" class="reference external" href="c_api.html#sipCanConvertToType"><tt class="xref docutils literal"><span class="pre">sipCanConvertToType()</span></tt></a> to check the object type and
<a title="sipConvertToType" class="reference external" href="c_api.html#sipConvertToType"><tt class="xref docutils literal"><span class="pre">sipConvertToType()</span></tt></a> to convert the object. The
<a title="SIP_NO_CONVERTORS" class="reference external" href="c_api.html#SIP_NO_CONVERTORS"><tt class="xref docutils literal"><span class="pre">SIP_NO_CONVERTORS</span></tt></a> flag <em>must</em> be passed to both these functions to
prevent recursive calls to the handwritten code.</p>
<dl class="directive">
<dt id="directive-%Copying">
<tt class="descname">%Copying</tt><a class="headerlink" href="#directive-%Copying" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Copying
<em>text</em>
%End
</pre>
<p>This directive is used to specify some arbitrary text that will be included at
the start of all source files generated by SIP. It is normally used to
include copyright and licensing terms.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Copying
Copyright (c) 2009 Riverbank Computing Limited
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%DefaultEncoding">
<tt class="descname">%DefaultEncoding</tt><a class="headerlink" href="#directive-%DefaultEncoding" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%DefaultEncoding <em>string</em>
</pre>
<p>This directive is used to specify the default encoding used for <tt class="docutils literal"><span class="pre">char</span></tt>,
<tt class="docutils literal"><span class="pre">const</span> <span class="pre">char</span></tt>, <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> or <tt class="docutils literal"><span class="pre">const</span> <span class="pre">char</span> <span class="pre">*</span></tt> values. The encoding can be
either <tt class="docutils literal"><span class="pre">"ASCII"</span></tt>, <tt class="docutils literal"><span class="pre">"Latin-1"</span></tt>, <tt class="docutils literal"><span class="pre">"UTF-8"</span></tt> or <tt class="docutils literal"><span class="pre">"None"</span></tt>. An encoding of
<tt class="docutils literal"><span class="pre">"None"</span></tt> means that the value is unencoded. The default can be overridden
for a particular value using the <a class="reference external" href="annotations.html#aanno-Encoding"><tt class="xref docutils literal"><span class="pre">Encoding</span></tt></a> annotation. If the
directive is not specified then <tt class="docutils literal"><span class="pre">"None"</span></tt> is used.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%DefaultEncoding "Latin-1"</pre>
</div>
<dl class="directive">
<dt id="directive-%DefaultMetatype">
<tt class="descname">%DefaultMetatype</tt><a class="headerlink" href="#directive-%DefaultMetatype" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%DefaultMetatype <em>dotted-name</em>
</pre>
<p>This directive is used to specify the Python type that should be used as the
meta-type for any C/C++ data type defined in the same module, and by importing
modules, that doesn’t have an explicit meta-type.</p>
<p>If this is not specified then <tt class="docutils literal"><span class="pre">sip.wrappertype</span></tt> is used.</p>
<p>You can also use the <a class="reference external" href="annotations.html#canno-Metatype"><tt class="xref docutils literal"><span class="pre">Metatype</span></tt></a> class annotation to specify the
meta-type used by a particular C/C++ type.</p>
<p>See the section <a class="reference external" href="using.html#ref-types-metatypes"><em>Types and Meta-types</em></a> for more details.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%DefaultMetatype PyTQt4.TQtCore.pyqtWrapperType</pre>
</div>
<dl class="directive">
<dt id="directive-%DefaultSupertype">
<tt class="descname">%DefaultSupertype</tt><a class="headerlink" href="#directive-%DefaultSupertype" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%DefaultSupertype <em>dotted-name</em>
</pre>
<p>This directive is used to specify the Python type that should be used as the
super-type for any C/C++ data type defined in the same module that doesn’t have
an explicit super-type.</p>
<p>If this is not specified then <tt class="docutils literal"><span class="pre">sip.wrapper</span></tt> is used.</p>
<p>You can also use the <a class="reference external" href="annotations.html#canno-Supertype"><tt class="xref docutils literal"><span class="pre">Supertype</span></tt></a> class annotation to specify the
super-type used by a particular C/C++ type.</p>
<p>See the section <a class="reference external" href="using.html#ref-types-metatypes"><em>Types and Meta-types</em></a> for more details.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%DefaultSupertype sip.simplewrapper</pre>
</div>
<dl class="directive">
<dt id="directive-%Doc">
<tt class="descname">%Doc</tt><a class="headerlink" href="#directive-%Doc" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Doc
<em>text</em>
%End
</pre>
<p>This directive is used to specify some arbitrary text that will be extracted
by SIP when the <tt class="docutils literal"><span class="pre">-d</span></tt> command line option is used. The directive can be
specified any number of times and SIP will concatenate all the separate pieces
of text in the order that it sees them.</p>
<p>Documentation that is specified using this directive is local to the module in
which it appears. It is ignored by modules that <a class="reference internal" href="#directive-%Import"><tt class="xref docutils literal"><span class="pre">%Import</span></tt></a> it. Use
the <a class="reference internal" href="#directive-%ExportedDoc"><tt class="xref docutils literal"><span class="pre">%ExportedDoc</span></tt></a> directive for documentation that should be
included by all modules that <a class="reference internal" href="#directive-%Import"><tt class="xref docutils literal"><span class="pre">%Import</span></tt></a> this one.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Doc
<h1>An Example</h1>
<p>
This fragment of documentation is HTML and is local to the module in
which it is defined.
</p>
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%Docstring">
<tt class="descname">%Docstring</tt><a class="headerlink" href="#directive-%Docstring" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Docstring
<em>text</em>
%End
</pre>
<p>
<span class="versionmodified">New in version 4.10.</span></p>
<p>This directive is used to specify explicit docstrings for classes, functions
and methods.</p>
<p>The docstring of a class is made up of the docstring specified for the class
itself, with the docstrings specified for each contructor appended.</p>
<p>The docstring of a function or method is made up of the concatenated docstrings
specified for each of the overloads.</p>
<p>Specifying an explicit docstring will prevent SIP from generating an automatic
docstring that describes the Python signature of a function or method overload.
This means that SIP will generate less informative exceptions (i.e. without a
full signature) when it fails to match a set of arguments to any function or
method overload.</p>
<p>For example:</p>
<div class="highlight-python"><pre>class Klass
{
%Docstring
This will be at the start of the class's docstring.
%End
public:
Klass();
%Docstring
This will be appended to the class's docstring.
%End
};</pre>
</div>
<dl class="directive">
<dt id="directive-%End">
<tt class="descname">%End</tt><a class="headerlink" href="#directive-%End" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<p>This isn’t a directive in itself, but is used to terminate a number of
directives that allow a block of handwritten code or text to be specified.</p>
<dl class="directive">
<dt id="directive-%Exception">
<tt class="descname">%Exception</tt><a class="headerlink" href="#directive-%Exception" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Exception <em>name</em> [(<em>base-exception)]
{
[*header-code</em>]
<em>raise-code</em>
};
</pre>
<p>This directive is used to define new Python exceptions, or to provide a stub
for existing Python exceptions. It allows handwritten code to be provided
that implements the translation between C++ exceptions and Python exceptions.
The arguments to <tt class="docutils literal"><span class="pre">throw</span> <span class="pre">()</span></tt> specifiers must either be names of classes or the
names of Python exceptions defined by this directive.</p>
<p><em>name</em> is the name of the exception.</p>
<p><em>base-exception</em> is the optional base exception. This may be either one of
the standard Python exceptions or one defined with a previous
<a class="reference internal" href="#directive-%Exception"><tt class="xref docutils literal"><span class="pre">%Exception</span></tt></a> directive.</p>
<p><em>header-code</em> is the optional <a class="reference internal" href="#directive-%TypeHeaderCode"><tt class="xref docutils literal"><span class="pre">%TypeHeaderCode</span></tt></a> used to specify any
external interface to the exception being defined.</p>
<p><em>raise-code</em> is the <a class="reference internal" href="#directive-%RaiseCode"><tt class="xref docutils literal"><span class="pre">%RaiseCode</span></tt></a> used to specify the handwritten
code that converts a reference to the C++ exception to the Python exception.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Exception std::exception(SIP_Exception) /PyName=StdException/
{
%TypeHeaderCode
#include <exception>
%End
%RaiseCode
const char *detail = sipExceptionRef.what();
SIP_BLOCK_THREADS
PyErr_SetString(sipException_std_exception, detail);
SIP_UNBLOCK_THREADS
%End
};</pre>
</div>
<p>In this example we map the standard C++ exception to a new Python exception.
The new exception is called <tt class="docutils literal"><span class="pre">StdException</span></tt> and is derived from the standard
Python exception <tt class="docutils literal"><span class="pre">Exception</span></tt>.</p>
<p>An exception may be annotated with <a class="reference external" href="annotations.html#xanno-Default"><tt class="xref docutils literal"><span class="pre">Default</span></tt></a> to specify that it should
be caught by default if there is no <tt class="docutils literal"><span class="pre">throw</span></tt> clause.</p>
<dl class="directive">
<dt id="directive-%ExportedDoc">
<tt class="descname">%ExportedDoc</tt><a class="headerlink" href="#directive-%ExportedDoc" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%ExportedDoc
<em>text</em>
%End
</pre>
<p>This directive is used to specify some arbitrary text that will be extracted
by SIP when the <tt class="docutils literal"><span class="pre">-d</span></tt> command line option is used. The directive can be
specified any number of times and SIP will concatenate all the separate pieces
of text in the order that it sees them.</p>
<p>Documentation that is specified using this directive will also be included by
modules that <a class="reference internal" href="#directive-%Import"><tt class="xref docutils literal"><span class="pre">%Import</span></tt></a> it.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%ExportedDoc
==========
An Example
==========
This fragment of documentation is reStructuredText and will appear in the
module in which it is defined and all modules that %Import it.
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%ExportedHeaderCode">
<tt class="descname">%ExportedHeaderCode</tt><a class="headerlink" href="#directive-%ExportedHeaderCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%ExportedHeaderCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code, typically the declarations
of types, that is placed in a header file that is included by all generated
code for all modules. It should not include function declarations because
Python modules should not explicitly call functions in another Python module.</p>
<p>See also <a class="reference internal" href="#directive-%ModuleCode"><tt class="xref docutils literal"><span class="pre">%ModuleCode</span></tt></a> and <a class="reference internal" href="#directive-%ModuleHeaderCode"><tt class="xref docutils literal"><span class="pre">%ModuleHeaderCode</span></tt></a>.</p>
<dl class="directive">
<dt id="directive-%Feature">
<tt class="descname">%Feature</tt><a class="headerlink" href="#directive-%Feature" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Feature <em>name</em>
</pre>
<p>This directive is used to declare a feature. Features (along with
<a class="reference internal" href="#directive-%Platforms"><tt class="xref docutils literal"><span class="pre">%Platforms</span></tt></a> and <a class="reference internal" href="#directive-%Timeline"><tt class="xref docutils literal"><span class="pre">%Timeline</span></tt></a>) are used by the
<a class="reference internal" href="#directive-%If"><tt class="xref docutils literal"><span class="pre">%If</span></tt></a> directive to control whether or not parts of a specification
are processed or ignored.</p>
<p>Features are mutually independent of each other - any combination of features
may be enabled or disable. By default all features are enabled. The SIP
<tt class="docutils literal"><span class="pre">-x</span></tt> command line option is used to disable a feature.</p>
<p>If a feature is enabled then SIP will automatically generate a corresponding C
preprocessor symbol for use by handwritten code. The symbol is the name of
the feature prefixed by <tt class="docutils literal"><span class="pre">SIP_FEATURE_</span></tt>.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Feature FOO_SUPPORT
%If (FOO_SUPPORT)
void foo();
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%GCClearCode">
<tt class="descname">%GCClearCode</tt><a class="headerlink" href="#directive-%GCClearCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%GCClearCode
<em>code</em>
%End
</pre>
<p>Python has a cyclic garbage collector which can identify and release unneeded
objects even when their reference counts are not zero. If a wrapped C
structure or C++ class keeps its own reference to a Python object then, if the
garbage collector is to do its job, it needs to provide some handwritten code
to traverse and potentially clear those embedded references.</p>
<p>See the section <em>Supporting cyclic garbage collection</em> in <a class="reference external" href="http://www.python.org/dev/doc/devel/ext/">Embedding and
Extending the Python Interpreter</a>
for the details.</p>
<p>This directive is used to specify the code that clears any embedded references.
(See <a class="reference internal" href="#directive-%GCTraverseCode"><tt class="xref docutils literal"><span class="pre">%GCTraverseCode</span></tt></a> for specifying the code that traverses any
embedded references.)</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>int sipRes</dt>
<dd>The handwritten code should set this to the result to be returned.</dd>
</dl>
<p>The following simplified example is taken from PyTQt. The <tt class="docutils literal"><span class="pre">TQCustomEvent</span></tt>
class allows arbitary data to be attached to the event. In PyTQt this data is
always a Python object and so should be handled by the garbage collector:</p>
<div class="highlight-python"><pre>%GCClearCode
PyObject *obj;
// Get the object.
obj = reinterpret_cast<PyObject *>(sipCpp->data());
// Clear the pointer.
sipCpp->setData(0);
// Clear the reference.
Py_XDECREF(obj);
// Report no error.
sipRes = 0;
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%GCTraverseCode">
<tt class="descname">%GCTraverseCode</tt><a class="headerlink" href="#directive-%GCTraverseCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%GCTraverseCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify the code that traverses any embedded
references for Python’s cyclic garbage collector. (See
<a class="reference internal" href="#directive-%GCClearCode"><tt class="xref docutils literal"><span class="pre">%GCClearCode</span></tt></a> for a full explanation.)</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>visitproc sipVisit</dt>
<dd>This is the visit function provided by the garbage collector.</dd>
<dt>void *sipArg</dt>
<dd>This is the argument to the visit function provided by the garbage
collector.</dd>
<dt>int sipRes</dt>
<dd>The handwritten code should set this to the result to be returned.</dd>
</dl>
<p>The following simplified example is taken from PyTQt’s <tt class="docutils literal"><span class="pre">TQCustomEvent</span></tt> class:</p>
<div class="highlight-python"><pre>%GCTraverseCode
PyObject *obj;
// Get the object.
obj = reinterpret_cast<PyObject *>(sipCpp->data());
// Call the visit function if there was an object.
if (obj)
sipRes = sipVisit(obj, sipArg);
else
sipRes = 0;
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%GetCode">
<tt class="descname">%GetCode</tt><a class="headerlink" href="#directive-%GetCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%GetCode
<em>code</em>
%End
</pre>
<p>This directive is used after the declaration of a C++ class variable or C
structure member to specify handwritten code to convert it to a Python object.
It is usually used to handle types that SIP cannot deal with automatically.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class. It is not made available if the
variable being wrapped is a static class variable.</dd>
<dt>PyObject *sipPy</dt>
<dd>The handwritten code must set this to the Python representation of the
class variable or structure member. If there is an error then the code
must raise an exception and set this to <tt class="docutils literal"><span class="pre">NULL</span></tt>.</dd>
<dt>PyObject *sipPyType</dt>
<dd>If the variable being wrapped is a static class variable then this is the
Python type object of the class from which the variable was referenced
(<em>not</em> the class in which it is defined). It may be safely cast to a
PyTypeObject * or a sipWrapperType *.</dd>
</dl>
<p>For example:</p>
<div class="highlight-python"><pre>struct Entity
{
/*
* In this contrived example the C library we are wrapping actually
* defines this as char buffer[100] which SIP cannot handle
* automatically.
*/
char *buffer;
%GetCode
sipPy = PyString_FromStringAndSize(sipCpp->buffer, 100);
%End
%SetCode
char *ptr;
int length;
if (PyString_AsStringAndSize(sipPy, &ptr, &length) == -1)
sipErr = 1;
else if (length != 100)
{
/*
* Raise an exception because the length isn't exactly right.
*/
PyErr_SetString(PyExc_ValueError, "an Entity.buffer must be exactly 100 bytes");
sipErr = 1;
}
else
memcpy(sipCpp->buffer, ptr, 100);
%End
}</pre>
</div>
<dl class="directive">
<dt id="directive-%If">
<tt class="descname">%If</tt><a class="headerlink" href="#directive-%If" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%If (<em>expression</em>)
<em>specification</em>
%End
</pre>
<p>where</p>
<pre class="literal-block">
<em>expression</em> ::= [<em>ored-qualifiers</em> | <em>range</em>]
<em>ored-qualifiers</em> ::= [<em>qualifier</em> | <em>qualifier</em> <strong>||</strong> <em>ored-qualifiers</em>]
<em>qualifier</em> ::= [<strong>!</strong>] [<em>feature</em> | <em>platform</em>]
<em>range</em> ::= [<em>version</em>] <strong>-</strong> [<em>version</em>]
</pre>
<p>This directive is used in conjunction with features (see
<a class="reference internal" href="#directive-%Feature"><tt class="xref docutils literal"><span class="pre">%Feature</span></tt></a>), platforms (see <a class="reference internal" href="#directive-%Platforms"><tt class="xref docutils literal"><span class="pre">%Platforms</span></tt></a>) and versions
(see <a class="reference internal" href="#directive-%Timeline"><tt class="xref docutils literal"><span class="pre">%Timeline</span></tt></a>) to control whether or not parts of a specification
are processed or not.</p>
<p>A <em>range</em> of versions means all versions starting with the lower bound up to
but excluding the upper bound. If the lower bound is omitted then it is
interpreted as being before the earliest version. If the upper bound is
omitted then it is interpreted as being after the latest version.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Feature SUPPORT_FOO
%Platforms {WIN32_PLATFORM POSIX_PLATFORM MACOS_PLATFORM}
%Timeline {V1_0 V1_1 V2_0 V3_0}
%If (!SUPPORT_FOO)
// Process this if the SUPPORT_FOO feature is disabled.
%End
%If (POSIX_PLATFORM || MACOS_PLATFORM)
// Process this if either the POSIX_PLATFORM or MACOS_PLATFORM
// platforms are enabled.
%End
%If (V1_0 - V2_0)
// Process this if either V1_0 or V1_1 is enabled.
%End
%If (V2_0 - )
// Process this if either V2_0 or V3_0 is enabled.
%End
%If ( - )
// Always process this.
%End</pre>
</div>
<p>Note that this directive is not implemented as a preprocessor. Only the
following parts of a specification are affected by it:</p>
<blockquote>
<ul class="simple">
<li><a class="reference internal" href="#directive-%API"><tt class="xref docutils literal"><span class="pre">%API</span></tt></a></li>
<li><tt class="docutils literal"><span class="pre">class</span></tt></li>
<li><a class="reference internal" href="#directive-%ConvertFromTypeCode"><tt class="xref docutils literal"><span class="pre">%ConvertFromTypeCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%ConvertToSubClassCode"><tt class="xref docutils literal"><span class="pre">%ConvertToSubClassCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%ConvertToTypeCode"><tt class="xref docutils literal"><span class="pre">%ConvertToTypeCode</span></tt></a></li>
<li><tt class="docutils literal"><span class="pre">enum</span></tt></li>
<li><a class="reference internal" href="#directive-%DefaultEncoding"><tt class="xref docutils literal"><span class="pre">%DefaultEncoding</span></tt></a></li>
<li><a class="reference internal" href="#directive-%DefaultMetatype"><tt class="xref docutils literal"><span class="pre">%DefaultMetatype</span></tt></a></li>
<li><a class="reference internal" href="#directive-%DefaultSupertype"><tt class="xref docutils literal"><span class="pre">%DefaultSupertype</span></tt></a></li>
<li><a class="reference internal" href="#directive-%ExportedHeaderCode"><tt class="xref docutils literal"><span class="pre">%ExportedHeaderCode</span></tt></a></li>
<li>functions</li>
<li><a class="reference internal" href="#directive-%GCClearCode"><tt class="xref docutils literal"><span class="pre">%GCClearCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%GCTraverseCode"><tt class="xref docutils literal"><span class="pre">%GCTraverseCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%If"><tt class="xref docutils literal"><span class="pre">%If</span></tt></a></li>
<li><a class="reference internal" href="#directive-%InitialisationCode"><tt class="xref docutils literal"><span class="pre">%InitialisationCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%MappedType"><tt class="xref docutils literal"><span class="pre">%MappedType</span></tt></a></li>
<li><a class="reference internal" href="#directive-%MethodCode"><tt class="xref docutils literal"><span class="pre">%MethodCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%ModuleCode"><tt class="xref docutils literal"><span class="pre">%ModuleCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%ModuleHeaderCode"><tt class="xref docutils literal"><span class="pre">%ModuleHeaderCode</span></tt></a></li>
<li><tt class="docutils literal"><span class="pre">namespace</span></tt></li>
<li><a class="reference internal" href="#directive-%PostInitialisationCode"><tt class="xref docutils literal"><span class="pre">%PostInitialisationCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%PreInitialisationCode"><tt class="xref docutils literal"><span class="pre">%PreInitialisationCode</span></tt></a></li>
<li><tt class="docutils literal"><span class="pre">struct</span></tt></li>
<li><tt class="docutils literal"><span class="pre">typedef</span></tt></li>
<li><a class="reference internal" href="#directive-%TypeCode"><tt class="xref docutils literal"><span class="pre">%TypeCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%TypeHeaderCode"><tt class="xref docutils literal"><span class="pre">%TypeHeaderCode</span></tt></a></li>
<li><a class="reference internal" href="#directive-%UnitCode"><tt class="xref docutils literal"><span class="pre">%UnitCode</span></tt></a></li>
<li>variables</li>
<li><a class="reference internal" href="#directive-%VirtualCatcherCode"><tt class="xref docutils literal"><span class="pre">%VirtualCatcherCode</span></tt></a></li>
</ul>
</blockquote>
<p>Also note that the only way to specify the logical and of qualifiers is to use
nested <a class="reference internal" href="#directive-%If"><tt class="xref docutils literal"><span class="pre">%If</span></tt></a> directives.</p>
<dl class="directive">
<dt id="directive-%Import">
<tt class="descname">%Import</tt><a class="headerlink" href="#directive-%Import" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Import <em>filename</em>
</pre>
<p>This directive is used to import the specification of another module. This is
needed if the current module makes use of any types defined in the imported
module, e.g. as an argument to a function, or to sub-class.</p>
<p>If <em>filename</em> cannot be opened then SIP prepends <em>filename</em> with the name of
the directory containing the current specification file (i.e. the one
containing the <a class="reference internal" href="#directive-%Import"><tt class="xref docutils literal"><span class="pre">%Import</span></tt></a> directive) and tries again. If this also
fails then SIP prepends <em>filename</em> with each of the directories, in turn,
specified by the <tt class="docutils literal"><span class="pre">-I</span></tt> command line option.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Import qt/qtmod.sip</pre>
</div>
<dl class="directive">
<dt id="directive-%Include">
<tt class="descname">%Include</tt><a class="headerlink" href="#directive-%Include" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Include <em>filename</em>
</pre>
<p>This directive is used to include contents of another file as part of the
specification of the current module. It is the equivalent of the C
preprocessor’s <tt class="docutils literal"><span class="pre">#include</span></tt> directive and is used to structure a large module
specification into manageable pieces.</p>
<p><a class="reference internal" href="#directive-%Include"><tt class="xref docutils literal"><span class="pre">%Include</span></tt></a> follows the same search process as <a class="reference internal" href="#directive-%Import"><tt class="xref docutils literal"><span class="pre">%Import</span></tt></a>
when trying to open <em>filename</em>.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Include qwidget.sip</pre>
</div>
<dl class="directive">
<dt id="directive-%InitialisationCode">
<tt class="descname">%InitialisationCode</tt><a class="headerlink" href="#directive-%InitialisationCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%InitialisationCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that is embedded in-line
in the generated module initialisation code after the SIP module has been
imported but before the module itself has been initialised.</p>
<p>It is typically used to call <a title="sipRegisterPyType" class="reference external" href="c_api.html#sipRegisterPyType"><tt class="xref docutils literal"><span class="pre">sipRegisterPyType()</span></tt></a>.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%InitialisationCode
// The code will be executed when the module is first imported, after
// the SIP module has been imported, but before other module-specific
// initialisation has been completed.
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%License">
<tt class="descname">%License</tt><a class="headerlink" href="#directive-%License" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%License /<em>license-annotations</em>/
</pre>
<p>This directive is used to specify the contents of an optional license
dictionary. The license dictionary is called <tt class="xref docutils literal"><span class="pre">__license__</span></tt> and is stored
in the module dictionary. The elements of the dictionary are specified using
the <a class="reference external" href="annotations.html#lanno-Licensee"><tt class="xref docutils literal"><span class="pre">Licensee</span></tt></a>, <a class="reference external" href="annotations.html#lanno-Signature"><tt class="xref docutils literal"><span class="pre">Signature</span></tt></a>, <a class="reference external" href="annotations.html#lanno-Timestamp"><tt class="xref docutils literal"><span class="pre">Timestamp</span></tt></a> and <a class="reference external" href="annotations.html#lanno-Type"><tt class="xref docutils literal"><span class="pre">Type</span></tt></a>
annotations. Only the <a class="reference external" href="annotations.html#lanno-Type"><tt class="xref docutils literal"><span class="pre">Type</span></tt></a> annotation is compulsory.</p>
<p>Note that this directive isn’t an attempt to impose any licensing restrictions
on a module. It is simply a method for easily embedding licensing information
in a module so that it is accessible to Python scripts.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%License /Type="GPL"/</pre>
</div>
<dl class="directive">
<dt id="directive-%MappedType">
<tt class="descname">%MappedType</tt><a class="headerlink" href="#directive-%MappedType" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
template<<em>type-list</em>>
%MappedType <em>type</em>
{
[<em>header-code</em>]
[<em>convert-to-code</em>]
[<em>convert-from-code</em>]
};
%MappedType <em>type</em>
{
[<em>header-code</em>]
[<em>convert-to-code</em>]
[<em>convert-from-code</em>]
};
</pre>
<p>This directive is used to define an automatic mapping between a C or C++ type
and a Python type. It can be used as part of a template, or to map a specific
type.</p>
<p>When used as part of a template <em>type</em> cannot itself refer to a template. Any
occurrences of any of the type names (but not any <tt class="docutils literal"><span class="pre">*</span></tt> or <tt class="docutils literal"><span class="pre">&</span></tt>) in
<em>type-list</em> will be replaced by the actual type names used when the template is
instantiated. Template mapped types are instantiated automatically as required
(unlike template classes which are only instantiated using <tt class="docutils literal"><span class="pre">typedef</span></tt>).</p>
<p>Any explicit mapped type will be used in preference to any template that maps
the same type, ie. a template will not be automatically instantiated if there
is an explicit mapped type.</p>
<p><em>header-code</em> is the <a class="reference internal" href="#directive-%TypeHeaderCode"><tt class="xref docutils literal"><span class="pre">%TypeHeaderCode</span></tt></a> used to specify the library
interface to the type being mapped.</p>
<p><em>convert-to-code</em> is the <a class="reference internal" href="#directive-%ConvertToTypeCode"><tt class="xref docutils literal"><span class="pre">%ConvertToTypeCode</span></tt></a> used to specify the
handwritten code that converts a Python object to an instance of the mapped
type.</p>
<p><em>convert-from-code</em> is the <a class="reference internal" href="#directive-%ConvertFromTypeCode"><tt class="xref docutils literal"><span class="pre">%ConvertFromTypeCode</span></tt></a> used to specify
the handwritten code that converts an instance of the mapped type to a Python
object.</p>
<p>For example:</p>
<div class="highlight-python"><pre>template<Type *>
%MappedType TQList
{
%TypeHeaderCode
// Include the library interface to the type being mapped.
#include <qlist.h>
%End
%ConvertToTypeCode
// See if we are just being asked to check the type of the Python
// object.
if (sipIsErr == NULL)
{
// Check it is a list.
if (!PyList_Check(sipPy))
return 0;
// Now check each element of the list is of the type we expect.
// The template is for a pointer type so we don't disallow None.
for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
if (!sipCanConvertToType(PyList_GET_ITEM(sipPy, i),
sipType_Type, 0))
return 0;
return 1;
}
// Create the instance on the heap.
TQList<Type *> *ql = new TQList<Type *>;
for (int i = 0; i < PyList_GET_SIZE(sipPy); ++i)
{
// Use the SIP API to convert the Python object to the
// corresponding C++ instance. Note that we apply any ownership
// transfer to the list itself, not the individual elements.
Type *t = reinterpret_cast<Type *>(sipConvertToType(
PyList_GET_ITEM(sipPy, i),
sipType_Type, 0, 0, 0,
sipIsErr));
if (*sipIsErr)
{
// Tidy up.
delete ql;
// There is nothing on the heap.
return 0;
}
// Add the pointer to the C++ instance.
ql->append(t);
}
// Return the instance on the heap.
*sipCppPtr = ql;
// Apply the normal transfer.
return sipGetState(sipTransferObj);
%End
%ConvertFromTypeCode
PyObject *l;
// Create the Python list of the correct length.
if ((l = PyList_New(sipCpp->size())) == NULL)
return NULL;
// Go through each element in the C++ instance and convert it to the
// corresponding Python object.
for (int i = 0; i < sipCpp->size(); ++i)
{
Type *t = sipCpp->at(i);
PyObject *tobj;
if ((tobj = sipConvertFromType(t, sipType_Type, sipTransferObj)) == NULL)
{
// There was an error so garbage collect the Python list.
Py_DECREF(l);
return NULL;
}
PyList_SET_ITEM(l, i, tobj);
}
// Return the Python list.
return l;
%End
}</pre>
</div>
<p>Using this we can use, for example, <tt class="docutils literal"><span class="pre">TQList<TQObject</span> <span class="pre">*></span></tt> throughout the
module’s specification files (and in any module that imports this one). The
generated code will automatically map this to and from a Python list of TQObject
instances when appropriate.</p>
<dl class="directive">
<dt id="directive-%MethodCode">
<tt class="descname">%MethodCode</tt><a class="headerlink" href="#directive-%MethodCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%MethodCode
<em>code</em>
%End
</pre>
<p>This directive is used as part of the specification of a global function, class
method, operator, constructor or destructor to specify handwritten code that
replaces the normally generated call to the function being wrapped. It is
usually used to handle argument types and results that SIP cannot deal with
automatically.</p>
<p>Normally the specified code is embedded in-line after the function’s arguments
have been successfully converted from Python objects to their C or C++
equivalents. In this case the specified code must not include any <tt class="docutils literal"><span class="pre">return</span></tt>
statements.</p>
<p>However if the <a class="reference external" href="annotations.html#fanno-NoArgParser"><tt class="xref docutils literal"><span class="pre">NoArgParser</span></tt></a> annotation has been used then the specified
code is also responsible for parsing the arguments. No other code is generated
by SIP and the specified code must include a <tt class="docutils literal"><span class="pre">return</span></tt> statement.</p>
<p>In the context of a destructor the specified code is embedded in-line in the
Python type’s deallocation function. Unlike other contexts it supplements
rather than replaces the normally generated code, so it must not include code
to return the C structure or C++ class instance to the heap. The code is only
called if ownership of the structure or class is with Python.</p>
<p>The specified code must also handle the Python Global Interpreter Lock (GIL).
If compatibility with SIP v3.x is required then the GIL must be released
immediately before the C++ call and reacquired immediately afterwards as shown
in this example fragment:</p>
<div class="highlight-python"><pre>Py_BEGIN_ALLOW_THREADS
sipCpp->foo();
Py_END_ALLOW_THREADS</pre>
</div>
<p>If compatibility with SIP v3.x is not required then this is optional but
should be done if the C++ function might block the current thread or take a
significant amount of time to execute. (See <a class="reference external" href="using.html#ref-gil"><em>The Python Global Interpreter Lock</em></a> and the
<a class="reference external" href="annotations.html#fanno-ReleaseGIL"><tt class="xref docutils literal"><span class="pre">ReleaseGIL</span></tt></a> and <a class="reference external" href="annotations.html#fanno-HoldGIL"><tt class="xref docutils literal"><span class="pre">HoldGIL</span></tt></a> annotations.)</p>
<p>If the <a class="reference external" href="annotations.html#fanno-NoArgParser"><tt class="xref docutils literal"><span class="pre">NoArgParser</span></tt></a> annotation has not been used then the following
variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> a0</dt>
<dd><p class="first">There is a variable for each argument of the Python signature (excluding
any <tt class="docutils literal"><span class="pre">self</span></tt> argument) named <tt class="docutils literal"><span class="pre">a0</span></tt>, <tt class="docutils literal"><span class="pre">a1</span></tt>, etc. The <em>type</em> of the
variable is the same as the type defined in the specification with the
following exceptions:</p>
<ul class="simple">
<li>if the argument is only used to return a value (e.g. it is an <tt class="docutils literal"><span class="pre">int</span> <span class="pre">*</span></tt>
without an <a class="reference external" href="annotations.html#aanno-In"><tt class="xref docutils literal"><span class="pre">In</span></tt></a> annotation) then the type has one less level of
indirection (e.g. it will be an <tt class="docutils literal"><span class="pre">int</span></tt>)</li>
<li>if the argument is a structure or class (or a reference or a pointer to a
structure or class) then <em>type</em> will always be a pointer to the structure
or class.</li>
</ul>
<p class="last">Note that handwritten code for destructors never has any arguments.</p>
</dd>
<dt>PyObject *a0Wrapper</dt>
<dd>This variable is made available only if the <a class="reference external" href="annotations.html#aanno-GetWrapper"><tt class="xref docutils literal"><span class="pre">GetWrapper</span></tt></a> annotation
is specified for the corresponding argument. The variable is a pointer to
the Python object that wraps the argument.</dd>
<dt><em>type</em> *sipCpp</dt>
<dd><p class="first">If the directive is used in the context of a class constructor then this
must be set by the handwritten code to the constructed instance. If it is
set to <tt class="docutils literal"><span class="pre">0</span></tt> and no Python exception is raised then SIP will continue to
try other Python signatures.</p>
<p>If the directive is used in the context of a method (but not the standard
binary operator methods, e.g. <tt class="xref docutils literal"><span class="pre">__add__()</span></tt>) or a destructor then this is
a pointer to the C structure or C++ class instance.</p>
<p>Its <em>type</em> is a pointer to the structure or class.</p>
<p class="last">Standard binary operator methods follow the same convention as global
functions and instead define two arguments called <tt class="docutils literal"><span class="pre">a0</span></tt> and <tt class="docutils literal"><span class="pre">a1</span></tt>.</p>
</dd>
<dt>sipErrorState sipError</dt>
<dd><p class="first">The handwritten code should set this to either <tt class="docutils literal"><span class="pre">sipErrorContinue</span></tt> or
<tt class="docutils literal"><span class="pre">sipErrorFail</span></tt>, and raise an appropriate Python exception, if an error
is detected. Its initial value will be <tt class="docutils literal"><span class="pre">sipErrorNone</span></tt>.</p>
<p>When <tt class="docutils literal"><span class="pre">sipErrorContinue</span></tt> is used, SIP will remember the exception as the
reason why the particular overloaded callable could not be invoked. It
will then continue to try the next overloaded callable. It is typically
used by code that needs to do additional type checking of the callable’s
arguments.</p>
<p>When <tt class="docutils literal"><span class="pre">sipErrorFail1</span></tt> is used, SIP will report the exception immediately
and will not attempt to invoke other overloaded callables.</p>
<p class="last"><tt class="docutils literal"><span class="pre">sipError</span></tt> is not provided for destructors.</p>
</dd>
<dt>int sipIsErr</dt>
<dd><p class="first">The handwritten code should set this to a non-zero value, and raise an
appropriate Python exception, if an error is detected. This is the
equivalent of setting <tt class="docutils literal"><span class="pre">sipError</span></tt> to <tt class="docutils literal"><span class="pre">sipErrorFail</span></tt>. Its initial value
will be <tt class="docutils literal"><span class="pre">0</span></tt>.</p>
<p class="last"><tt class="docutils literal"><span class="pre">sipIsErr</span></tt> is not provided for destructors.</p>
</dd>
<dt><em>type</em> sipRes</dt>
<dd><p class="first">The handwritten code should set this to the result to be returned. The
<em>type</em> of the variable is the same as the type defined in the Python
signature in the specification with the following exception:</p>
<ul class="simple">
<li>if the argument is a structure or class (or a reference or a pointer to a
structure or class) then <em>type</em> will always be a pointer to the structure
or class.</li>
</ul>
<p class="last"><tt class="docutils literal"><span class="pre">sipRes</span></tt> is not provided for inplace operators (e.g. <tt class="docutils literal"><span class="pre">+=</span></tt> or
<tt class="xref docutils literal"><span class="pre">__imul__()</span></tt>) as their results are handled automatically, nor for class
constructors or destructors.</p>
</dd>
<dt>PyObject *sipSelf</dt>
<dd>If the directive is used in the context of a class constructor, destructor
or method then this is the Python object that wraps the structure or class
instance, i.e. <tt class="docutils literal"><span class="pre">self</span></tt>.</dd>
<dt>bool sipSelfWasArg</dt>
<dd><p class="first">This is only made available for non-abstract, virtual methods. It is set
if <tt class="docutils literal"><span class="pre">self</span></tt> was explicitly passed as the first argument of the method
rather than being bound to the method. In other words, the call was:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Klass</span><span class="o">.</span><span class="n">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>rather than:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="bp">self</span><span class="o">.</span><span class="n">foo</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
</dd>
</dl>
<p>If the <a class="reference external" href="annotations.html#fanno-NoArgParser"><tt class="xref docutils literal"><span class="pre">NoArgParser</span></tt></a> annotation has been used then only the following
variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt>PyObject *sipArgs</dt>
<dd>This is the tuple of arguments.</dd>
<dt>PyObject *sipKwds</dt>
<dd>This is the dictionary of keyword arguments.</dd>
</dl>
<p>The following is a complete example:</p>
<div class="highlight-python"><pre>class Klass
{
public:
virtual int foo(SIP_PYTUPLE);
%MethodCode
// The C++ API takes a 2 element array of integers but passing a
// two element tuple is more Pythonic.
int iarr[2];
if (PyArg_ParseTuple(a0, "ii", &iarr[0], &iarr[1]))
{
Py_BEGIN_ALLOW_THREADS
sipRes = sipSelfWasArg ? sipCpp->Klass::foo(iarr)
: sipCpp->foo(iarr);
Py_END_ALLOW_THREADS
}
else
{
// PyArg_ParseTuple() will have raised the exception.
sipIsErr = 1;
}
%End
};</pre>
</div>
<p>As the example is a virtual method <a class="footnote-reference" href="#id2" id="id1">[1]</a>, note the use of <tt class="docutils literal"><span class="pre">sipSelfWasArg</span></tt> to
determine exactly which implementation of <tt class="docutils literal"><span class="pre">foo()</span></tt> to call.</p>
<p>If a method is in the <tt class="docutils literal"><span class="pre">protected</span></tt> section of a C++ class then SIP generates
helpers that provide access to method. However, these are not available if
the Python module is being built with <tt class="docutils literal"><span class="pre">protected</span></tt> redefined as <tt class="docutils literal"><span class="pre">public</span></tt>.</p>
<p>The following pattern should be used to cover all possibilities:</p>
<div class="highlight-python"><pre>#if defined(SIP_PROTECTED_IS_PUBLIC)
sipRes = sipSelfWasArg ? sipCpp->Klass::foo(iarr)
: sipCpp->foo(iarr);
#else
sipRes = sipCpp->sipProtectVirt_foo(sipSelfWasArg, iarr);
#endif</pre>
</div>
<p>If a method is in the <tt class="docutils literal"><span class="pre">protected</span></tt> section of a C++ class but is not virtual
then the pattern should instead be:</p>
<div class="highlight-python"><pre>#if defined(SIP_PROTECTED_IS_PUBLIC)
sipRes = sipCpp->foo(iarr);
#else
sipRes = sipCpp->sipProtect_foo(iarr);
#endif</pre>
</div>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>See <a class="reference internal" href="#directive-%VirtualCatcherCode"><tt class="xref docutils literal"><span class="pre">%VirtualCatcherCode</span></tt></a> for a description of how SIP
generated code handles the reimplementation of C++ virtual methods in
Python.</td></tr>
</tbody>
</table>
<dl class="directive">
<dt id="directive-%Module">
<tt class="descname">%Module</tt><a class="headerlink" href="#directive-%Module" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Module <em>name</em> [<em>version</em>]
</pre>
<p>This directive is used to identify that the library being wrapped is a C++
library and to define the name of the module and it’s optional version number.</p>
<p>The name may contain periods to specify that the module is part of a Python
package.</p>
<p>The optional version number is useful if you (or others) might create other
modules that build on this module, i.e. if another module might
<a class="reference internal" href="#directive-%Import"><tt class="xref docutils literal"><span class="pre">%Import</span></tt></a> this module. Under the covers, a module exports an API
that is used by modules that <a class="reference internal" href="#directive-%Import"><tt class="xref docutils literal"><span class="pre">%Import</span></tt></a> it and the API is given a
version number. A module built on that module knows the version number of the
API that it is expecting. If, when the modules are imported at run-time, the
version numbers do not match then a Python exception is raised. The dependent
module must then be re-built using the correct specification files for the base
module.</p>
<p>The version number should be incremented whenever a module is changed. Some
changes don’t affect the exported API, but it is good practice to change the
version number anyway.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Module qt 5</pre>
</div>
<dl class="directive">
<dt id="directive-%ModuleCode">
<tt class="descname">%ModuleCode</tt><a class="headerlink" href="#directive-%ModuleCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%ModuleCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code, typically the
implementations of utility functions, that can be called by other handwritten
code in the module.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%ModuleCode
// Print an object on stderr for debugging purposes.
void dump_object(PyObject *o)
{
PyObject_Print(o, stderr, 0);
fprintf(stderr, "\n");
}
%End</pre>
</div>
<p>See also <a class="reference internal" href="#directive-%ExportedHeaderCode"><tt class="xref docutils literal"><span class="pre">%ExportedHeaderCode</span></tt></a> and <a class="reference internal" href="#directive-%ModuleHeaderCode"><tt class="xref docutils literal"><span class="pre">%ModuleHeaderCode</span></tt></a>.</p>
<dl class="directive">
<dt id="directive-%ModuleHeaderCode">
<tt class="descname">%ModuleHeaderCode</tt><a class="headerlink" href="#directive-%ModuleHeaderCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%ModuleHeaderCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code, typically the declarations
of utility functions, that is placed in a header file that is included by all
generated code for the same module.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%ModuleHeaderCode
void dump_object(PyObject *o);
%End</pre>
</div>
<p>See also <a class="reference internal" href="#directive-%ExportedHeaderCode"><tt class="xref docutils literal"><span class="pre">%ExportedHeaderCode</span></tt></a> and <a class="reference internal" href="#directive-%ModuleCode"><tt class="xref docutils literal"><span class="pre">%ModuleCode</span></tt></a>.</p>
<dl class="directive">
<dt id="directive-%OptionalInclude">
<tt class="descname">%OptionalInclude</tt><a class="headerlink" href="#directive-%OptionalInclude" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%OptionalInclude <em>filename</em>
</pre>
<p>This directive is identical to the <a class="reference internal" href="#directive-%Include"><tt class="xref docutils literal"><span class="pre">%Include</span></tt></a> directive except that
SIP silently continues processing if <em>filename</em> could not be opened.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%OptionalInclude license.sip</pre>
</div>
<dl class="directive">
<dt id="directive-%PickleCode">
<tt class="descname">%PickleCode</tt><a class="headerlink" href="#directive-%PickleCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%PickleCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code to pickle a C structure or
C++ class instance.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>PyObject *sipRes</dt>
<dd>The handwritten code must set this to a tuple of the arguments that will
be passed to the type’s __init__() method when the structure or class
instance is unpickled. If there is an error then the code must raise an
exception and set this to <tt class="docutils literal"><span class="pre">NULL</span></tt>.</dd>
</dl>
<p>For example:</p>
<div class="highlight-python"><pre>class Point
{
Point(int x, y);
int x() const;
int y() const;
%PickleCode
sipRes = Py_BuildValue("ii", sipCpp->x(), sipCpp->y());
%End
}</pre>
</div>
<p>Note that SIP works around the Python limitation that prevents nested types
being pickled.</p>
<p>Both named and unnamed enums can be pickled automatically without providing any
handwritten code.</p>
<dl class="directive">
<dt id="directive-%Platforms">
<tt class="descname">%Platforms</tt><a class="headerlink" href="#directive-%Platforms" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Platforms {<em>name</em> <em>name</em> ...}
</pre>
<p>This directive is used to declare a set of platforms. Platforms (along with
<a class="reference internal" href="#directive-%Feature"><tt class="xref docutils literal"><span class="pre">%Feature</span></tt></a> and <a class="reference internal" href="#directive-%Timeline"><tt class="xref docutils literal"><span class="pre">%Timeline</span></tt></a>) are used by the
<a class="reference internal" href="#directive-%If"><tt class="xref docutils literal"><span class="pre">%If</span></tt></a> directive to control whether or not parts of a specification
are processed or ignored.</p>
<p>Platforms are mutually exclusive - only one platform can be enabled at a time.
By default all platforms are disabled. The SIP <tt class="docutils literal"><span class="pre">-t</span></tt> command line option is
used to enable a platform.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Platforms {WIN32_PLATFORM POSIX_PLATFORM MACOS_PLATFORM}
%If (WIN32_PLATFORM)
void undocumented();
%End
%If (POSIX_PLATFORM)
void documented();
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%PostInitialisationCode">
<tt class="descname">%PostInitialisationCode</tt><a class="headerlink" href="#directive-%PostInitialisationCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%PostInitialisationCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that is embedded in-line
at the very end of the generated module initialisation code.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt>PyObject *sipModule</dt>
<dd>This is the module object returned by <tt class="docutils literal"><span class="pre">Py_InitModule()</span></tt>.</dd>
<dt>PyObject *sipModuleDict</dt>
<dd>This is the module’s dictionary object returned by <tt class="docutils literal"><span class="pre">Py_ModuleGetDict()</span></tt>.</dd>
</dl>
<p>For example:</p>
<div class="highlight-python"><pre>%PostInitialisationCode
// The code will be executed when the module is first imported and
// after all other initialisation has been completed.
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%PreInitialisationCode">
<tt class="descname">%PreInitialisationCode</tt><a class="headerlink" href="#directive-%PreInitialisationCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%PreInitialisationCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that is embedded in-line
at the very start of the generated module initialisation code.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%PreInitialisationCode
// The code will be executed when the module is first imported and
// before other initialisation has been completed.
%End</pre>
</div>
<dl class="directive">
<dt id="directive-%RaiseCode">
<tt class="descname">%RaiseCode</tt><a class="headerlink" href="#directive-%RaiseCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%RaiseCode
<em>code</em>
%End
</pre>
<p>This directive is used as part of the definition of an exception using the
<a class="reference internal" href="#directive-%Exception"><tt class="xref docutils literal"><span class="pre">%Exception</span></tt></a> directive to specify handwritten code that raises a
Python exception when a C++ exception has been caught. The code is embedded
in-line as the body of a C++ <tt class="docutils literal"><span class="pre">catch</span> <span class="pre">()</span></tt> clause.</p>
<p>The specified code must handle the Python Global Interpreter Lock (GIL) if
necessary. The GIL must be acquired before any calls to the Python API and
released after the last call as shown in this example fragment:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">SIP_BLOCK_THREADS</span>
<span class="n">PyErr_SetNone</span><span class="p">(</span><span class="n">PyErr_Exception</span><span class="p">);</span>
<span class="n">SIP_UNBLOCK_THREADS</span>
</pre></div>
</div>
<p>Finally, the specified code must not include any <tt class="docutils literal"><span class="pre">return</span></tt> statements.</p>
<p>The following variable is made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> &sipExceptionRef</dt>
<dd>This is a reference to the caught C++ exception. The <em>type</em> of the
reference is the same as the type defined in the <tt class="docutils literal"><span class="pre">throw</span> <span class="pre">()</span></tt> specifier.</dd>
</dl>
<p>See the <a class="reference internal" href="#directive-%Exception"><tt class="xref docutils literal"><span class="pre">%Exception</span></tt></a> directive for an example.</p>
<dl class="directive">
<dt id="directive-%SetCode">
<tt class="descname">%SetCode</tt><a class="headerlink" href="#directive-%SetCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%SetCode
<em>code</em>
%End
</pre>
<p>This directive is used after the declaration of a C++ class variable or C
structure member to specify handwritten code to convert it from a Python
object. It is usually used to handle types that SIP cannot deal with
automatically.</p>
<p>The following variables are made available to the handwritten code:</p>
<dl class="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class. It is not made available if the
variable being wrapped is a static class variable.</dd>
<dt>int sipErr</dt>
<dd>If the conversion failed then the handwritten code should raise a Python
exception and set this to a non-zero value. Its initial value will be
automatically set to zero.</dd>
<dt>PyObject *sipPy</dt>
<dd>This is the Python object that the handwritten code should convert.</dd>
<dt>PyObject *sipPyType</dt>
<dd>If the variable being wrapped is a static class variable then this is the
Python type object of the class from which the variable was referenced
(<em>not</em> the class in which it is defined). It may be safely cast to a
PyTypeObject * or a sipWrapperType *.</dd>
</dl>
<p>See the <a class="reference internal" href="#directive-%GetCode"><tt class="xref docutils literal"><span class="pre">%GetCode</span></tt></a> directive for an example.</p>
<dl class="directive">
<dt id="directive-%Timeline">
<tt class="descname">%Timeline</tt><a class="headerlink" href="#directive-%Timeline" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%Timeline {<em>name</em> <em>name</em> ...}
</pre>
<p>This directive is used to declare a set of versions released over a period of
time. Versions (along with <a class="reference internal" href="#directive-%Feature"><tt class="xref docutils literal"><span class="pre">%Feature</span></tt></a> and <a class="reference internal" href="#directive-%Platforms"><tt class="xref docutils literal"><span class="pre">%Platforms</span></tt></a>)
are used by the <a class="reference internal" href="#directive-%If"><tt class="xref docutils literal"><span class="pre">%If</span></tt></a> directive to control whether or not parts of a
specification are processed or ignored.</p>
<p>Versions are mutually exclusive - only one version can be enabled at a time.
By default all versions are disabled. The SIP <tt class="docutils literal"><span class="pre">-t</span></tt> command line option is
used to enable a version.</p>
<p>For example:</p>
<div class="highlight-python"><pre>%Timeline {V1_0 V1_1 V2_0 V3_0}
%If (V1_0 - V2_0)
void foo();
%End
%If (V2_0 -)
void foo(int = 0);
%End</pre>
</div>
<p><a class="reference internal" href="#directive-%Timeline"><tt class="xref docutils literal"><span class="pre">%Timeline</span></tt></a> can be used any number of times in a module to allow
multiple libraries to be wrapped in the same module.</p>
<dl class="directive">
<dt id="directive-%TypeCode">
<tt class="descname">%TypeCode</tt><a class="headerlink" href="#directive-%TypeCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%TypeCode
<em>code</em>
%End
</pre>
<p>This directive is used as part of the specification of a C structure or a C++
class to specify handwritten code, typically the implementations of utility
functions, that can be called by other handwritten code in the structure or
class.</p>
<p>For example:</p>
<div class="highlight-python"><pre>class Klass
{
%TypeCode
// Print an instance on stderr for debugging purposes.
static void dump_klass(const Klass *k)
{
fprintf(stderr,"Klass %s at %p\n", k->name(), k);
}
%End
// The rest of the class specification.
};</pre>
</div>
<p>Because the scope of the code is normally within the generated file that
implements the type, any utility functions would normally be declared
<tt class="docutils literal"><span class="pre">static</span></tt>. However a naming convention should still be adopted to prevent
clashes of function names within a module in case the SIP <tt class="docutils literal"><span class="pre">-j</span></tt> command line
option is used.</p>
<dl class="directive">
<dt id="directive-%TypeHeaderCode">
<tt class="descname">%TypeHeaderCode</tt><a class="headerlink" href="#directive-%TypeHeaderCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%TypeHeaderCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that defines the interface
to a C or C++ type being wrapped, either a structure, a class, or a template.
It is used within a class definition or a <a class="reference internal" href="#directive-%MappedType"><tt class="xref docutils literal"><span class="pre">%MappedType</span></tt></a> directive.</p>
<p>Normally <em>code</em> will be a pre-processor <tt class="docutils literal"><span class="pre">#include</span></tt> statement.</p>
<p>For example:</p>
<div class="highlight-python"><pre>// Wrap the Klass class.
class Klass
{
%TypeHeaderCode
#include <klass.h>
%End
// The rest of the class specification.
};</pre>
</div>
<dl class="directive">
<dt id="directive-%UnitCode">
<tt class="descname">%UnitCode</tt><a class="headerlink" href="#directive-%UnitCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%UnitCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that it included at the very
start of a generated compilation unit (ie. C or C++ source file). It is
typically used to <tt class="docutils literal"><span class="pre">#include</span></tt> a C++ precompiled header file.</p>
<dl class="directive">
<dt id="directive-%VirtualCatcherCode">
<tt class="descname">%VirtualCatcherCode</tt><a class="headerlink" href="#directive-%VirtualCatcherCode" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<pre class="literal-block">
%VirtualCatcherCode
<em>code</em>
%End
</pre>
<p>For most classes there are corresponding <a class="reference external" href="c_api.html#ref-derived-classes"><em>generated derived classes</em></a> that contain reimplementations of the class’s virtual
methods. These methods (which SIP calls catchers) determine if there is a
corresponding Python reimplementation and call it if so. If there is no Python
reimplementation then the method in the original class is called instead.</p>
<p>This directive is used to specify handwritten code that replaces the normally
generated call to the Python reimplementation and the handling of any returned
results. It is usually used to handle argument types and results that SIP
cannot deal with automatically.</p>
<p>This directive can also be used in the context of a class destructor to
specify handwritten code that is embedded in-line in the internal derived
class’s destructor.</p>
<p>In the context of a method the Python Global Interpreter Lock (GIL) is
automatically acquired before the specified code is executed and automatically
released afterwards.</p>
<p>In the context of a destructor the specified code must handle the GIL. The
GIL must be acquired before any calls to the Python API and released after the
last call as shown in this example fragment:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">SIP_BLOCK_THREADS</span>
<span class="n">Py_DECREF</span><span class="p">(</span><span class="n">obj</span><span class="p">);</span>
<span class="n">SIP_UNBLOCK_THREADS</span>
</pre></div>
</div>
<p>The following variables are made available to the handwritten code in the
context of a method:</p>
<dl class="docutils">
<dt><em>type</em> a0</dt>
<dd>There is a variable for each argument of the C++ signature named <tt class="docutils literal"><span class="pre">a0</span></tt>,
<tt class="docutils literal"><span class="pre">a1</span></tt>, etc. The <em>type</em> of the variable is the same as the type defined in
the specification.</dd>
<dt>int a0Key</dt>
<dd>There is a variable for each argument of the C++ signature that has a type
where it is important to ensure that the corresponding Python object is not
garbage collected too soon. This only applies to output arguments that
return <tt class="docutils literal"><span class="pre">'\0'</span></tt> terminated strings. The variable would normally be passed
to <a title="sipParseResult" class="reference external" href="c_api.html#sipParseResult"><tt class="xref docutils literal"><span class="pre">sipParseResult()</span></tt></a> using either the <tt class="docutils literal"><span class="pre">A</span></tt> or <tt class="docutils literal"><span class="pre">B</span></tt> format
characters.</dd>
<dt>int sipIsErr</dt>
<dd>The handwritten code should set this to a non-zero value, and raise an
appropriate Python exception, if an error is detected.</dd>
<dt>PyObject *sipMethod</dt>
<dd>This object is the Python reimplementation of the virtual C++ method. It
is normally passed to <a title="sipCallMethod" class="reference external" href="c_api.html#sipCallMethod"><tt class="xref docutils literal"><span class="pre">sipCallMethod()</span></tt></a>.</dd>
<dt><em>type</em> sipRes</dt>
<dd>The handwritten code should set this to the result to be returned. The
<em>type</em> of the variable is the same as the type defined in the C++ signature
in the specification.</dd>
<dt>int sipResKey</dt>
<dd>This variable is only made available if the result has a type where it is
important to ensure that the corresponding Python object is not garbage
collected too soon. This only applies to <tt class="docutils literal"><span class="pre">'\0'</span></tt> terminated strings. The
variable would normally be passed to <a title="sipParseResult" class="reference external" href="c_api.html#sipParseResult"><tt class="xref docutils literal"><span class="pre">sipParseResult()</span></tt></a> using either
the <tt class="docutils literal"><span class="pre">A</span></tt> or <tt class="docutils literal"><span class="pre">B</span></tt> format characters.</dd>
<dt>sipSimpleWrapper *sipPySelf</dt>
<dd>This variable is only made available if either the <tt class="docutils literal"><span class="pre">a0Key</span></tt> or
<tt class="docutils literal"><span class="pre">sipResKey</span></tt> are made available. It defines the context within which keys
are unique. The variable would normally be passed to
<a title="sipParseResult" class="reference external" href="c_api.html#sipParseResult"><tt class="xref docutils literal"><span class="pre">sipParseResult()</span></tt></a> using the <tt class="docutils literal"><span class="pre">S</span></tt> format character.</dd>
</dl>
<p>No variables are made available in the context of a destructor.</p>
<p>For example:</p>
<div class="highlight-python"><pre>class Klass
{
public:
virtual int foo(SIP_PYTUPLE) [int (int *)];
%MethodCode
// The C++ API takes a 2 element array of integers but passing a
// two element tuple is more Pythonic.
int iarr[2];
if (PyArg_ParseTuple(a0, "ii", &iarr[0], &iarr[1]))
{
Py_BEGIN_ALLOW_THREADS
sipRes = sipCpp->Klass::foo(iarr);
Py_END_ALLOW_THREADS
}
else
{
// PyArg_ParseTuple() will have raised the exception.
sipIsErr = 1;
}
%End
%VirtualCatcherCode
// Convert the 2 element array of integers to the two element
// tuple.
PyObject *result;
result = sipCallMethod(&sipIsErr, sipMethod, "ii", a0[0], a0[1]);
if (result != NULL)
{
// Convert the result to the C++ type.
sipParseResult(&sipIsErr, sipMethod, result, "i", &sipRes);
Py_DECREF(result);
}
%End
};</pre>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="specification_files.html"
title="previous chapter">SIP Specification Files</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="annotations.html"
title="next chapter">Annotations</a></p>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="modindex.html" title="Global Module Index"
>modules</a> |</li>
<li class="right" >
<a href="annotations.html" title="Annotations"
>next</a> |</li>
<li class="right" >
<a href="specification_files.html" title="SIP Specification Files"
>previous</a> |</li>
<li><a href="index.html">SIP 4.10.5 Reference Guide</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2010 Riverbank Computing Limited.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
</div>
</body>
</html>
|