File: sequenceprotocol.rst

package info (click to toggle)
shiboken 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,248 kB
  • ctags: 8,532
  • sloc: cpp: 50,923; python: 5,950; xml: 2,354; makefile: 144; ansic: 136
file content (23 lines) | stat: -rw-r--r-- 1,751 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Sequence Protocol
-----------------

Support for the sequence protocol is achieved adding functions with special names, this is done using the add-function tag.

The special function names are:

    ============= =============================================== ==================== ===================
    Function name Parameters                                      Return type          CPython equivalent
    ============= =============================================== ==================== ===================
    __len__       PyObject* self                                  Py_ssize_t           PySequence_Size
    __getitem__   PyObject* self, Py_ssize_t _i                   PyObject*            PySequence_GetItem
    __setitem__   PyObject* self, Py_ssize_t _i, PyObject* _value int                  PySequence_SetItem
    __contains__  PyObject* self, PyObject* _value                int                  PySequence_Contains
    __concat__    PyObject* self, PyObject* _other                PyObject*            PySequence_Concat
    ============= =============================================== ==================== ===================

You just need to inform the function name to the add-function tag, without any parameter or return type information, when you do it, |project| will create a C function with parameters and return type definied by the table above.

The function needs to follow the same semantics of the *CPython equivalent* function, the only way to do it is using the :doc:`inject-code <codeinjectionsemantics>` tag.

A concrete exemple how to add sequence protocol support to a class can be found on shiboken tests, more precisely in the definition of the Str class in ``tests/samplebinding/typesystem_sample.xml``.