File: extreme_exc.py

package info (click to toggle)
giac 1.9.0.93%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 117,732 kB
  • sloc: cpp: 404,272; ansic: 205,462; python: 30,548; javascript: 28,788; makefile: 17,997; yacc: 2,690; lex: 2,464; sh: 705; perl: 314; lisp: 216; asm: 62; java: 41; xml: 36; sed: 16; csh: 7; pascal: 6
file content (85 lines) | stat: -rw-r--r-- 2,790 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
# test some extreme cases of allocating exceptions and tracebacks

import micropython

# Check for stackless build, which can't call functions without
# allocating a frame on the heap.
try:
    def stackless(): pass
    micropython.heap_lock(); stackless(); micropython.heap_unlock()
except RuntimeError:
    print("SKIP")
    raise SystemExit

# some ports need to allocate heap for the emergency exception
try:
    micropython.alloc_emergency_exception_buf(256)
except AttributeError:
    pass

def main():
    # create an exception with many args while heap is locked
    # should revert to empty tuple for args
    micropython.heap_lock()
    e = Exception(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    micropython.heap_unlock()
    print(repr(e))

    # create an exception with a long formatted error message while heap is locked
    # should use emergency exception buffer and truncate the message
    def f():
        pass
    micropython.heap_lock()
    try:
        f(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=1)
    except Exception as er:
        e = er
    micropython.heap_unlock()
    print(repr(e)[:10])

    # create an exception with a long formatted error message while heap is low
    # should use the heap and truncate the message
    lst = []
    while 1:
        try:
            lst = [lst]
        except MemoryError:
            break
    try:
        f(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=1)
    except Exception as er:
        e = er
    lst[0] = None
    lst = None
    print(repr(e)[:10])

    # raise a deep exception with the heap locked
    # should use emergency exception and be unable to resize traceback array
    def g():
        g()
    micropython.heap_lock()
    try:
        g()
    except Exception as er:
        e = er
    micropython.heap_unlock()
    print(repr(e)[:13])

    # create an exception on the heap with some traceback on the heap, but then
    # raise it with the heap locked so it can't allocate any more traceback
    exc = Exception('my exception')
    try:
        raise exc
    except:
        pass
    def h(e):
        raise e
    micropython.heap_lock()
    try:
        h(exc)
    except Exception as er:
        e = er
    micropython.heap_unlock()
    print(repr(e))

main()