Skip to content

Commit

Permalink
[WIP] Implement built-in function next
Browse files Browse the repository at this point in the history
`next` implemented by trying to call `__next__` method of
passed object.
Implementation seems to be finished, but this is marked as
work in progress for now to investigate why few test cases
still fail (possibly due to a bug in new or existing code)
  • Loading branch information
abonie committed Mar 18, 2017
1 parent 24a604b commit e7cff00
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
29 changes: 28 additions & 1 deletion batavia/builtins/next.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
var exceptions = require('../core').exceptions
var callables = require('../core').callables
var type_name = require('../core').type_name

function next(args, kwargs) {
// if its iterable return next thing in iterable
// else stop iteration
throw new exceptions.NotImplementedError.$pyclass("Builtin Batavia function 'next' not implemented")
if (arguments.length !== 2) {
throw new exceptions.BataviaError.$pyclass('Batavia calling convention not used.')
}
if (kwargs && Object.keys(kwargs).length > 0) {
throw new exceptions.TypeError.$pyclass('next() takes no keyword arguments')
}
if (!args || args.length === 0) {
throw new exceptions.TypeError.$pyclass('next expected at least 1 arguments, got 0')
}
if (args.length > 2) {
throw new exceptions.TypeError.$pyclass('next expected at most 2 arguments, got ' + args.length)
}

try {
return callables.call_method(args[0], '__next__', [])
} catch (e) {
if (e instanceof exceptions.StopIteration.$pyclass) {
if (args.length === 2) {
return args[1]
} else {
throw e
}
} else {
throw new exceptions.TypeError.$pyclass("'" + type_name(args[0]) + "' object is not an iterator")
}
}
}
next.__doc__ = 'next(iterator[, default])\n\nReturn the next item from the iterator. If default is given and the iterator\nis exhausted, it is returned instead of raising StopIteration.'

Expand Down
20 changes: 1 addition & 19 deletions tests/builtins/test_next.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@


class NextTests(TranspileTestCase):
@expectedFailure

def test_next_success(self):
self.assertCodeExecution("""
i = iter([1])
print(next(i))
""")

@expectedFailure
def test_next_success_with_default(self):
self.assertCodeExecution("""
i = iter([1])
print(next(i, 0))
""")

@expectedFailure
def test_next_exhausted_with_default(self):
self.assertCodeExecution("""
i = iter([])
Expand All @@ -37,22 +35,6 @@ class BuiltinNextFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
functions = ["next"]

not_implemented = [
'test_noargs',
'test_bool',
'test_bytearray',
'test_bytes',
'test_class',
'test_complex',
'test_dict',
'test_float',
'test_frozenset',
'test_int',
'test_list',
'test_None',
'test_NotImplemented',
'test_range',
'test_set',
'test_slice',
'test_str',
'test_tuple',
]

0 comments on commit e7cff00

Please sign in to comment.