This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author gregory.p.smith
Recipients Ramchandra Apte, abarnert, christian.heimes, cowlicks, georg.brandl, gregory.p.smith, gvanrossum, josh.r, martin.panter, pitrou, rhettinger, serhiy.storchaka, socketpair, terry.reedy, vstinner
Date 2016-04-25.17:29:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1461605381.45.0.976384880505.issue19251@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-04-25 17:29:41gregory.p.smithsetrecipients: + gregory.p.smith, gvanrossum, georg.brandl, rhettinger, terry.reedy, pitrou, vstinner, christian.heimes, socketpair, Ramchandra Apte, martin.panter, serhiy.storchaka, abarnert, josh.r, cowlicks
2016-04-25 17:29:41gregory.p.smithsetmessageid: <1461605381.45.0.976384880505.issue19251@psf.upfronthosting.co.za>
2016-04-25 17:29:41gregory.p.smithlinkissue19251 messages
2016-04-25 17:29:41gregory.p.smithcreate