File: deflate_compress_memory_error.py

package info (click to toggle)
micropython 1.26.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 50,196 kB
  • sloc: ansic: 324,551; python: 63,215; xml: 4,241; makefile: 3,618; sh: 1,586; javascript: 754; asm: 723; cpp: 83; exp: 11; pascal: 6
file content (39 lines) | stat: -rw-r--r-- 809 bytes parent folder | download
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
# Test deflate.DeflateIO compression, with out-of-memory errors.

try:
    # Check if deflate is available.
    import deflate
    import io
except ImportError:
    print("SKIP")
    raise SystemExit

# Check if compression is enabled.
if not hasattr(deflate.DeflateIO, "write"):
    print("SKIP")
    raise SystemExit

# Create a compressor object.
b = io.BytesIO()
g = deflate.DeflateIO(b, deflate.RAW, 15)

# Then, use up most of the heap.
l = []
while True:
    try:
        l.append(bytearray(1000))
    except:
        break
l.pop()

# Try to compress.  This will try to allocate a large window and fail.
try:
    g.write("test")
except MemoryError:
    print("MemoryError")

# Should still be able to close the stream.
g.close()

# The underlying output stream should be unchanged.
print(b.getvalue())