File: heapalloc_fail_bytearray.py

package info (click to toggle)
giac 1.6.0.41%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 64,540 kB
  • sloc: cpp: 351,842; ansic: 105,138; python: 30,545; javascript: 8,675; yacc: 2,690; lex: 2,449; makefile: 1,243; sh: 579; perl: 314; lisp: 216; asm: 62; java: 41; sed: 16; csh: 7; pascal: 6
file content (90 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# test handling of failed heap allocation with bytearray

import micropython

class GetSlice:
    def __getitem__(self, idx):
        return idx
sl = GetSlice()[:]

# create bytearray
micropython.heap_lock()
try:
    bytearray(4)
except MemoryError:
    print('MemoryError: bytearray create')
micropython.heap_unlock()

# create bytearray from bytes
micropython.heap_lock()
try:
    bytearray(b'0123')
except MemoryError:
    print('MemoryError: bytearray create from bytes')
micropython.heap_unlock()

# create bytearray from iterator
r = range(4)
micropython.heap_lock()
try:
    bytearray(r)
except MemoryError:
    print('MemoryError: bytearray create from iter')
micropython.heap_unlock()

# bytearray add
b = bytearray(4)
micropython.heap_lock()
try:
    b + b'01'
except MemoryError:
    print('MemoryError: bytearray.__add__')
micropython.heap_unlock()

# bytearray iadd
b = bytearray(4)
micropython.heap_lock()
try:
    b += b'01234567'
except MemoryError:
    print('MemoryError: bytearray.__iadd__')
micropython.heap_unlock()
print(b)

# bytearray append
b = bytearray(4)
micropython.heap_lock()
try:
    for i in range(100):
        b.append(1)
except MemoryError:
    print('MemoryError: bytearray.append')
micropython.heap_unlock()

# bytearray extend
b = bytearray(4)
micropython.heap_lock()
try:
    b.extend(b'01234567')
except MemoryError:
    print('MemoryError: bytearray.extend')
micropython.heap_unlock()

# bytearray get with slice
b = bytearray(4)
micropython.heap_lock()
try:
    b[sl]
except MemoryError:
    print('MemoryError: bytearray subscr get')
micropython.heap_unlock()

# extend bytearray using slice subscr
b = bytearray(4)
micropython.heap_lock()
try:
    b[sl] = b'01234567'
except MemoryError:
    print('MemoryError: bytearray subscr grow')
micropython.heap_unlock()
print(b)