cython

Форк
0
/
generators_pep479.pyx 
156 строк · 3.1 Кб
1
# mode: run
2
# tag: generators, pep479
3

4
from __future__ import generator_stop
5

6
# additionally test exception chaining
7
__doc__ = u"""
8
>>> g = test_raise_StopIteration_value()
9
>>> next(g)
10
1
11
>>> try: next(g)
12
... except RuntimeError as exc:
13
...     print(type(exc.__context__) is StopIteration or type(exc.__context__), exc.__context__)
14
...     print(type(exc.__cause__) is StopIteration or type(exc.__cause__), exc.__context__)
15
... else:
16
...     print("NOT RAISED!")
17
True huhu
18
True huhu
19
"""
20

21

22
def test_raise_StopIteration():
23
    """
24
    >>> g = test_raise_StopIteration()
25
    >>> next(g)
26
    1
27
    >>> next(g)
28
    Traceback (most recent call last):
29
    RuntimeError: generator raised StopIteration
30
    """
31
    yield 1
32
    raise StopIteration
33

34

35
def test_raise_StopIteration_value():
36
    """
37
    >>> g = test_raise_StopIteration_value()
38
    >>> next(g)
39
    1
40
    >>> next(g)
41
    Traceback (most recent call last):
42
    RuntimeError: generator raised StopIteration
43
    """
44
    yield 1
45
    raise StopIteration('huhu')
46

47

48
def test_return():
49
    """
50
    >>> g = test_return()
51
    >>> next(g)
52
    1
53
    >>> next(g)
54
    Traceback (most recent call last):
55
    StopIteration
56
    """
57
    yield 1
58
    return
59

60

61
def test_return_value():
62
    """
63
    >>> g = test_return_value()
64
    >>> next(g)
65
    1
66
    >>> next(g)
67
    Traceback (most recent call last):
68
    StopIteration: 2
69
    """
70
    yield 1
71
    return 2
72

73

74
def test_propagate_StopIteration(it):
75
    """
76
    >>> results = []
77
    >>> for x in test_propagate_StopIteration(iter([])):
78
    ...     results.append(x)
79
    Traceback (most recent call last):
80
    RuntimeError: generator raised StopIteration
81
    >>> results
82
    []
83

84
    >>> for x in test_propagate_StopIteration(iter([1, 2])):
85
    ...     results.append(x)
86
    Traceback (most recent call last):
87
    RuntimeError: generator raised StopIteration
88
    >>> results
89
    [1, 2]
90
    """
91
    while True:
92
       yield next(it)
93

94

95
def test_catch_StopIteration(it):
96
    """
97
    >>> for x in test_catch_StopIteration(iter([])):
98
    ...     print(x)
99

100
    >>> for x in test_catch_StopIteration(iter([1, 2])):
101
    ...     print(x)
102
    1
103
    2
104
    """
105
    try:
106
        while True:
107
           yield next(it)
108
    except StopIteration:
109
        pass
110
    else:
111
        print("NOT RAISED!")
112

113

114
def test_yield_from(it):
115
    """
116
    >>> for x in test_yield_from(iter([])):
117
    ...     print(x)
118

119
    >>> for x in test_yield_from(iter([1, 2])):
120
    ...     print(x)
121
    1
122
    2
123
    """
124
    yield from it
125

126

127
def test_yield_from_gen():
128
    """
129
    >>> for x in test_yield_from_gen():
130
    ...     print(x)
131
    1
132
    RETURN: 2
133
    """
134
    x = yield from test_return_value()
135
    print("RETURN: %s" % x)
136

137

138
def test_genexpr(it):
139
    """
140
    >>> list(test_genexpr(iter([])))
141
    []
142
    >>> list(test_genexpr(iter([1, 2])))
143
    [1]
144

145
    >>> list(test_genexpr(iter([1])))
146
    Traceback (most recent call last):
147
    RuntimeError: generator raised StopIteration
148

149
    >>> list(test_genexpr(iter([1, 2, 3])))
150
    Traceback (most recent call last):
151
    RuntimeError: generator raised StopIteration
152

153
    >>> list(test_genexpr(iter([1, 2])))
154
    [1]
155
    """
156
    return (x for x in it if next(it))
157

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

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

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

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