[Twisted-Python] Request for help with Twisted bindings in M2Crypto

Craig Rodrigues rodrigc at crodrigues.org
Mon Jul 25 03:55:11 MDT 2016


On Mon, Jul 25, 2016 at 2:03 AM, Matěj Cepl <mcepl at cepl.eu> wrote:

>
> 2) Even more pressing is that the Twisted module breaks my tests when
> porting to py3k (https://travis-ci.org/mcepl/M2Crypto/jobs/146633964).
> Given the opaque and complicated data types in Twisted, I see horribly
> complicated task of diving into it in front of me and I am not eager.
>

I call shenanigans on you.

Twisted is open source, so none of the data types are opaque.
Twisted is probably the best open source project I have worked with
in terms of having documentation which is generated from the code (
https://twistedmatrix.com/documents/current/api/ ).

Twisted is also absolutely *the* best project I have worked with in terms
of having unit tests with very high coverage
of the code.

If you are unfamiliar with Twisted's code and data types, and don't have
the energy to dig in,
then be honest about that, but don't accuse Twisted of being "opaque",
because it isn't.

Regarding your code example which is failing,
your code is failing because you are intermixing bytes and strings which is
a big no-no for Python 3.

If I look at this line for example:
https://gitlab.com/m2crypto/m2crypto/blob/master/M2Crypto/SSL/TwistedProtocolWrapper.py#L357

I see the code is doing stuff like:
   data = ''
   encryptedData = ''

Those are of type str, and need to be of type bytes:

   data = b''
   encryptedData = b''

You need to clean stuff like that up in your code so that you are only
using bytes.

I've really learned this lesson very hard after contributing hundreds of
py3k fixes for Twisted:

      Python 2:
              type(str) == type(bytes)
              type(str)  != type(unicode)
              b"foo" == "foo"
              "foo" != u"foo"

      Python 3:
              type(str) != type(bytes)
              type(unicode) is Gone
              b"foo" != "foo"
              "foo" == u"foo"

There is lots of code out there which uses Python strings and bytes
interchangeably
which "works" under Python2, but breaks big time on Python 3.

--
Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20160725/e6733f8b/attachment-0002.html>


More information about the Twisted-Python mailing list