← index #1710Issue #1743
Off-topic · high · value 0.577
QUERY · ISSUE

bytearray slice assignment fails for derived types

openby mbueschopened 2015-12-11updated 2024-08-28
docs

The type check for slice assignment to bytearray derived types seems to be a bit too strict:

class MyBytearray(bytearray): pass

a = MyBytearray(4)
b = MyBytearray(4)
a[0:4] = b

results in this error:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
NotImplementedError: array/bytes required on right side
CANDIDATE · ISSUE

uctypes: assign to scalar in an array is not fully implemented

closedby Anton-2opened 2015-12-23updated 2016-01-11
import uctypes

desc = {
    # arr is array at offset 0, of UINT32 elements, array size is 2
    "arr": (uctypes.ARRAY | 0, uctypes.UINT32 | 2),
}

data = bytearray(8)
s = uctypes.struct(uctypes.addressof(data), desc)

s.arr[0] = 0x11223344

gives TypeError: 'struct' object does not support item assignment

It only works for UINT8 (because the array comes out as a bytearray).

I've got a patch+test for this, but #1698 mention I big refactor coming.
Are you interested in it anyway ?

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied