[Python-Dev] bitwise operations for bytes and bytearray

Joe Jevnik jjevnik at quantopian.com
Thu Jan 7 17:39:12 EST 2016


You want to put the `xor` method in the `nb_xor` field of the
`PyNumberMethods` structure that lives in the `tp_as_number` field of the
bytes type object. Two things I noticed in a quick pass: you might want to
add some type checking around the case where `a` or `b` is not a
`PyByteArray` object. Also, variable length arrays are added in C99, so you
will need to manually allocate the `raw_*` arrays in the heap. This seems
like a cool feature!

On Thu, Jan 7, 2016 at 5:26 PM, Blake Griffith <blake.a.griffith at gmail.com>
wrote:

> Hi!
>
> I'm interested in adding the functionality to do something like:
>
> >>>  b'a' ^ b'b'
> b'\x03'
>
>
> Instead of the good ol' TypeError.
>
> I think both bytes and bytearray should support all the bitwise operations.
>
> I've never hacked on cpython before. I'm starting by just trying to add
> xor to bytearray. I have a ByteArray_Xor function that I think should do
> this here
> https://github.com/cowlicks/cpython/commit/d6dddb11cdb33032b39dcb9dfdaa7b10d4377b5f
>
> But I'm not sure how to hook this in to the rest of cypython. I tried
> adding it where bytearray_as_sequence is declared in this
> bytearrayobject.c file. But that gave me compiler warnings and broke things.
>
> So now that I have this ByteArray_Xor function, how do I make it be
> bytearray.__xor___?
>
> Thanks!
>
> Blake
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/joe%40quantopian.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160107/377aaa7f/attachment.html>


More information about the Python-Dev mailing list