Skip to content

Commit

Permalink
depgen.py: consume iterators at C speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
skosukhin committed Apr 25, 2024
1 parent 85addc1 commit fe6e10b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 2 additions & 3 deletions mkhelper/depgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import os
import sys

from depgen import map23, open23, zip_longest23
from depgen import exhaust, map23, open23, zip_longest23


class ArgumentParser(argparse.ArgumentParser):
Expand Down Expand Up @@ -542,8 +542,7 @@ def debug_callback(line, msg):
)

if parser:
for _ in parser.parse(in_stream, in_stream.name):
pass
exhaust(parser.parse(in_stream, in_stream.name))

not in_stream_close or in_stream.close()

Expand Down
14 changes: 14 additions & 0 deletions mkhelper/depgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ def map23(foo, iterable):
return list(map(foo, iterable))


if hasattr(sys, "implementation") and sys.implementation.name == "cpython":
# see https://docs.python.org/3/library/itertools.html#itertools-recipes
import collections

def exhaust(it):
return collections.deque(it, maxlen=0)

else:

def exhaust(it):
for _ in it:
pass


def file_in_dir(f, d):
if d:
return os.path.abspath(f).startswith(os.path.abspath(d) + os.path.sep)
Expand Down

0 comments on commit fe6e10b

Please sign in to comment.