| 1 |
======================================== |
| 2 |
Tutorial - Adding a GBytes Marshaler |
| 3 |
======================================== |
| 4 |
|
| 5 |
This tutorial is based on a request for adding a Python bytes to GBytes argument |
| 6 |
marshaler for introspected functions (`GnomeBug 729541 <http://bugzilla.gnome.org/show_bug.cgi?id=729541>`_). |
| 7 |
|
| 8 |
Working with GI Interactively |
| 9 |
============================= |
| 10 |
|
| 11 |
To get started, we can inspect a GI function which takes a GBytes argument interactively |
| 12 |
to determine what the argument is in terms of GI (ipython is very helpful here): |
| 13 |
|
| 14 |
PyGI exposes GI functions as custom callable objects which also implement the |
| 15 |
`GIFunctionInfo API <https://docs.gtk.org/girepository/class.FunctionInfo.html>`_ and its base classes. |
| 16 |
|
| 17 |
.. code-block:: python |
| 18 |
|
| 19 |
>>> from gi.repository import GLib |
| 20 |
>>> Gio.MemoryInputStream.new_from_bytes |
| 21 |
gi.FunctionInfo(new_from_bytes) |
| 22 |
|
| 23 |
Get the GIArgInfo: |
| 24 |
|
| 25 |
.. code-block:: python |
| 26 |
|
| 27 |
>>> Gio.MemoryInputStream.new_from_bytes.get_arguments() |
| 28 |
(gi.ArgInfo(bytes),) |
| 29 |
>>> arg, = _ |
| 30 |
>>> arg |
| 31 |
gi.ArgInfo(bytes) |
| 32 |
|
| 33 |
Determine argument type using: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
>>> ty = arg.get_type() |
| 38 |
>>> ty |
| 39 |
gi.TypeInfo(type_type_instance) |
| 40 |
>>> ty.get_tag_as_string() |
| 41 |
'interface' |
| 42 |
|
| 43 |
At this point we know the argument type tag is an "interface" or |
| 44 |
`GI_TYPE_TAG_INTERFACE <https://docs.gtk.org/girepository/enum.TypeTag.html>`_ |
| 45 |
so `g_type_info_get_interface <https://docs.gtk.org/girepository/method.TypeInfo.get_interface.html>`_ |
| 46 |
should be valid for the `GITypeInfo <https://docs.gtk.org/girepository/class.TypeInfo.html>`_. |
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
>>> iface = ty.get_interface() |
| 51 |
StructInfo(Bytes) |
| 52 |
|
| 53 |
In this case get_interface() is giving us a |
| 54 |
`GIStructInfo <https://docs.gtk.org/girepository/class.StructInfo.html>`_. |
| 55 |
We can then verify a valid GType is available using `gi_registered_type_info_get_g_type() <https://docs.gtk.org/girepository/method.RegisteredTypeInfo.get_g_type.html>`_ |
| 56 |
(GIRegisteredTypeInfo is a base class of GIStructInfo): |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
|
| 60 |
>>> gtype = iface.get_g_type() |
| 61 |
>>> gtype |
| 62 |
<GType GBytes (12737104)> |
| 63 |
|
| 64 |
We also have to find the fundamental GType for GBytes: |
| 65 |
|
| 66 |
.. code-block:: python |
| 67 |
|
| 68 |
>>> gtype.fundamental |
| 69 |
<GType GBoxed (72)> |
| 70 |
|
| 71 |
Mapping out the C Code |
| 72 |
====================== |
| 73 |
|
| 74 |
We now have enough information to correlate to the various switch statements in |
| 75 |
the PyGI caching system which will help us place our new marshaling code. Starting |
| 76 |
with `pygi-cache.c:pygi_arg_cache_new <https://git.gnome.org/browse/pygobject/tree/gi/pygi-cache.c?id=3.13.1#n345>`_ |
| 77 |
you can trace through `_arg_cache_new_for_interface <https://git.gnome.org/browse/pygobject/tree/gi/pygi-cache.c?id=3.13.1#n291>`_ |
| 78 |
and finally land in `pygi-struct-marshal.c:pygi_arg_struct_new_from_info <https://git.gnome.org/browse/pygobject/tree/gi/pygi-struct-marshal.c?id=3.13.1#n489>`_. |
| 79 |
|
| 80 |
For this bug, we are looking to add a "from py" marshaling convenience. |
| 81 |
So we could add a new conditional in `_pygi_marshal_from_py_interface_struct <https://git.gnome.org/browse/pygobject/tree/gi/pygi-struct-marshal.c?id=3.13.1#n191>`_ within the G_TYPE_BOXED conditional. However, note this text in the function: |
| 82 |
|
| 83 |
.. code-block:: c |
| 84 |
|
| 85 |
/* FIXME: handle this large if statement in the cache |
| 86 |
* and set the correct marshaller |
| 87 |
*/ |
| 88 |
|
| 89 |
What this means is _pygi_marshal_from_py_interface_struct is actually dispatching |
| 90 |
to sub-types of GIStructInfo at runtime for every argument. Not very ideal considering |
| 91 |
we have this whole caching system for marshaling arguments. |
| 92 |
|
| 93 |
Instead what we should really do is create a new from_py_marshaller and from_py_cleanup |
| 94 |
callback pair specifically for GBytes arguments which are baked in at cache setup time. |
| 95 |
Essentially specializing GBytes as early as possible in |
| 96 |
`_arg_cache_from_py_interface_struct_setup <https://git.gnome.org/browse/pygobject/tree/gi/pygi-struct-marshal.c?id=3.13.1#n434>`_ |
| 97 |
by setting arg_cache->from_py_marshaller and arg_cache->from_py_cleanup. |
| 98 |
|
| 99 |
Marshaler Callbacks |
| 100 |
=================== |
| 101 |
|
| 102 |
Relevant marshaler callbacks are declared in `pygi-cache.h <https://git.gnome.org/browse/pygobject/tree/gi/pygi-cache.h?id=3.13.1#n35>`_ |
| 103 |
and we need an implementation of both PyGIMarshalFromPyFunc and PyGIMarshalFromPyCleanupFunc. |
| 104 |
|
| 105 |
.. code-block:: c |
| 106 |
|
| 107 |
typedef gboolean (*PyGIMarshalFromPyFunc) (PyGIInvokeState *state, |
| 108 |
PyGICallableCache *callable_cache, |
| 109 |
PyGIArgCache *arg_cache, |
| 110 |
PyObject *py_arg, |
| 111 |
GIArgument *arg, |
| 112 |
gpointer *cleanup_data); |
| 113 |
|
| 114 |
typedef void (*PyGIMarshalFromPyCleanupFunc) (PyGIInvokeState *state, |
| 115 |
PyGIArgCache *arg_cache, |
| 116 |
PyObject *py_arg, /* always NULL for to_py cleanup */ |
| 117 |
gpointer data, |
| 118 |
gboolean was_processed); |
| 119 |
|
| 120 |
PyGIMarshalFromPyFunc is called for each argument prior to executing the callee, the relevant bits are as follows: |
| 121 |
|
| 122 |
* py_arg - This is the input PyObject the Python caller is passing to the GI function. |
| 123 |
We need to type check this and do a mini dispatch depending on the type |
| 124 |
(PyBytes or buffer protocol check, PyGIBoxed, and Py_None). |
| 125 |
* arg - This is the target memory area marshaler will fill out. In this case arg->v_pointer |
| 126 |
will be assigned a pointer to a GBytes object. |
| 127 |
* arg_cache->allow_none - If TRUE, py_arg can be Py_None and arg->v_pointer should be set to |
| 128 |
NULL, returning TRUE from the marshaling callback. |
| 129 |
* arg_cache->transfer - Determines how memory should be managed for the argument. |
| 130 |
* cleanup_data - This is an output argument that can be set to custom data which passed back |
| 131 |
to us in the cleanup callback as "data", used for freeing relevant memory after the callee |
| 132 |
returns. In our case this will either be NULL or a GBytes pointer, in which case we should |
| 133 |
call g_bytes_unref() on the data. |
| 134 |
|
| 135 |
PyGIMarshalFromPyCleanupFunc is called after the callee finishes and to cleanup any temporary data |
| 136 |
we created while the callee was running. |
| 137 |
|
| 138 |
Transfer Semantics |
| 139 |
================== |
| 140 |
|
| 141 |
A py_arg input of type PyGIBoxed is a direct wrapping of an existing GBoxed. This is a fairly |
| 142 |
simple case to deal with, we just need to extract the boxed pointer (pyg_boxed_get) and assign |
| 143 |
it to arg->v_pointer. For GI_TRANSFER_EVERYTHING we also need to add a reference the callee can |
| 144 |
own by calling g_bytes_ref on this pointer. |
| 145 |
|
| 146 |
In the case where we are passed a PyBytes (or Python object implementing the buffer protocol), |
| 147 |
we need to create a new GBytes which holds a pointer to the PyBytes data. Zero copy can easily |
| 148 |
be achieved when transfer is GI_TRANSFER_NOTHING because a read-only buffer can be retrieved |
| 149 |
from Python and passed to the GBytes constructor (without a free_func). We know the lifetime |
| 150 |
of the PyBytes is valid at least until the callee completes. The trick here is we also need to |
| 151 |
set *cleanup_data* to the newly created GBytes so our cleanup callback can free the GBytes. |
| 152 |
Since we didn't set a free_func when constructing the GBytes, calling g_bytes_unref will not |
| 153 |
touch our Python owned data. |
| 154 |
|
| 155 |
For converting a PyBytes with transfer mode as GI_TRANSFER_EVERYTHING, we basically follow |
| 156 |
the same as above with some extra tricks. Since the callee is intending to own the GBytes |
| 157 |
we pass it, we must pass it something which is guaranteed to survive after our Python function |
| 158 |
returns (must exist after our cleanup callback). The easiest technique here is to memcpy the result |
| 159 |
of the PyBytes data and construct a GBytes using g_bytes_new_with_free_func, with a free_func of |
| 160 |
g_free and user_data of the bytes (no need for setting cleanup_data because the C callee owns everything). |
| 161 |
|
| 162 |
However, it is possible to achieve zero copy with PyBytes and GI_TRANSFER_EVERYTHING by creating a custom |
| 163 |
free_func which calls Py_DECREF. However, this free_func must wrap any Python API calls with |
| 164 |
PyGILState_Ensure/Release pairs: |
| 165 |
|
| 166 |
.. code-block:: c |
| 167 |
|
| 168 |
void |
| 169 |
threaded_py_bytes_free (PyObject *py_bytes) |
| 170 |
{ |
| 171 |
PyGILState_STATE state = PyGILState_Ensure (); |
| 172 |
Py_DECREF (py_bytes); |
| 173 |
PyGILState_Release (state); |
| 174 |
} |
| 175 |
|
| 176 |
gboolean marshal (...) |
| 177 |
{ |
| 178 |
/* ... py_arg type and transfer checks ... */ |
| 179 |
char *buf = NULL; |
| 180 |
Py_ssize_t length; |
| 181 |
PyBytes_AsStringAndSize (py_arg, &buf, &length); |
| 182 |
arg->v_pointer = g_bytes_new_with_free_func (buf, length, threaded_py_bytes_free, py_arg); |
| 183 |
*cleanup_data = NULL; |
| 184 |
return True; |
| 185 |
|
| 186 |
The above zero copy implementation could also possibly be implemented using memoryviews for accessing a Py_buffer instead of requiring a PyBytes type as input. |
| 187 |
|
| 188 |
Marshaler Implementation |
| 189 |
======================== |
| 190 |
|
| 191 |
This section is left up to the reader as an exercise, remember to write tests! |