Skip to content

Commit

Permalink
Merge pull request #791 from haukex/time_limit_0
Browse files Browse the repository at this point in the history
Ensure time_limited(0, ...) always returns nothing
  • Loading branch information
bbayles committed Jan 8, 2024
2 parents 91a3821 + 7313c86 commit 3e94640
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions more_itertools/more.py
Original file line number Diff line number Diff line change
Expand Up @@ -3227,6 +3227,8 @@ class time_limited:
stops if the time elapsed is greater than *limit_seconds*. If your time
limit is 1 second, but it takes 2 seconds to generate the first item from
the iterable, the function will run for 2 seconds and not yield anything.
As a special case, when *limit_seconds* is zero, the iterator never
returns anything.
"""

Expand All @@ -3242,6 +3244,9 @@ def __iter__(self):
return self

def __next__(self):
if self.limit_seconds == 0:
self.timed_out = True
raise StopIteration
item = next(self._iterable)
if monotonic() - self._start_time > self.limit_seconds:
self.timed_out = True
Expand Down

0 comments on commit 3e94640

Please sign in to comment.