Skip to content

Commit

Permalink
Make sure iterable is exhausted when encountering the sentinel value.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurdok committed May 24, 2017
1 parent 5dcc80a commit a71c350
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions batavia/types/CallableIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ function CallableIterator(callable, sentinel) {
PyObject.call(this)
this.callable = callable
this.sentinel = sentinel
this.exhausted = false
}

create_pyclass(CallableIterator, 'callable_iterator')

CallableIterator.prototype.__next__ = function() {
if (this.exhausted) {
throw new exceptions.StopIteration.$pyclass()
}

var item = this.callable.__call__([])
if (item.__eq__(this.sentinel)) {
this.exhausted = true
throw new exceptions.StopIteration.$pyclass()
}
return item
Expand Down
18 changes: 18 additions & 0 deletions tests/builtins/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ def test_iter_sentinel_range(self):
print(list(result))
""")

def test_iter_sentinel_repeated(self):
self.assertCodeExecution("""
seq = iter(range(10))
callable = lambda: next(seq)
iterator = iter(callable, 3)
print(next(iterator))
print(next(iterator))
print(next(iterator))
try:
print(next(iterator))
except StopIteration:
pass
try:
print(next(iterator))
except StopIteration:
pass
""")

def test_iter_sentinel_gen(self):
self.assertCodeExecution("""
def gen():
Expand Down

0 comments on commit a71c350

Please sign in to comment.