File: failure.py

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (87 lines) | stat: -rw-r--r-- 1,464 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
"""See how slow failure creation is"""


import random

from twisted.python import failure

random.seed(10050)
O = [0, 20, 40, 60, 80, 10, 30, 50, 70, 90]
DEPTH = 30


def pickVal():
    return random.choice([None, 1, "Hello", [], {1: 1}, (1, 2, 3)])


def makeLocals(n):
    return ";".join(["x%d = %s" % (i, pickVal()) for i in range(n)])


for nLocals in O:
    for i in range(DEPTH):
        s = """
def deepFailure%d_%d():
    %s
    deepFailure%d_%d()
""" % (
            nLocals,
            i,
            makeLocals(nLocals),
            nLocals,
            i + 1,
        )
    exec(s)

    exec(
        """
def deepFailure%d_%d():
    1 / 0
"""
        % (nLocals, DEPTH)
    )

R = range(5000)


def fail(n):
    for i in R:
        try:
            eval("deepFailure%d_0" % n)()
        except BaseException:
            failure.Failure()


def fail_str(n):
    for i in R:
        try:
            eval("deepFailure%d_0" % n)()
        except BaseException:
            str(failure.Failure())


class PythonException(Exception):
    pass


def fail_easy(n):
    for i in R:
        try:
            failure.Failure(PythonException())
        except BaseException:
            pass


from timer import timeit

# for i in O:
#     timeit(fail, 1, i)

# for i in O:
#     print('easy failing', i, timeit(fail_easy, 1, i))

for i in O:
    print("failing", i, timeit(fail, 1, i))

# for i in O:
#     print('string failing', i, timeit(fail_str, 1, i))