[Python-3000] Last call for PEP 3137: Immutable Bytes andMutable Buffer

Christian Heimes lists at cheimes.de
Wed Oct 3 19:30:46 CEST 2007


Terry Reedy wrote:
> | It'd be useful and more efficient if the new buffer type would support
> | the bit wise operations directly:
> |
> | >>> orig_data &= 0x1F
> | TypeError: unsupported operand type(s) for &=: 'bytes' and 'int'
> 
> This sort of broadcast behavior seems like numpy territory to me.  Or 
> better for a buffer subclass.  Write it first in Python, using loops like 
> above (partly for documentation and other implementations), then in C when 
> interest and usage warrents.

The C implementation of the bit wise operations for buffer() gains a
large speed improvement over the Python implementation. I'm not sure if
Guido would like it and I don't have a use case yet but it sounds like a
useful addition to the new buffer() type:

buffer &= smallint
buffer |= smallint
buffer ^= smallint
newbuffer = buffer & smallint
newbuffer = buffer | smallint
newbuffer = buffer ^ smallint

I'm willing to give it a try and implement it if people are interested
in it.

I have an use case for another feature but that's surely out of the
scope for the Python core. For some algorithms especially cryptographic
algorithms I could use a bytes type which contains larger elements than
a char (unsigned int8) and which does overflow (255 + 1 == 0).

for b in bytes(b"....", wordsize=32, signed=True):
    ...

Again, it's just a pipe dream and I tend to say that it doesn't belong
into the core.

> 
> | >>> orig_data &= b"\x1F"
> | TypeError: unsupported operand type(s) for &=: 'bytes' and 'bytes'
> 
> Ugh is my response.  Stick with the first ;-).

Ugh, too :)

Christian



More information about the Python-3000 mailing list