Gregory P. Smith added the comment:

I have wanted bytes/bytearray bit operations (be sure to include the in place 
operators for bytearray) when using micropython where it is normal to be 
operating on binary data.

that said, i'd need someone from micropython to chime in as to if they can 
magically detect
 # Equivalent of:  c = b ^ a
 c = bytes(x ^ y for x, y in zip(a, b))
and make it run fast.

what is a similar expression for an in place bytearray modification?
 # Equivalent of:  a ^= b
 assert len(a) == len(b)
 for i, b_i in enumerate(b): a[i] ^= b_i  ?

Why both of the above are slow is obvious: tons of work looping within python 
itself, creating and destroying small integers and/or tuples the entire time 
instead of deferring to the optimal loop in C.

Neither of the above "look as nice" as a simple operator would.
But they are at least both understandable and frankly about the same as what 
you would naively write in C for the task.

Security claims?  Nonsense. This has nothing to do with security.  It is 
*fundamentally impossible* to write constant time side channel attack resistant 
algorithms in a high level garbage collected language. Do not attempt it.  
Leave that stuff to assembler or _very_ carefully crafted C/C++ that the 
compiler cannot undo constant time enforcing tricks in.  Where it belongs.  
Python will never make such guarantees.

NumPy?  No.  That is a huge bloated dependency.  It is not relevant to this as 
we are not doing scientific computing.  It is not available everywhere.

The int.from_bytes(...) variant optimizations?  Those are hacks that might be 
useful to people in CPython today, but they are much less readable.  Do not 
write that in polite code, hide it behind a function with a comment explaining 
why it's so ugly to anyone who dares look inside please.

So as much as I'd love this feature to exist on bytes & bytearray, it is not a 
big deal that it does not.

Starting with a PyPI module for fast bit operations on bytes & bytearray 
objects makes more sense (include both pure python and a C extension 
implementation).  Use of that will give an idea of how often anyone actually 
wants to do this.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19251>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to