binary data through dbus-python

Simon McVittie simon.mcvittie at collabora.co.uk
Wed Dec 20 14:03:59 PST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 20 Dec 2006 at 22:16:41 +0100, Tomeu Vizoso wrote:
> I need to pass some binary data (for images, audio, etc.) from python to
> python through dbus using the latest dbus-python (this stuff is for the
> olpc).
> 
> What is the recommended way for doing this? Which data types should I
> use in the method signatures?

Array of bytes, 'ay'. There are some more efficient paths in dbus-python 0.80
for this purpose.

> If the answer is to use an array of bytes, what is the best way to
> convert an string to an array of bytes and back? iterating?

Sending:

If you're prepared to depend on dbus-python >= 0.80rc1 (actually
0.79.91 internally) and on the remote object's Introspect method working
correctly, just pass a 'str' object where an array of bytes is
expected, and dbus-python should do the right thing. If you need to
support 0.71 or older, or you're calling a method on some object
that doesn't support Introspect() correctly, or it's for a variant parameter,
use dbus.ByteArray(my_str) to get a str subclass that dbus-python will
understand to be a byte array, at the cost of an extra object allocation
and an extra memcpy().

Receiving:

If you can rely on >= 0.80rc1, pass a keyword argument
byte_arrays=True to the @method decorator, connect_to_signal or
add_signal_receiver (as appropriate), or to the Message object's
get_args_list method if you're using low-level API. You'll get a
ByteArray back - it's a subclass of str, so it implements the read-only
buffer interface that your imaging library probably uses.

Currently, doing byte_array[i] on a ByteArray gives you a Byte (an
int subclass) but I think I'm going to go back to it producing a str of
length 1 before the final release of 0.80. You can always use
str(my_byte_array) to get a real str object, again at a cost of one object
allocation and one memcpy().

If you need to support 0.71 or older, get the list of Byte objects and
do ''.join(map(chr, my_list_of_bytes)) or
''.join([chr(x) for x in my_list_of_bytes]) or some such.

We hope to release dbus-python 0.80 sometime in January.

Regards,
	Simon
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: OpenPGP key: http://www.pseudorandom.co.uk/2003/contact/ or pgp.net

iD8DBQFFibNPWSc8zVUw7HYRAp4rAJsEx+v3yXhEe6mL6NQtM9Ewqs7+HwCcCbFD
1fquK32NWzAp4qdq5fDNsWA=
=iEHv
-----END PGP SIGNATURE-----


More information about the dbus mailing list