Skip to content

Commit fd0d3e5

Browse files
committed
more yield from
patch by Serhiy Storchaka
1 parent 8bfcf51 commit fd0d3e5

File tree

5 files changed

+22
-39
lines changed

5 files changed

+22
-39
lines changed

Lib/http/cookiejar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,7 @@ def deepvalues(mapping):
11931193
pass
11941194
else:
11951195
mapping = True
1196-
for subobj in deepvalues(obj):
1197-
yield subobj
1196+
yield from deepvalues(obj)
11981197
if not mapping:
11991198
yield obj
12001199

Lib/json/encoder.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ def _iterencode_list(lst, _current_indent_level):
305305
chunks = _iterencode_dict(value, _current_indent_level)
306306
else:
307307
chunks = _iterencode(value, _current_indent_level)
308-
for chunk in chunks:
309-
yield chunk
308+
yield from chunks
310309
if newline_indent is not None:
311310
_current_indent_level -= 1
312311
yield '\n' + _indent * _current_indent_level
@@ -381,8 +380,7 @@ def _iterencode_dict(dct, _current_indent_level):
381380
chunks = _iterencode_dict(value, _current_indent_level)
382381
else:
383382
chunks = _iterencode(value, _current_indent_level)
384-
for chunk in chunks:
385-
yield chunk
383+
yield from chunks
386384
if newline_indent is not None:
387385
_current_indent_level -= 1
388386
yield '\n' + _indent * _current_indent_level
@@ -404,20 +402,17 @@ def _iterencode(o, _current_indent_level):
404402
elif isinstance(o, float):
405403
yield _floatstr(o)
406404
elif isinstance(o, (list, tuple)):
407-
for chunk in _iterencode_list(o, _current_indent_level):
408-
yield chunk
405+
yield from _iterencode_list(o, _current_indent_level)
409406
elif isinstance(o, dict):
410-
for chunk in _iterencode_dict(o, _current_indent_level):
411-
yield chunk
407+
yield from _iterencode_dict(o, _current_indent_level)
412408
else:
413409
if markers is not None:
414410
markerid = id(o)
415411
if markerid in markers:
416412
raise ValueError("Circular reference detected")
417413
markers[markerid] = o
418414
o = _default(o)
419-
for chunk in _iterencode(o, _current_indent_level):
420-
yield chunk
415+
yield from _iterencode(o, _current_indent_level)
421416
if markers is not None:
422417
del markers[markerid]
423418
return _iterencode

Lib/xml/etree/ElementPath.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,12 @@ def select(context, result):
105105
def prepare_star(next, token):
106106
def select(context, result):
107107
for elem in result:
108-
for e in elem:
109-
yield e
108+
yield from elem
110109
return select
111110

112111
def prepare_self(next, token):
113112
def select(context, result):
114-
for elem in result:
115-
yield elem
113+
yield from result
116114
return select
117115

118116
def prepare_descendant(next, token):

Lib/xml/etree/ElementTree.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,7 @@ def iter(self, tag=None):
459459
if tag is None or self.tag == tag:
460460
yield self
461461
for e in self._children:
462-
for e in e.iter(tag):
463-
yield e
462+
yield from e.iter(tag)
464463

465464
# compatibility
466465
def getiterator(self, tag=None):
@@ -487,8 +486,7 @@ def itertext(self):
487486
if self.text:
488487
yield self.text
489488
for e in self:
490-
for s in e.itertext():
491-
yield s
489+
yield from e.itertext()
492490
if e.tail:
493491
yield e.tail
494492

Tools/importbench/importbench.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ def from_cache(seconds, repeat):
4646
module.__package__ = ''
4747
with util.uncache(name):
4848
sys.modules[name] = module
49-
for result in bench(name, repeat=repeat, seconds=seconds):
50-
yield result
49+
yield from bench(name, repeat=repeat, seconds=seconds)
5150

5251

5352
def builtin_mod(seconds, repeat):
@@ -56,9 +55,8 @@ def builtin_mod(seconds, repeat):
5655
if name in sys.modules:
5756
del sys.modules[name]
5857
# Relying on built-in importer being implicit.
59-
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
60-
seconds=seconds):
61-
yield result
58+
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
59+
seconds=seconds)
6260

6361

6462
def source_wo_bytecode(seconds, repeat):
@@ -73,9 +71,8 @@ def source_wo_bytecode(seconds, repeat):
7371
loader = (importlib.machinery.SourceFileLoader,
7472
importlib.machinery.SOURCE_SUFFIXES, True)
7573
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
76-
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
77-
seconds=seconds):
78-
yield result
74+
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
75+
seconds=seconds)
7976
finally:
8077
sys.dont_write_bytecode = False
8178

@@ -89,9 +86,8 @@ def benchmark_wo_bytecode(seconds, repeat):
8986
os.unlink(bytecode_path)
9087
sys.dont_write_bytecode = True
9188
try:
92-
for result in bench(name, lambda: sys.modules.pop(name),
93-
repeat=repeat, seconds=seconds):
94-
yield result
89+
yield from bench(name, lambda: sys.modules.pop(name),
90+
repeat=repeat, seconds=seconds)
9591
finally:
9692
sys.dont_write_bytecode = False
9793

@@ -127,8 +123,7 @@ def writing_bytecode_benchmark(seconds, repeat):
127123
def cleanup():
128124
sys.modules.pop(name)
129125
os.unlink(imp.cache_from_source(module.__file__))
130-
for result in bench(name, cleanup, repeat=repeat, seconds=seconds):
131-
yield result
126+
yield from bench(name, cleanup, repeat=repeat, seconds=seconds)
132127

133128
writing_bytecode_benchmark.__doc__ = (
134129
writing_bytecode_benchmark.__doc__.format(name))
@@ -148,19 +143,17 @@ def source_using_bytecode(seconds, repeat):
148143
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
149144
py_compile.compile(mapping[name])
150145
assert os.path.exists(imp.cache_from_source(mapping[name]))
151-
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
152-
seconds=seconds):
153-
yield result
146+
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
147+
seconds=seconds)
154148

155149

156150
def _using_bytecode(module):
157151
name = module.__name__
158152
def using_bytecode_benchmark(seconds, repeat):
159153
"""Source w/ bytecode: {}"""
160154
py_compile.compile(module.__file__)
161-
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
162-
seconds=seconds):
163-
yield result
155+
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
156+
seconds=seconds)
164157

165158
using_bytecode_benchmark.__doc__ = (
166159
using_bytecode_benchmark.__doc__.format(name))

0 commit comments

Comments
 (0)