← index #7712Issue #3204
Related · high · value 0.725
QUERY · ISSUE

bytearray(length) does not validate length is >= 0

openby dhalbertopened 2021-08-26updated 2021-08-28
py-core
>>> bytearray(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError: memory allocation failed, allocating 4294967295 bytes

This is pretty minor, since it does raise an error. CPython raises a ValueError instead:

>>> bytearray(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative count
CANDIDATE · ISSUE

Unix: CPython incompatibility assigning negative values to bytearrays.

openby peterhinchopened 2017-07-09updated 2025-04-04
py-core

This is arguably minor, but I thought it worth flagging up. It also applies to bytes objects.

MicroPython v1.8.7-893-g46b849a on 2017-06-22; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> a = bytearray([0, 1, -1])
>>> a[2]
255
>>> 

Under CPython 3.4.3:

>>> a = bytearray([0, 1, -1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: byte must be in range(0, 256)

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