File: specification_files.rst

package info (click to toggle)
sip6 6.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,880 kB
  • sloc: ansic: 175,824; python: 19,041; makefile: 23; cpp: 8
file content (536 lines) | stat: -rw-r--r-- 19,120 bytes parent folder | download
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
.. _ref-specification:

Specification Files
===================

A SIP specification consists of some C/C++ type and function declarations and
some :ref:`directives <ref-directives>`.  The declarations may contain
:ref:`annotations <ref-annotations>` which provide SIP with additional
information that cannot be expressed in C/C++.  SIP does not implement a full
C/C++ parser.


Syntax Definition
-----------------

The following is a semi-formal description of the syntax of a specification
file.

.. parsed-literal::
    *specification* ::= {*module-statement*}

    *module-statement* ::= [*module-directive* | *statement*]

    *module-directive* ::= [
            :directive:`%CompositeModule` |
            :directive:`%Copying` |
            :directive:`%DefaultDocstringFormat` |
            :directive:`%DefaultDocstringSignature` |
            :directive:`%DefaultEncoding` |
            :directive:`%DefaultMetatype` |
            :directive:`%DefaultSupertype` |
            :directive:`%ExportedHeaderCode` |
            :directive:`%ExportedTypeHintCode` |
            :directive:`%Extract` |
            :directive:`%Feature` |
            :directive:`%HideNamespace` |
            :directive:`%Import` |
            :directive:`%Include` |
            :directive:`%InitialisationCode` |
            :directive:`%License` |
            :directive:`%MinimumABIVersion` |
            :directive:`%Module` |
            :directive:`%ModuleCode` |
            :directive:`%ModuleHeaderCode` |
            :directive:`%Platforms` |
            :directive:`%PreInitialisationCode` |
            :directive:`%PostInitialisationCode` |
            :directive:`%Timeline` |
            :directive:`%TypeHintCode` |
            :directive:`%UnitCode` |
            :directive:`%UnitPostIncludeCode` |
            :directive:`%VirtualErrorHandler` |
            *mapped-type* |
            *mapped-type-template*]

    *statement* :: [*class-statement* | *function* | *variable*]

    *class-statement* :: [
            :directive:`%If` |
            *class* |
            *class-template* |
            *enum* |
            *namespace* |
            *opaque-class* |
            *operator* |
            *struct* |
            *union* |
            *typedef* |
            *exception*]

    *class* ::= **class** *name* [**:** *super-classes*] [*class-annotations*]
            **{** {*class-line*} **};**

    *super-classes* ::= [**public** | **protected** | **private**] *name*
            [**,** *super-classes*]

    *class-line* ::= [
            *class-statement* |
            :directive:`%BIGetBufferCode` |
            :directive:`%BIReleaseBufferCode` |
            :directive:`%ConvertFromTypeCode` |
            :directive:`%ConvertToSubClassCode` |
            :directive:`%ConvertToTypeCode` |
            :directive:`%Docstring` |
            :directive:`%FinalisationCode` |
            :directive:`%GCClearCode` |
            :directive:`%GCTraverseCode` |
            :directive:`%InstanceCode` |
            :directive:`%PickleCode` |
            :directive:`%TypeCode` |
            :directive:`%TypeHeaderCode` |
            :directive:`%TypeHintCode` |
            *constructor* |
            *destructor* |
            *method* |
            *static-method* |
            *virtual-method* |
            *special-method* |
            *operator* |
            *virtual-operator* |
            *class-variable* |
            **public:** |
            **public Q_SLOTS:** |
            **public slots:** |
            **protected:** |
            **protected Q_SLOTS:** |
            **protected slots:** |
            **private:** |
            **private Q_SLOTS:** |
            **private slots:** |
            **Q_SIGNALS:** |
            **signals:**]

    *constructor* ::= [**explicit**] *name* **(** [*argument-list*] **)**
            [**noexcept**] [*function-annotations*]
            [*c++-constructor-signature*] **;** [:directive:`%Docstring`]
            [:directive:`%MethodCode`]

    *c++-constructor-signature* ::= **[(** [*argument-list*] **)]**

    *destructor* ::= [**virtual**] **~** *name* **()** [**noexcept**] [**= 0**]
            [*function-annotations*] **;** [:directive:`%MethodCode`]
            [:directive:`%VirtualCatcherCode`]

    *method* ::= [**Q_SIGNAL**] [**Q_SLOT**] *type* *name* **(**
            [*argument-list*] **)** [**const**] [**final**] [**noexcept**]
            [**= 0**] [*function-annotations*] [*c++-signature*] **;**
            [:directive:`%Docstring`] [:directive:`%MethodCode`]

    *c++-signature* ::= **[** *type* **(** [*argument-list*] **)]**

    *static-method* ::= **static** *function*

    *virtual-method* ::= [**Q_SIGNAL**] [**Q_SLOT**] **virtual** *type* *name*
            **(** [*argument-list*] **)** [**const**] [**final**]
            [**noexcept**] [**= 0**] [*function-annotations*] [*c++-signature*]
            **;** [:directive:`%MethodCode`] [:directive:`%VirtualCatcherCode`]
            [:directive:`%VirtualCallCode`]

    *special-method* ::= *type* *special-method-name*
            **(** [*argument-list*] **)** [*function-annotations*] **;**
            [:directive:`%MethodCode`]

    *special-method-name* ::= [**__abs__** | **__add__** | **__and__** |
            **__aiter__** | **__anext__** | **__await__** | **__bool__** |
            **__call__** | **__contains__** | **__delattr__** |
            **__delitem__** | **__div__** | **__eq__** | **__float__** |
            **__floordiv__** | **__ge__** | **__getattr__** |
            **__getattribute__** | **__getitem__** | **__gt__** |
            **__hash__** | **__iadd__** | **__iand__** | **__idiv__** |
            **__ifloordiv__** | **__ilshift__** | **__imatmul__** |
            **__imod__** | **__imul__** | **__index__** | **__int__** |
            **__invert__** | **__ior__** | **__irshift__** | **__isub__** |
            **__iter__** | **__itruediv__** | **__ixor__** | **__le__** |
            **__len__** | **__lshift__** | **__lt__** | **__matmul** |
            **__mod__** | **__mul__** | **__ne__** | **__neg__** |
            **__next__** | **__or__** | **__pos__** | **__repr__** |
            **__rshift__** | **__setattr__** | **__setitem__** | **__str__** |
            **__sub__** | **__truediv__** | **__xor__**]

    *operator* ::= *operator-type*
            **(** [*argument-list*] **)** [**const**] [**final**]
            [**noexcept**] [*function-annotations*] **;**
            [:directive:`%MethodCode`]

    *virtual-operator* ::= **virtual** *operator-type*
            **(** [*argument-list*] **)** [**const**] [**final**]
            [**noexcept**] [**= 0**] [*function-annotations*] **;**
            [:directive:`%MethodCode`] [:directive:`%VirtualCatcherCode`]
            [:directive:`%VirtualCallCode`]

    *operatator-type* ::= [ *operator-function* | *operator-cast* ]

    *operator-function* ::= *type* **operator** *operator-name*

    *operator-cast* ::= **operator** *type*

    *operator-name* ::= [**+** | **-** | ***** | **/** | **%** | **&** |
            **|** | **^** | **<<** | **>>** | **+=** | **-=** | ***=** |
            **/=** | **%=** | **&=** | **|=** | **^=** | **<<=** | **>>=** |
            **~** | **()** | **[]** | **<** | **<=** | **==** | **!=** |
            **>** | **>>=** | **=**]

    *class-variable* ::= [**static**] *variable*

    *class-template* :: = **template** **<** *type-list* **>** *class*

    *mapped-type* :: = :directive:`%MappedType` *base-type*
            [*mapped-type-annotations*]
            **{** {*mapped-type-line*} **};**

    *mapped-type-line* ::= [
            :directive:`%If` |
            :directive:`%ConvertFromTypeCode` |
            :directive:`%ConvertToTypeCode` |
            :directive:`%ReleaseCode` |
            :directive:`%TypeCode` |
            :directive:`%TypeHeaderCode` |
            *enum* |
            *function* |
            *variable*]

    *mapped-type-template* :: = **template** **<** *type-list* **>**
            *mapped-type*

    *enum* ::= **enum** [*enum-key*] [*name*] [**:** *enum-base*]
            [*enum-annotations*] **{** {*enum-line*} **};**

    *enum-key* ::= [**class** | **struct**]

    *enum-base* ::= *base-type*

    *enum-line* ::= [:directive:`%If` | *name* [*enum-annotations*] **,**

    *function* ::= *typed-name* **(** [*argument-list*] **)** [**noexcept**]
            [*function-annotations*] **;** [:directive:`%Docstring`]
            [:directive:`%MethodCode`]

    *namespace* ::= **namespace** *name* [**{** {*namespace-line*} **}**] **;**

    *namespace-line* ::= [:directive:`%TypeHeaderCode` | *statement*]

    *opaque-class* ::= **class** *scoped-name* **;**

    *struct* ::= **struct** *name* **{** {*class-line*} **};**

    *union* ::= **union** *name* **{** {*class-line*} **};**

    *typedef* ::= **typedef** [*typed-name* | *function-pointer*]
            *typedef-annotations* **;**

    *variable*::= *typed-name* [*variable-annotations*] **;**
            [:directive:`%AccessCode`] [:directive:`%GetCode`]
            [:directive:`%SetCode`]

    *exception* ::= :directive:`%Exception` *exception-name* [*exception-base*]
            **{** [:directive:`%TypeHeaderCode`] :directive:`%RaiseCode` **};**

    *exception-name* ::= *scoped-name*

    *exception-base* ::= **(** [*exception-name* | *python-exception*] **)**

    *python-exception* ::= [**SIP_ArithmeticError** | **SIP_AssertionError** |
            **SIP_AttributeError** | **SIP_BaseException** |
            **SIP_BlockingIOError** | **SIP_BrokenPipeError** |
            **SIP_BufferError** | **SIP_ChildProcessError** |
            **SIP_ConnectionAbortedError** | **SIP_ConnectionError** |
            **SIP_ConnectionRefusedError** | **SIP_ConnectionResetError** |
            **SIP_EnvironmentError** | **SIP_EOFError** | **SIP_Exception** |
            **SIP_FileExistsError** | **SIP_FileNotFoundError** |
            **SIP_FloatingPointError** | **SIP_GeneratorExit** |
            **SIP_ImportError** | **SIP_IndentationError** |
            **SIP_IndexError** | **SIP_InterruptedError** | **SIP_IOError** |
            **SIP_IsADirectoryError** | **SIP_KeyboardInterrupt** |
            **SIP_KeyError** | **SIP_LookupError** | **SIP_MemoryError** |
            **SIP_NameError** | **SIP_NotADirectoryError** |
            **SIP_NotImplementedError** | **SIP_OSError** |
            **SIP_OverflowError** | **SIP_PermissionError** |
            **SIP_ProcessLookupError** | **SIP_ReferenceError** |
            **SIP_RuntimeError** | **SIP_StandardError** |
            **SIP_StopIteration** | **SIP_SyntaxError** | **SIP_SystemError** |
            **SIP_SystemExit** | **SIP_TabError** | **SIP_TimeoutError** |
            **SIP_TypeError** | **SIP_UnboundLocalError** |
            **SIP_UnicodeDecodeError** | **SIP_UnicodeEncodeError** |
            **SIP_UnicodeError** | **SIP_UnicodeTranslateError** |
            **SIP_ValueError** | **SIP_VMSError** | **SIP_WindowsError** |
            **SIP_ZeroDivisionError** | **SIP_Warning** |
            **SIP_BytesWarning** | **SIP_DeprecationWarning** |
            **SIP_FutureWarning** | **SIP_ImportWarning** |
            **SIP_PendingDeprecationWarning** | **SIP_ResourceWarning** |
            **SIP_RuntimeWarning** | **SIP_SyntaxWarning** |
            **SIP_UnicodeWarning** | **SIP_UserWarning**]

    *argument-list* ::= *argument* [**,** *argument-list*] [**,** **...**]

    *argument* ::= *type* [*name*] [*argument-annotations*] [*default-value*]

    *default-value* ::= **=** *expression*

    *expression* ::= [*value* | *value* *binary-operator* *expression*]

    *value* ::= [*unary-operator*] *simple-value*

    *simple-value* ::= [*scoped-name* | *function-call* | *real-value* |
            *integer-value* | *boolean-value* | *string-value* |
            *character-value*]

    *typed-name*::= *type* *name*

    *function-pointer*::= *type* **(*** *name* **)(** [*type-list*] **)**

    *type-list* ::= *type* [**,** *type-list*]

    *function-call* ::= *scoped-name* **(** [*value-list*] **)**

    *value-list* ::= *value* [**,** *value-list*]

    *real-value* ::= a floating point number

    *integer-value* ::= a number

    *boolean-value* ::= [**true** | **false**]

    *string-value* ::= **"** {*character*} **"**

    *character-value* ::= **'** *character* **'**

    *unary-operator* ::= [**!** | **~** | **-** | **+** | **\*** | **&**]

    *binary-operator* ::= [**-** | **+** | ***** | **/** | **&** | **|**]

    *argument-annotations* ::= see :ref:`ref-arg-annos`

    *class-annotations* ::= see :ref:`ref-class-annos`

    *enum-annotations* ::= see :ref:`ref-enum-annos`

    *function-annotations* ::= see :ref:`ref-function-annos`

    *mapped-type-annotations* ::= see :ref:`ref-mapped-type-annos`

    *typedef-annotations* ::= see :ref:`ref-typedef-annos`

    *variable-annotations* ::= see :ref:`ref-variable-annos`

    *type* ::= [**const**] *base-type* {*****} [**&**]

    *type-list* ::= *type* [**,** *type-list*]

    *base-type* ::= [*scoped-name* | *template* |
            **struct** *scoped-name* |
            **union** *scoped-name* |
            **char** | **signed char** | **unsigned char** | **wchar_t** |
            **int** | **unsigned** | **unsigned int** | **size_t** |
            **short** | **unsigned short** |
            **long** | **unsigned long** |
            **long long** | **unsigned long long** |
            **float** | **double** |
            **bool** |
            **void** |
            **Py_hash_t** |
            **Py_ssize_t** |
            **PyObject** |
            :stype:`SIP_PYBUFFER` |
            :stype:`SIP_PYCALLABLE` |
            :stype:`SIP_PYDICT` |
            :stype:`SIP_PYENUM` |
            :stype:`SIP_PYLIST` |
            :stype:`SIP_PYOBJECT` |
            :stype:`SIP_PYSLICE` |
            :stype:`SIP_PYTUPLE` |
            :stype:`SIP_PYTYPE`]

    *scoped-name* ::= *name* [**::** *scoped-name*]

    *template* ::= *scoped-name* **<** *type-list* **>**

    *dotted-name* ::= *name* [**.** *dotted-name*]

    *name* ::= _A-Za-z {_A-Za-z0-9}

Here is a short list of differences between C++ and the subset supported by
SIP that might trip you up.

    - SIP does not support the use of ``[]`` in types.  Use pointers instead.

    - A global ``operator`` can only be defined if its first argument is a
      class or a named enum that has been wrapped in the same module.

    - Variables declared outside of a class are effectively read-only.


Variable Numbers of Arguments
-----------------------------

SIP supports the use of ``...`` as the last part of a function signature.  Any
remaining arguments are collected as a Python tuple.


Additional SIP Types
--------------------

SIP supports a number of additional data types that can be used in Python
signatures.

.. sip-type:: SIP_PYBUFFER

This is a ``PyObject *`` that implements the Python buffer protocol.


.. sip-type:: SIP_PYCALLABLE

This is a ``PyObject *`` that is a Python callable object.


.. sip-type:: SIP_PYDICT

This is a ``PyObject *`` that is a Python dictionary object.


.. sip-type:: SIP_PYENUM

This is a ``PyObject *`` that is a Python enum object.


.. sip-type:: SIP_PYLIST

This is a ``PyObject *`` that is a Python list object.


.. sip-type:: SIP_PYOBJECT

This is a ``PyObject *`` of any Python type.  The type ``PyObject *`` can also
be used.


.. sip-type:: SIP_PYSLICE

This is a ``PyObject *`` that is a Python slice object.


.. sip-type:: SIP_PYTUPLE

This is a ``PyObject *`` that is a Python tuple object.


.. sip-type:: SIP_PYTYPE

This is a ``PyObject *`` that is a Python type object.


Python API vs. C/C++ API
------------------------

It is important to understand that a SIP specification describes the Python
API, i.e. the API available to the Python programmer when they ``import`` the
generated module.  It does not have to accurately represent the underlying
C/C++ library.  There is nothing wrong with omitting functions that make
little sense in a Python context, or adding functions implemented with
handwritten code that have no C/C++ equivalent.  It is even possible (and
sometimes necessary) to specify a different super-class hierarchy for a C++
class.  All that matters is that the generated code compiles properly.

In most cases the Python API matches the C/C++ API.  In some cases handwritten
code (see :directive:`%MethodCode`) is used to map from one to the other
without SIP having to know the details itself.  However, there are a few cases
where SIP generates a thin wrapper around a C++ method or constructor (see
:ref:`ref-derived-classes`) and needs to know the exact C++ signature.  To deal
with these cases SIP allows two signatures to be specified.  For example::

    class Klass
    {
    public:
        // The Python signature is a tuple, but the underlying C++ signature
        // is a 2 element array.
        Klass(SIP_PYTUPLE) [(int *)];
    %MethodCode
            int iarr[2];

            if (PyArg_ParseTuple(a0, "ii", &iarr[0], &iarr[1]))
            {
                // Note that we use the SIP generated derived class
                // constructor.
                Py_BEGIN_ALLOW_THREADS
                sipCpp = new sipKlass(iarr);
                Py_END_ALLOW_THREADS
            }
    %End
    };


Namespaces
----------

SIP implements C++ namespaces as a Python class which cannot be instantiated.
The contents of the namespace, including nested namespaces, are implemented as
attributes of the class.

The namespace class is created in the module that SIP is parsing when it first
sees the namespace defined.  If a function (for example) is defined in a
namespace that is first defined in another module then the function is added to
the namespace class in that other module.

Say that we have a file ``a.sip`` that defines a module ``a_module`` as
follows::

    %Module a_module

    namespace N
    {
        void hello();
    };

We also have a file ``b.sip`` that defines a module ``b_module`` as follows::

    %Module b_module

    %Import a.sip

    namespace N
    {
        void bye();
    };

When SIP parses ``b.sip`` it first sees the ``N`` namespace defined in module
``a_module``.  Therefore it places the ``bye()`` function in the ``N`` Python
class in the ``a_module``.  It does not create an ``N`` Python class in the
``b_module``.  Consequently the following code will call the ``bye()``
function::

    import a_module
    import b_module
    a_module.N.bye()

While this reflects the C++ usage it may not be obvious to the Python
programmer who might expect to call the ``bye()`` function using::

    import b_module
    b_module.N.bye()

In order to achieve this behavior make sure that the ``N`` namespace is first
defined in the ``b_module``.  The following version of ``b.sip`` does this::

    %Module b_module

    namespace N;

    %Import a.sip

    namespace N
    {
        void bye();
    };

Alternatively you could just move the :directive:`%Import` directive so that it
is at the end of the file.