[zeromq-dev] Python bindings strings and bytes

Michel Pelletier pelletier.michel at gmail.com
Wed Nov 11 17:42:39 CET 2015


My personal opinion is that the API should use bytes, and only bytes, and
never return or accept any unicode objects.

It's a bit brutal but then at least the rules are simple.  Pass unicode,
get a clear exception.

On Wed, Nov 11, 2015 at 3:43 AM, Arnaud Loonstra <arnaud at sphaero.org> wrote:

> This is a frequent issue dealing with Python but how do we want to deal
> with strings? Python strings are a bit cumbersome when dealing with C.
>
> For example to use the Zyre bindings in python one needs to do one of
> the following:
>
> > from zyre import Zyre
> > zn = Zyre(b'MyZyreNode')
>
> or
>
> > zn = Zyre('MyZyreNode'.encode('utf-8'))
>
> This will work in both major Python versions.
> The current unittest uses:
>
> > z1 = Zyre('t1')
>
> which only works in Python 2.
>
> In Python 3 this excepts:
>
> Traceback (most recent call last):
>    File "test.py", line 6, in test_all
>      z1 = Zyre('t1')
>    File "/home/people/arnaud/src/zyre/bindings/python/zyre.py", line
> 129, in __init__
>      self._as_parameter_ = lib.zyre_new(args[0]) # Creation of new raw
> type
> ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
>
> We could just use bytes for everything but it has some consequences and
> makes it a bit un-pythonic. For example the Zyre unittest tests for the
> type of a Zyre event:
>
> > self.assertEquals(e.type(), 'join')
>
> type() returns a Python string and not a bytes object.
>
> Actually I don't know of any other way as converting to a python string
> needs encoding. Anybody thoughts about this?
>
> Rg,
>
> Arnaud
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20151111/feea32f7/attachment.htm>


More information about the zeromq-dev mailing list