mirror of
https://github.com/python/cpython.git
synced 2025-11-24 20:30:18 +00:00
more yield from
patch by Serhiy Storchaka
This commit is contained in:
parent
8bfcf51b5d
commit
fd0d3e5d25
5 changed files with 22 additions and 39 deletions
|
|
@ -1193,8 +1193,7 @@ def deepvalues(mapping):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
mapping = True
|
mapping = True
|
||||||
for subobj in deepvalues(obj):
|
yield from deepvalues(obj)
|
||||||
yield subobj
|
|
||||||
if not mapping:
|
if not mapping:
|
||||||
yield obj
|
yield obj
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -305,8 +305,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
|
||||||
chunks = _iterencode_dict(value, _current_indent_level)
|
chunks = _iterencode_dict(value, _current_indent_level)
|
||||||
else:
|
else:
|
||||||
chunks = _iterencode(value, _current_indent_level)
|
chunks = _iterencode(value, _current_indent_level)
|
||||||
for chunk in chunks:
|
yield from chunks
|
||||||
yield chunk
|
|
||||||
if newline_indent is not None:
|
if newline_indent is not None:
|
||||||
_current_indent_level -= 1
|
_current_indent_level -= 1
|
||||||
yield '\n' + _indent * _current_indent_level
|
yield '\n' + _indent * _current_indent_level
|
||||||
|
|
@ -381,8 +380,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
|
||||||
chunks = _iterencode_dict(value, _current_indent_level)
|
chunks = _iterencode_dict(value, _current_indent_level)
|
||||||
else:
|
else:
|
||||||
chunks = _iterencode(value, _current_indent_level)
|
chunks = _iterencode(value, _current_indent_level)
|
||||||
for chunk in chunks:
|
yield from chunks
|
||||||
yield chunk
|
|
||||||
if newline_indent is not None:
|
if newline_indent is not None:
|
||||||
_current_indent_level -= 1
|
_current_indent_level -= 1
|
||||||
yield '\n' + _indent * _current_indent_level
|
yield '\n' + _indent * _current_indent_level
|
||||||
|
|
@ -404,11 +402,9 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
|
||||||
elif isinstance(o, float):
|
elif isinstance(o, float):
|
||||||
yield _floatstr(o)
|
yield _floatstr(o)
|
||||||
elif isinstance(o, (list, tuple)):
|
elif isinstance(o, (list, tuple)):
|
||||||
for chunk in _iterencode_list(o, _current_indent_level):
|
yield from _iterencode_list(o, _current_indent_level)
|
||||||
yield chunk
|
|
||||||
elif isinstance(o, dict):
|
elif isinstance(o, dict):
|
||||||
for chunk in _iterencode_dict(o, _current_indent_level):
|
yield from _iterencode_dict(o, _current_indent_level)
|
||||||
yield chunk
|
|
||||||
else:
|
else:
|
||||||
if markers is not None:
|
if markers is not None:
|
||||||
markerid = id(o)
|
markerid = id(o)
|
||||||
|
|
@ -416,8 +412,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
|
||||||
raise ValueError("Circular reference detected")
|
raise ValueError("Circular reference detected")
|
||||||
markers[markerid] = o
|
markers[markerid] = o
|
||||||
o = _default(o)
|
o = _default(o)
|
||||||
for chunk in _iterencode(o, _current_indent_level):
|
yield from _iterencode(o, _current_indent_level)
|
||||||
yield chunk
|
|
||||||
if markers is not None:
|
if markers is not None:
|
||||||
del markers[markerid]
|
del markers[markerid]
|
||||||
return _iterencode
|
return _iterencode
|
||||||
|
|
|
||||||
|
|
@ -105,14 +105,12 @@ def prepare_child(next, token):
|
||||||
def prepare_star(next, token):
|
def prepare_star(next, token):
|
||||||
def select(context, result):
|
def select(context, result):
|
||||||
for elem in result:
|
for elem in result:
|
||||||
for e in elem:
|
yield from elem
|
||||||
yield e
|
|
||||||
return select
|
return select
|
||||||
|
|
||||||
def prepare_self(next, token):
|
def prepare_self(next, token):
|
||||||
def select(context, result):
|
def select(context, result):
|
||||||
for elem in result:
|
yield from result
|
||||||
yield elem
|
|
||||||
return select
|
return select
|
||||||
|
|
||||||
def prepare_descendant(next, token):
|
def prepare_descendant(next, token):
|
||||||
|
|
|
||||||
|
|
@ -459,8 +459,7 @@ class Element:
|
||||||
if tag is None or self.tag == tag:
|
if tag is None or self.tag == tag:
|
||||||
yield self
|
yield self
|
||||||
for e in self._children:
|
for e in self._children:
|
||||||
for e in e.iter(tag):
|
yield from e.iter(tag)
|
||||||
yield e
|
|
||||||
|
|
||||||
# compatibility
|
# compatibility
|
||||||
def getiterator(self, tag=None):
|
def getiterator(self, tag=None):
|
||||||
|
|
@ -487,8 +486,7 @@ class Element:
|
||||||
if self.text:
|
if self.text:
|
||||||
yield self.text
|
yield self.text
|
||||||
for e in self:
|
for e in self:
|
||||||
for s in e.itertext():
|
yield from e.itertext()
|
||||||
yield s
|
|
||||||
if e.tail:
|
if e.tail:
|
||||||
yield e.tail
|
yield e.tail
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,8 +46,7 @@ def from_cache(seconds, repeat):
|
||||||
module.__package__ = ''
|
module.__package__ = ''
|
||||||
with util.uncache(name):
|
with util.uncache(name):
|
||||||
sys.modules[name] = module
|
sys.modules[name] = module
|
||||||
for result in bench(name, repeat=repeat, seconds=seconds):
|
yield from bench(name, repeat=repeat, seconds=seconds)
|
||||||
yield result
|
|
||||||
|
|
||||||
|
|
||||||
def builtin_mod(seconds, repeat):
|
def builtin_mod(seconds, repeat):
|
||||||
|
|
@ -56,9 +55,8 @@ def builtin_mod(seconds, repeat):
|
||||||
if name in sys.modules:
|
if name in sys.modules:
|
||||||
del sys.modules[name]
|
del sys.modules[name]
|
||||||
# Relying on built-in importer being implicit.
|
# Relying on built-in importer being implicit.
|
||||||
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
|
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
|
||||||
seconds=seconds):
|
seconds=seconds)
|
||||||
yield result
|
|
||||||
|
|
||||||
|
|
||||||
def source_wo_bytecode(seconds, repeat):
|
def source_wo_bytecode(seconds, repeat):
|
||||||
|
|
@ -73,9 +71,8 @@ def source_wo_bytecode(seconds, repeat):
|
||||||
loader = (importlib.machinery.SourceFileLoader,
|
loader = (importlib.machinery.SourceFileLoader,
|
||||||
importlib.machinery.SOURCE_SUFFIXES, True)
|
importlib.machinery.SOURCE_SUFFIXES, True)
|
||||||
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
|
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
|
||||||
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
|
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
|
||||||
seconds=seconds):
|
seconds=seconds)
|
||||||
yield result
|
|
||||||
finally:
|
finally:
|
||||||
sys.dont_write_bytecode = False
|
sys.dont_write_bytecode = False
|
||||||
|
|
||||||
|
|
@ -89,9 +86,8 @@ def _wo_bytecode(module):
|
||||||
os.unlink(bytecode_path)
|
os.unlink(bytecode_path)
|
||||||
sys.dont_write_bytecode = True
|
sys.dont_write_bytecode = True
|
||||||
try:
|
try:
|
||||||
for result in bench(name, lambda: sys.modules.pop(name),
|
yield from bench(name, lambda: sys.modules.pop(name),
|
||||||
repeat=repeat, seconds=seconds):
|
repeat=repeat, seconds=seconds)
|
||||||
yield result
|
|
||||||
finally:
|
finally:
|
||||||
sys.dont_write_bytecode = False
|
sys.dont_write_bytecode = False
|
||||||
|
|
||||||
|
|
@ -127,8 +123,7 @@ def _writing_bytecode(module):
|
||||||
def cleanup():
|
def cleanup():
|
||||||
sys.modules.pop(name)
|
sys.modules.pop(name)
|
||||||
os.unlink(imp.cache_from_source(module.__file__))
|
os.unlink(imp.cache_from_source(module.__file__))
|
||||||
for result in bench(name, cleanup, repeat=repeat, seconds=seconds):
|
yield from bench(name, cleanup, repeat=repeat, seconds=seconds)
|
||||||
yield result
|
|
||||||
|
|
||||||
writing_bytecode_benchmark.__doc__ = (
|
writing_bytecode_benchmark.__doc__ = (
|
||||||
writing_bytecode_benchmark.__doc__.format(name))
|
writing_bytecode_benchmark.__doc__.format(name))
|
||||||
|
|
@ -148,9 +143,8 @@ def source_using_bytecode(seconds, repeat):
|
||||||
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
|
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
|
||||||
py_compile.compile(mapping[name])
|
py_compile.compile(mapping[name])
|
||||||
assert os.path.exists(imp.cache_from_source(mapping[name]))
|
assert os.path.exists(imp.cache_from_source(mapping[name]))
|
||||||
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
|
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
|
||||||
seconds=seconds):
|
seconds=seconds)
|
||||||
yield result
|
|
||||||
|
|
||||||
|
|
||||||
def _using_bytecode(module):
|
def _using_bytecode(module):
|
||||||
|
|
@ -158,9 +152,8 @@ def _using_bytecode(module):
|
||||||
def using_bytecode_benchmark(seconds, repeat):
|
def using_bytecode_benchmark(seconds, repeat):
|
||||||
"""Source w/ bytecode: {}"""
|
"""Source w/ bytecode: {}"""
|
||||||
py_compile.compile(module.__file__)
|
py_compile.compile(module.__file__)
|
||||||
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
|
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
|
||||||
seconds=seconds):
|
seconds=seconds)
|
||||||
yield result
|
|
||||||
|
|
||||||
using_bytecode_benchmark.__doc__ = (
|
using_bytecode_benchmark.__doc__ = (
|
||||||
using_bytecode_benchmark.__doc__.format(name))
|
using_bytecode_benchmark.__doc__.format(name))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue