Skip to content

Commit e740b39

Browse files
committed
builtin: Implement builtin_iter
1 parent 05fb6f3 commit e740b39

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

builtin/builtin.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func init() {
4444
// py.MustNewMethod("input", builtin_input, 0, input_doc),
4545
// py.MustNewMethod("isinstance", builtin_isinstance, 0, isinstance_doc),
4646
// py.MustNewMethod("issubclass", builtin_issubclass, 0, issubclass_doc),
47-
// py.MustNewMethod("iter", builtin_iter, 0, iter_doc),
47+
py.MustNewMethod("iter", builtin_iter, 0, iter_doc),
4848
py.MustNewMethod("len", builtin_len, 0, len_doc),
4949
py.MustNewMethod("locals", py.InternalMethodLocals, 0, locals_doc),
5050
py.MustNewMethod("max", builtin_max, 0, max_doc),
@@ -762,6 +762,39 @@ object.
762762
The globals and locals are dictionaries, defaulting to the current
763763
globals and locals. If only globals is given, locals defaults to it.`
764764

765+
const iter_doc = `iter(iterable) -> iterator
766+
iter(callable, sentinel) -> iterator
767+
768+
Get an iterator from an object. In the first form, the argument must
769+
supply its own iterator, or be a sequence.
770+
In the second form, the callable is called until it returns the sentinel.
771+
`
772+
773+
func builtin_iter(self py.Object, args py.Tuple) (py.Object, error) {
774+
nArgs := len(args)
775+
if nArgs < 1 {
776+
return nil, py.ExceptionNewf(py.TypeError,
777+
"iter expected at least 1 arguments, got %d",
778+
nArgs)
779+
} else if nArgs > 2 {
780+
return nil, py.ExceptionNewf(py.TypeError,
781+
"iter expected at most 2 arguments, got %d",
782+
nArgs)
783+
}
784+
785+
v := args[0]
786+
if nArgs == 1 {
787+
return py.Iter(v)
788+
}
789+
_, ok := v.(*py.Function)
790+
if !ok {
791+
return nil, py.ExceptionNewf(py.TypeError,
792+
"iter(v, w): v must be callable")
793+
}
794+
return nil, py.ExceptionNewf(py.NotImplementedError,
795+
"Not implemented yet")
796+
}
797+
765798
// For code see vm/builtin.go
766799

767800
const len_doc = `len(object) -> integer

0 commit comments

Comments
 (0)