Skip to content

Commit

Permalink
Merge pull request beeware#539 from Nurdok/feature/iter-sentinel
Browse files Browse the repository at this point in the history
Implement the sentinel variant of the 'iter' builtin
  • Loading branch information
glasnt committed May 24, 2017
2 parents 12649b0 + a71c350 commit 5a4364e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
3 changes: 2 additions & 1 deletion batavia/builtins/iter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var exceptions = require('../core').exceptions
var callables = require('../core').callables
var type_name = require('../core').type_name
var types = require('../types')

function iter(args, kwargs) {
if (arguments.length !== 2) {
Expand All @@ -13,7 +14,7 @@ function iter(args, kwargs) {
throw new exceptions.TypeError.$pyclass('iter() expected at least 1 arguments, got 0')
}
if (args.length === 2) {
throw new exceptions.NotImplementedError.$pyclass("Builtin Batavia function 'iter' with callable/sentinel not implemented")
return new types.CallableIterator(args[0], args[1])
}
if (args.length > 2) {
throw new exceptions.TypeError.$pyclass('iter() expected at most 2 arguments, got 3')
Expand Down
2 changes: 2 additions & 0 deletions batavia/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ types['Generator'] = require('./types/Generator')
types['Range'] = require('./types/Range')
types['Slice'] = require('./types/Slice')

types['CallableIterator'] = require('./types/CallableIterator')

/*************************************************************************
* Type comparison defintions that match Python-like behavior.
*************************************************************************/
Expand Down
43 changes: 43 additions & 0 deletions batavia/types/CallableIterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var PyObject = require('../core').Object
var exceptions = require('../core').exceptions
var create_pyclass = require('../core').create_pyclass

/**************************************************
* Callable Iterator
**************************************************/

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
}

CallableIterator.prototype.__iter__ = function() {
return this
}

CallableIterator.prototype.__str__ = function() {
return '<callable_iterator object at 0x99999999>'
}

/**************************************************
* Module exports
**************************************************/

module.exports = CallableIterator
38 changes: 38 additions & 0 deletions tests/builtins/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,44 @@ def test_iter_bytes(self):
print(list(iter(b"abcdefgh")))
""")

def test_iter_sentinel_range(self):
self.assertCodeExecution("""
seq = iter(range(10))
callable = lambda: next(seq)
result = iter(callable, 3)
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():
abc = 'abcdefghij'
for letter in abc:
yield letter
g = gen()
callable = lambda: next(g)
result = iter(callable, 'd')
print(list(result))
""")


class BuiltinIterFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
Expand Down

0 comments on commit 5a4364e

Please sign in to comment.