@@ -44,7 +44,7 @@ func init() {
4444 // py.NewMethod("locals", builtin_locals, py.METH_NOARGS, locals_doc),
4545 // py.NewMethod("max", builtin_max, 0, max_doc),
4646 // py.NewMethod("min", builtin_min, 0, min_doc),
47- // py.NewMethod("next", builtin_next, 0, next_doc),
47+ py .NewMethod ("next" , builtin_next , 0 , next_doc ),
4848 // py.NewMethod("oct", builtin_oct, 0, oct_doc),
4949 // py.NewMethod("ord", builtin_ord, 0, ord_doc),
5050 py .NewMethod ("pow" , builtin_pow , 0 , pow_doc ),
@@ -301,3 +301,30 @@ func builtin___build_class__(self py.Object, args py.Tuple, kwargs py.StringDict
301301 fmt .Printf ("Globals = %v, Locals = %v\n " , fn .Globals , ns )
302302 return cls
303303}
304+
305+ const next_doc = `next(iterator[, default])
306+
307+ Return the next item from the iterator. If default is given and the iterator
308+ is exhausted, it is returned instead of raising StopIteration.`
309+
310+ func builtin_next (self py.Object , args py.Tuple ) (res py.Object ) {
311+ var it , def py.Object
312+
313+ py .UnpackTuple (args , nil , "next" , 1 , 2 , & it , & def )
314+
315+ if def != nil {
316+ defer func () {
317+ if r := recover (); r != nil {
318+ if py .IsException (py .StopIteration , r ) {
319+ // Return defult on StopIteration
320+ res = def
321+ } else {
322+ // Re-raise
323+ panic (r )
324+ }
325+ }
326+ }()
327+ }
328+
329+ return py .Next (it )
330+ }
0 commit comments