cython

Форк
0
/
uninitialized.py 
179 строк · 3.6 Кб
1
# mode: run
2
# tag: control-flow, uninitialized
3

4
def conditional(cond):
5
    """
6
    >>> conditional(True)
7
    []
8
    >>> conditional(False)  # doctest: +ELLIPSIS
9
    Traceback (most recent call last):
10
    ...
11
    UnboundLocalError: ...local variable 'a'...
12
    """
13
    if cond:
14
        a = []
15
    return a
16

17
def inside_loop(iter):
18
    """
19
    >>> inside_loop([1,2,3])
20
    3
21
    >>> inside_loop([])  # doctest: +ELLIPSIS
22
    Traceback (most recent call last):
23
    ...
24
    UnboundLocalError: ...local variable 'i'...
25
    """
26
    for i in iter:
27
        pass
28
    return i
29

30
def try_except(cond):
31
    """
32
    >>> try_except(True)
33
    []
34
    >>> try_except(False)  # doctest: +ELLIPSIS
35
    Traceback (most recent call last):
36
    ...
37
    UnboundLocalError: ...local variable 'a'...
38
    """
39
    try:
40
        if cond:
41
            a = []
42
        raise ValueError
43
    except ValueError:
44
        return a
45

46
def try_finally(cond):
47
    """
48
    >>> try_finally(True)
49
    []
50
    >>> try_finally(False)  # doctest: +ELLIPSIS
51
    Traceback (most recent call last):
52
    ...
53
    UnboundLocalError: ...local variable 'a'...
54
    """
55
    try:
56
        if cond:
57
            a = []
58
        raise ValueError
59
    finally:
60
        return a
61

62
def deleted(cond):
63
    """
64
    >>> deleted(False)
65
    {}
66
    >>> deleted(True)  # doctest: +ELLIPSIS
67
    Traceback (most recent call last):
68
    ...
69
    UnboundLocalError: ...local variable 'a'...
70
    """
71
    a = {}
72
    if cond:
73
        del a
74
    return a
75

76
def test_nested(cond):
77
    """
78
    >>> test_nested(True)
79
    >>> test_nested(False)  # doctest: +ELLIPSIS
80
    Traceback (most recent call last):
81
    ...
82
    UnboundLocalError: ...local variable 'a'...
83
    """
84
    if cond:
85
        def a():
86
            pass
87
    return a()
88

89
def test_outer(cond):
90
    """
91
    >>> test_outer(True)
92
    {}
93
    >>> test_outer(False)  # doctest: +ELLIPSIS
94
    Traceback (most recent call last):
95
    ...
96
    UnboundLocalError: ...local variable 'a'...
97
    """
98
    if cond:
99
        a = {}
100
    def inner():
101
        return a
102
    return a
103

104
def test_inner(cond):
105
    """
106
    >>> test_inner(True)
107
    {}
108
    >>> test_inner(False)  # doctest: +ELLIPSIS
109
    Traceback (most recent call last):
110
    ...
111
    NameError: ...free variable 'a' ... in enclosing scope
112
    """
113
    if cond:
114
        a = {}
115
    def inner():
116
        return a
117
    return inner()
118

119
def test_class(cond):
120
    """
121
    >>> test_class(True)
122
    1
123
    >>> test_class(False)  # doctest: +ELLIPSIS
124
    Traceback (most recent call last):
125
    ...
126
    UnboundLocalError: ...local variable 'A'...
127
    """
128
    if cond:
129
        class A:
130
            x = 1
131
    return A.x
132

133

134
def test_try_except_regression(c):
135
    """
136
    >>> test_try_except_regression(True)
137
    (123,)
138
    >>> test_try_except_regression(False)  # doctest: +ELLIPSIS
139
    Traceback (most recent call last):
140
    ...
141
    UnboundLocalError: ...local variable 'a'...
142
    """
143
    if c:
144
        a = (123,)
145
    try:
146
        return a
147
    except:
148
        return a
149

150

151
def test_try_finally_regression(c):
152
    """
153
    >>> test_try_finally_regression(True)
154
    (123,)
155
    >>> test_try_finally_regression(False)  # doctest: +ELLIPSIS
156
    Traceback (most recent call last):
157
    ...
158
    UnboundLocalError: ...local variable 'a'...
159
    """
160
    if c:
161
        a = (123,)
162
    try:
163
        return a
164
    finally:
165
        return a
166

167

168
def test_expression_calculation_order_bug(a):
169
    """
170
    >>> test_expression_calculation_order_bug(False)
171
    []
172
    >>> test_expression_calculation_order_bug(True)  # doctest: +ELLIPSIS
173
    Traceback (most recent call last):
174
    ...
175
    UnboundLocalError: ...local variable 'b'...
176
    """
177
    if not a:
178
        b = []
179
    return (a or b) and (b or a)
180

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.