summaryrefslogtreecommitdiffstats
path: root/sphinx/python_api.rst
blob: fa90411bd6fd9052eb294af808e70182b9af46c1 (plain)
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
.. _ref-python-api:

Python API for Applications
===========================

.. module:: sip

The main purpose of the :mod:`sip` module is to provide functionality common to
all SIP generated bindings.  It is loaded automatically and most of the time
you will completely ignore it.  However, it does expose some functionality that
can be used by applications.


.. function:: cast(obj, type) -> object

    This does the Python equivalent of casting a C++ instance to one of its
    sub or super-class types.

    :param obj:
        the Python object.
    :param type:
        the type.
    :return:
        a new Python object is that wraps the same C++ instance as *obj*, but
        has the type *type*.


.. function:: delete(obj)

    For C++ instances this calls the C++ destructor.  For C structures it
    returns the structure's memory to the heap.

    :param obj:
        the Python object.


.. function:: dump(obj)

    This displays various bits of useful information about the internal state
    of the Python object that wraps a C++ instance or C structure.

    :param obj:
        the Python object.


.. function:: getapi(name) -> version

    .. versionadded:: 4.9

    This returns the version number that has been set for an API.  The version
    number is either set explicitly by a call to :func:`sip.setapi` or
    implicitly by importing the module that defines it.

    :param name:
        the name of the API.
    :return:
        The version number that has been set for the API.  An exception will
        be raised if the API is unknown.


.. function:: isdeleted(obj) -> bool

    This checks if the C++ instance or C structure has been deleted and
    returned to the heap.

    :param obj:
        the Python object.
    :return:
        ``True`` if the C/C++ instance has been deleted.


.. function:: ispyowned(obj) -> bool

    This checks if the C++ instance or C structure is owned by Python.

    :param obj:
        the Python object.
    :return:
        ``True`` if the C/C++ instance is owned by Python.


.. function:: setapi(name, version)

    .. versionadded:: 4.9

    This sets the version number of an API.  An exception is raised if a
    different version number has already been set, either explicitly by a
    previous call, or implicitly by importing the module that defines it.

    :param name:
        the name of the API.
    :param version:
        The version number to set for the API.  Version numbers must be
        greater than or equal to 1.


.. function:: setdeleted(obj)

    This marks the C++ instance or C structure as having been deleted and
    returned to the heap so that future references to it raise an exception
    rather than cause a program crash.  Normally SIP handles such things
    automatically, but there may be circumstances where this isn't possible.

    :param obj:
        the Python object.


.. function:: settracemask(mask)

    If the bindings have been created with SIP's :option:`-r <sip -r>` command
    line option then the generated code will include debugging statements that
    trace the execution of the code.  (It is particularly useful when trying to
    understand the operation of a C++ library's virtual function calls.)

    :param mask:
        the mask that determines which debugging statements are enabled.

    Debugging statements are generated at the following points:

    - in a C++ virtual function (*mask* is ``0x0001``)
    - in a C++ constructor (*mask* is ``0x0002``)
    - in a C++ destructor (*mask* is ``0x0004``)
    - in a Python type's __init__ method (*mask* is ``0x0008``)
    - in a Python type's __del__ method (*mask* is ``0x0010``)
    - in a Python type's ordinary method (*mask* is ``0x0020``).

    By default the trace mask is zero and all debugging statements are
    disabled.


.. data:: SIP_VERSION

    This is a Python integer object that represents the SIP version number as
    a 3 part hexadecimal number (e.g. v4.0.0 is represented as ``0x040000``).
    It was first implemented in SIP v4.2.


.. data:: SIP_VERSION_STR

    This is a Python string object that defines the SIP version number as
    represented as a string.  For development snapshots it will start with
    ``snapshot-``.  It was first implemented in SIP v4.3.


.. function:: transferback(obj)

    This function is a wrapper around :cfunc:`sipTransferBack()`.


.. function:: transferto(obj, owner)

    This function is a wrapper around :cfunc:`sipTransferTo()`.


.. function:: unwrapinstance(obj) -> integer

    This returns the address, as an integer, of a wrapped C/C++ structure or
    class instance.

    :param obj:
        the Python object.
    :return:
        an integer that is the address of the C/C++ instance.


.. class:: voidptr

    This is the type object for the type SIP uses to represent a C/C++
    ``void *``.  It may have a size associated with the address in which case
    the Python buffer protocol is supported.  This means that the memory can
    be treated as a mutable array of bytes when wrapped with the ``buffer()``
    builtin.  The type has the following methods.

    .. method:: __init__(address[, size=-1[, writeable=True]])

        :param address:
            the address, either another :class:`sip.voidptr`, ``None``, a
            Python Capsule, a Python CObject, or an integer.
        :param size:
            the optional associated size of the block of memory and is negative
            if the size is not known.
        :param writeable:
            set if the memory is writeable.  If it is not specified, and
            *address* is a :class:`sip.voidptr` instance then its value will be
            used.

    .. method:: __int__() -> integer

        This returns the address as an integer.

        :return:
            the integer address.

    .. method:: __hex__() -> string

        This returns the address as a hexadecimal string.

        :return:
            the hexadecimal string address.

    .. method:: ascapsule() -> capsule

        .. versionadded:: 4.10

        This returns the address as an unnamed Python Capsule.  This requires
        Python v3.1 or later or Python v2.7 or later.

        :return:
            the Capsule.

    .. method:: ascobject() -> cObject

        This returns the address as a Python CObject.  This is deprecated with
        Python v3.1 or later.

        :return:
            the CObject.

    .. method:: asstring([size=-1]) -> string/bytes

        This returns a copy of the block of memory as a Python v2 string object
        or a Python v3 bytes object.
        
        :param size:
            the number of bytes to copy.  If it is negative then the size
            associated with the address is used.  If there is no associated
            size then an exception is raised.
        :return:
            the string or bytes object.

    .. method:: getsize() -> integer

        This returns the size associated with the address.
        
        :return:
            the associated size which will be negative if there is none.

    .. method:: setsize(size)

        This sets the size associated with the address.
        
        :param size:
            the size to associate.  If it is negative then no size is
            associated.

    .. method:: getwriteable() -> bool

        This returns the writeable state of the memory.

        :return:
            ``True`` if the memory is writeable.

    .. method:: setwriteable(writeable)

        This sets the writeable state of the memory.

        :param writeable:
            the writeable state to set.


.. function:: wrapinstance(addr, type) -> object

    This wraps a C structure or C++ class instance in a Python object.  If the
    instance has already been wrapped then a new reference to the existing
    object is returned.
    
    :param addr:
        the address of the instance as a number.
    :param type:
        the Python type of the instance.
    :return:
        the Python object that wraps the instance.


.. class:: wrapper

    This is the type object of the base type of all instances wrapped by SIP.


.. class:: wrappertype

    This is the type object of the metatype of the :class:`sip.wrapper` type.