4
4
# pylint: disable=missing-docstring,invalid-name,import-error, try-except-raise, wrong-import-position,not-callable,raise-missing-from
5
5
import asyncio
6
6
7
+
7
8
class RebornStopIteration (StopIteration ):
8
9
"""
9
10
A class inheriting from StopIteration exception
10
11
"""
11
12
13
+
12
14
# This one is ok
13
15
def gen_ok ():
14
16
yield 1
15
17
yield 2
16
18
yield 3
17
19
20
+
18
21
# pylint should warn about this one
19
22
# because of a direct raising of StopIteration inside generator
20
23
def gen_stopiter ():
@@ -23,6 +26,7 @@ def gen_stopiter():
23
26
yield 3
24
27
raise StopIteration # [stop-iteration-return]
25
28
29
+
26
30
# pylint should warn about this one
27
31
# because of a direct raising of an exception inheriting from StopIteration inside generator
28
32
def gen_stopiterchild ():
@@ -31,13 +35,15 @@ def gen_stopiterchild():
31
35
yield 3
32
36
raise RebornStopIteration # [stop-iteration-return]
33
37
38
+
34
39
# pylint should warn here
35
40
# because of the possibility that next raises a StopIteration exception
36
41
def gen_next_raises_stopiter ():
37
42
g = gen_ok ()
38
43
while True :
39
44
yield next (g ) # [stop-iteration-return]
40
45
46
+
41
47
# This one is the same as gen_next_raises_stopiter
42
48
# but is ok because the next function is inside
43
49
# a try/except block handling StopIteration
@@ -49,6 +55,7 @@ def gen_next_inside_try_except():
49
55
except StopIteration :
50
56
return
51
57
58
+
52
59
# This one is the same as gen_next_inside_try_except
53
60
# but is not ok because the next function is inside
54
61
# a try/except block that don't handle StopIteration
@@ -60,6 +67,7 @@ def gen_next_inside_wrong_try_except():
60
67
except ValueError :
61
68
return
62
69
70
+
63
71
# This one is the same as gen_next_inside_try_except
64
72
# but is not ok because the next function is inside
65
73
# a try/except block that handle StopIteration but reraise it
@@ -71,11 +79,13 @@ def gen_next_inside_wrong_try_except2():
71
79
except StopIteration :
72
80
raise StopIteration # [stop-iteration-return]
73
81
82
+
74
83
# Those two last are ok
75
84
def gen_in_for ():
76
85
for el in gen_ok ():
77
86
yield el
78
87
88
+
79
89
def gen_yield_from ():
80
90
yield from gen_ok ()
81
91
@@ -84,7 +94,7 @@ def gen_dont_crash_on_no_exception():
84
94
g = gen_ok ()
85
95
while True :
86
96
try :
87
- yield next (g ) # [stop-iteration-return]
97
+ yield next (g ) # [stop-iteration-return]
88
98
except ValueError :
89
99
raise
90
100
@@ -97,7 +107,7 @@ def gen_dont_crash_on_uninferable():
97
107
98
108
# https://github.com/PyCQA/pylint/issues/1830
99
109
def gen_next_with_sentinel ():
100
- yield next ([], 42 ) # No bad return
110
+ yield next ([], 42 ) # No bad return
101
111
102
112
103
113
from itertools import count
@@ -113,6 +123,7 @@ def generator_using_next():
113
123
class SomeClassWithNext :
114
124
def next (self ):
115
125
return iter ([1 , 2 , 3 ])
126
+
116
127
def some_gen (self ):
117
128
for value in self .next ():
118
129
yield value
@@ -122,8 +133,39 @@ def some_gen(self):
122
133
123
134
124
135
def something_invalid ():
125
- raise Exception (' cannot iterate this' )
136
+ raise Exception (" cannot iterate this" )
126
137
127
138
128
139
def invalid_object_passed_to_next ():
129
- yield next (something_invalid ()) # [stop-iteration-return]
140
+ yield next (something_invalid ()) # [stop-iteration-return]
141
+
142
+
143
+ # pylint: disable=redefined-builtin,too-many-function-args
144
+ def safeiter (it ):
145
+ """Regression test for issue #7610 when ``next`` builtin is redefined"""
146
+
147
+ def next ():
148
+ while True :
149
+ try :
150
+ return next (it )
151
+ except StopIteration :
152
+ raise
153
+
154
+ it = iter (it )
155
+ while True :
156
+ yield next ()
157
+
158
+ def other_safeiter (it ):
159
+ """Regression test for issue #7610 when ``next`` builtin is redefined"""
160
+
161
+ def next (* things ):
162
+ print (* things )
163
+ while True :
164
+ try :
165
+ return next (it )
166
+ except StopIteration :
167
+ raise
168
+
169
+ it = iter (it )
170
+ while True :
171
+ yield next (1 , 2 )
0 commit comments