aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/doc/typesystemvariables.rst
blob: a07ba0d8c542ddd1ce5c51bcc43f7b5a89c23f8a (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
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
*********************
Type System Variables
*********************

User written code can be placed in arbitrary places using the
:ref:`inject-code <inject-code>` tag. To ease the binding developer
work, the injected code can make use of special variables that will be replaced
by the correct values. This also shields the developer from some |project|
implementation specifics.


.. _variables:

Variables
=========


.. _cpp_return_argument:

**%0**

  Replaced by the C++ return variable of the Python method/function wrapper.


.. _arg_number:

**%#**

  Replaced by the name of a C++ argument in the position indicated by ``#``.
  The argument counting starts with ``%1``, since ``%0`` represents the return
  variable name. If the number indicates a variable that was removed in the
  type system description, but there is a default value for it, this value will
  be used. Consider this example:

      .. code-block:: c++

          void argRemoval(int a0, int a1 = 123);


      .. code-block:: xml

            <modify-function signature="argRemoval(int, int)">
                <modify-argument index="2">
                    <remove-argument/>
                </modify-argument>
            </modify-function>

  The ``%1`` will be replaced by the C++ argument name, and ``%2`` will get the
  value ``123``.


.. _argument_names:

**%ARGUMENT_NAMES**

  Replaced by a comma separated list with the names of all C++ arguments that
  were not removed on the type system description for the method/function. When
  the removed argument has a default value (original or provided in the type
  system), this value will be inserted in the argument list. If you want to remove
  the argument so completely that it doesn't appear in any form on the
  ``%ARGUMENT_NAMES`` replacement, don't forget to remove also its default value
  with the :ref:`remove-default-expression <remove-default-expression>`  type system tag.


  Take the following method and related type system description as an example:

      .. code-block:: c++

          void argRemoval(int a0, Point a1 = Point(1, 2), bool a2 = true, Point a3 = Point(3, 4), int a4 = 56);


      .. code-block:: xml

            <modify-function signature="argRemoval(int, Point, bool, Point, int)">
                <modify-argument index="2">
                    <remove-argument/>
                    <replace-default-expression with="Point(6, 9)"/>
                </modify-argument>
                <modify-argument index="4">
                    <remove-argument/>
                </modify-argument>
            </modify-function>

  As seen on the XML description, the function's ``a1`` and ``a3`` arguments
  were removed. If any ``inject-code`` for this function uses ``%ARGUMENT_NAMES``
  the resulting list will be the equivalent of using individual argument type
  system variables this way:

      .. code-block:: c++

            %1, Point(6, 9), %3, Point(3, 4), %5


.. _arg_type:

**%ARG#_TYPE**

  Replaced by the type of a C++ argument in the position indicated by ``#``.
  The argument counting starts with ``%1``, since ``%0`` represents the return
  variable in other contexts, but ``%ARG0_TYPE`` will not translate to the
  return type, as this is already done by the
  :ref:`%RETURN_TYPE <return_type>` variable.
  Example:

      .. code-block:: c++

          void argRemoval(int a0, int a1 = 123);


      .. code-block:: xml

            <modify-function signature="argRemoval(int, int)">
                <modify-argument index="2">
                    <remove-argument/>
                </modify-argument>
            </modify-function>

  The ``%1`` will be replaced by the C++ argument name, and ``%2`` will get the
  value ``123``.


.. _converttocpp:

**%CONVERTTOCPP[CPPTYPE]**

  Replaced by a |project| conversion call that converts a Python variable
  to a C++ variable of the type indicated by ``CPPTYPE``.


.. _converttopython:

**%CONVERTTOPYTHON[CPPTYPE]**

  Replaced by a |project| conversion call that converts a C++ variable of the
  type indicated by ``CPPTYPE`` to the proper Python object.


.. _isconvertible:

**%ISCONVERTIBLE[CPPTYPE]**

  Replaced by a |project| "isConvertible" call that checks if a Python
  variable is convertible (via an implicit conversion or cast operator call)
  to a C++ variable of the type indicated by ``CPPTYPE``.


.. _checktype:

**%CHECKTYPE[CPPTYPE]**

  Replaced by a |project| "checkType" call that verifies if a Python
  if of the type indicated by ``CPPTYPE``.


.. _cppself:

**%CPPSELF**

  Replaced by the wrapped C++ object instance that owns the method in which the
  code with this variable was inserted.

.. _cpptype:

**%CPPTYPE**

  Replaced by the original name of the C++ class, without any namespace prefix,
  that owns the method in which the code with this variable was inserted. It will
  work on class level code injections also. Notice that ``CPPTYPE`` differs from
  the :ref:`%TYPE <type>` variable, for this latter may be translated to the original
  C++ class name or to the C++ wrapper class name.

  Namespaces will are treated as classes, so ``CPPTYPE`` will work for them and their
  enclosed functions as well.

.. _function_name:

**%FUNCTION_NAME**

  Replaced by the name of a function or method.



.. _py_return_argument:

**%PYARG_0**

  Replaced by the name of the Python return variable of the Python method/function wrapper.


.. _pyarg:

**%PYARG_#**

  Similar to ``%#``, but is replaced by the Python arguments (PyObjects)
  received by the Python wrapper method.

  If used in the context of a native code injection, i.e. in a virtual method
  override, ``%PYARG_#`` will be translated to one item of the Python tuple
  holding the arguments that should be passed to the Python override for this
  virtual method.

  The example

      .. code-block:: c++

          long a = PyInt_AS_LONG(%PYARG_1);


  is equivalent of

      .. code-block:: c++

          long a = PyInt_AS_LONG(PyTuple_GET_ITEM(%PYTHON_ARGUMENTS, 0));


  The generator tries to be smart with attributions, but it will work for the
  only simplest cases.

  This example

      .. code-block:: c++

           Py_DECREF(%PYARG_1);
           %PYARG_1 = PyInt_FromLong(10);


  is equivalent of

      .. code-block:: c++

          Py_DECREF(PyTuple_GET_ITEM(%PYTHON_ARGUMENTS, 0));
          PyTuple_SET_ITEM(%PYTHON_ARGUMENTS, 0, PyInt_FromLong(10));


.. _pyself:

**%PYSELF**

  Replaced by the Python wrapper variable (a PyObject) representing the instance
  bounded to the Python wrapper method which receives the custom code.


.. _python_arguments:

**%PYTHON_ARGUMENTS**

  Replaced by the pointer to the Python tuple with Python objects converted from
  the C++ arguments received on the binding override of a virtual method.
  This tuple is the same passed as arguments to the Python method overriding the
  C++ parent's one.


.. _python_method_override:

**%PYTHON_METHOD_OVERRIDE**

  This variable is used only on :ref:`native method code injections
  <codeinjecting_method_native>`, i.e. on the binding overrides for C++ virtual
  methods. It is replaced by a pointer to the Python method override.


.. _pythontypeobject:

**%PYTHONTYPEOBJECT**

  Replaced by the Python type object for the context in which it is inserted:
  method or class modification.


.. _beginallowthreads:

**%BEGIN_ALLOW_THREADS**

  Replaced by a thread state saving procedure.
  Must match with a :ref:`%END_ALLOW_THREADS <endallowthreads>` variable.


.. _endallowthreads:

**%END_ALLOW_THREADS**

  Replaced by a thread state restoring procedure.
  Must match with a :ref:`%BEGIN_ALLOW_THREADS <beginallowthreads>` variable.


.. _return_type:

**%RETURN_TYPE**

  Replaced by the type returned by a function or method.


.. _type:

**%TYPE**

  Replaced by the name of the class to which a function belongs. May be used
  in code injected at method or class level.


.. _example:

Example
=======

Just to illustrate the usage of the variables described in the previous
sections, below is an excerpt from the type system description of a |project|
test. It changes a method that received ``argc/argv`` arguments into something
that expects a Python sequence instead.

    .. code-block:: xml

        <modify-function signature="overloadedMethod(int, char**)">
            <modify-argument index="1">
                <replace-type modified-type="PySequence" />
            </modify-argument>
            <modify-argument index="2">
                <remove-argument />
            </modify-argument>
            <inject-code class="target" position="beginning">
                int argc;
                char** argv;
                if (!PySequence_to_argc_argv(%PYARG_1, &amp;argc, &amp;argv)) {
                    PyErr_SetString(PyExc_TypeError, "error");
                    return 0;
                }
                %RETURN_TYPE foo = %CPPSELF.%FUNCTION_NAME(argc, argv);
                %0 = %CONVERTTOPYTHON[%RETURN_TYPE](foo);

                for (int i = 0; i &lt; argc; ++i)
                    delete[] argv[i];
                delete[] argv;
            </inject-code>
        </modify-function>