Skip to content

islice has different behavior from the standard itertools version. #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kbsriram opened this issue Feb 23, 2024 · 1 comment · Fixed by #23
Closed

islice has different behavior from the standard itertools version. #22

kbsriram opened this issue Feb 23, 2024 · 1 comment · Fixed by #23

Comments

@kbsriram
Copy link
Contributor

While writing tests for a different issue, noticed a few differences with the current islice implementation.

Some examples:

>>> import adafruit_itertools as ait
>>> import itertools as it
>>> list(it.islice("ab", 10))
['a', 'b']
>>> list(ait.islice("ab", 10))
Traceback (most recent call last):
  File "/home/kbs/venv/lib/python3.8/site-packages/adafruit_itertools/__init__.py", line 353, in islice
    yield next(it)
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: generator raised StopIteration
>>> list(it.islice("ab", -1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.
>>> list(ait.islice("ab", -1))
[]
>>> 

I can follow-up with a fix to more closely align the behavior, if this feels worth fixing?

@dhalbert
Copy link
Contributor

Certainly, PR's are welcome to align it better with the CPython version.

kbsriram added a commit to kbsriram/Adafruit_CircuitPython_IterTools that referenced this issue Feb 23, 2024
- added checks during iteration to handle "short" iterators
- added tests to run some basic comparisons between CP and
  this library for `islice`.

Fixes adafruit#22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants