Skip to content

Commit

Permalink
Ensure time_limited(0, ...) always returns nothing
Browse files Browse the repository at this point in the history
As per the tests, this is the intended behavior, but it does not
actually work if `monotonic()` returns the same value twice
(e.g. on Windows).
  • Loading branch information
haukex committed Jan 7, 2024
1 parent 91a3821 commit 7313c86
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 7313c86

Please sign in to comment.