[Python-Dev] bitwise operations for bytes and bytearray

Andrew Barnert abarnert at yahoo.com
Sat Jan 9 19:31:58 EST 2016


On Jan 9, 2016, at 16:17, Blake Griffith <blake.a.griffith at gmail.com> wrote:
> 
> A little update, I got ^, &, and | working for bytearrays. You can view the diff here:
> https://github.com/python/cpython/compare/master...cowlicks:bitwise-bytes?expand=1

If you upload the diff to the issue on the tracker, the reitveld code review app should be able to pick it up automatically, allowing people to comment on it inline, in a much nicer format than a mailing list thread. It's especially nice if you're adding things in stages--people who have been following along can just look at the changes between patch 3 and 4, while new people can look at all the changes in one go, etc.

> How does it look? 
> Joe, is this how I should allocate the arrays? Am I freeing them properly?
> Am I checking the input enough?
> 
> After some feedback, I'll probably add bitshifting and negation. Then work on bytes objects.
> 
> Does this warrant a pep?

Personally, I'd just make the case for the feature on the tracker issue. If one of the core devs thinks it needs a PEP, or further discussion on this list or -ideas, they'll say so there.

At present, it seems like there's not much support for the idea, but I think that's at least partly because people want to see realistic use cases (that aren't server better by the existing bitarray/bitstring/etc. modules on PyPI, or using a NumPy array, or just using ints, etc.).

>> On Fri, Jan 8, 2016 at 2:08 AM, Cameron Simpson <cs at zip.com.au> wrote:
>>> On 07Jan2016 16:12, Python-Dev <python-dev at python.org> wrote:
>>> On Jan 7, 2016, at 15:57, Martin Panter <vadmium+py at gmail.com> wrote:
>>>>> On 7 January 2016 at 22:26, Blake Griffith <blake.a.griffith at gmail.com> wrote:
>>>>> 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.
>>>> 
>>>> There is a bug open about adding this kind of functionality:
>>>> <https://bugs.python.org/issue19251>.
>>> 
>>> And it's in the needs patch stage, which makes it perfect for the OP: in addition to learning how to hack on builtin types, he can also learn the other parts of the dev process. (Even if the bug is eventually rejected, as seems likely given that it sat around for three years with no compelling use case  and then Guido added a "very skeptical" comment.)
>> 
>> The use case which springs immediately to my mind is cryptography. To encrypt a stream symmetrically you can go:
>> 
>>  cleartext-bytes ^ cryptographicly-random-bytes-from-cipher
>> 
>> so with this one could write:
>> 
>>  def crypted(byteses, crypto_source):
>>    ''' Accept an iterable source of bytes objects and a preprimed source of    crypto bytes, yield encrypted versions of the bytes objects.
>>    '''
>>    for bs in byteses:
>>      cbs = crypto_source.next_bytes(len(bs))
>>      yield bs ^ cbs
>> 
>> Cheers,
>> Cameron Simpson <cs at zip.com.au>
>> _______________________________________________
>> 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/blake.a.griffith%40gmail.com
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160109/4fb53fca/attachment.html>


More information about the Python-Dev mailing list