Commit f4f23657 authored by Marius Wachtler's avatar Marius Wachtler

Implement next()

parent dcd85bb2
...@@ -311,6 +311,18 @@ extern "C" Box* max(Box* arg0, BoxedTuple* args) { ...@@ -311,6 +311,18 @@ extern "C" Box* max(Box* arg0, BoxedTuple* args) {
return maxElement; return maxElement;
} }
extern "C" Box* next(Box* iterator, Box* _default) {
try {
static std::string next_str = "next";
return callattr(iterator, &next_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = false }),
ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
} catch (ExcInfo e) {
if (_default && e.matches(StopIteration))
return _default;
throw;
}
}
extern "C" Box* sum(Box* container, Box* initial) { extern "C" Box* sum(Box* container, Box* initial) {
if (initial->cls == str_cls) if (initial->cls == str_cls)
raiseExcHelper(TypeError, "sum() can't sum strings [use ''.join(seq) instead]"); raiseExcHelper(TypeError, "sum() can't sum strings [use ''.join(seq) instead]");
...@@ -1015,6 +1027,9 @@ void setupBuiltins() { ...@@ -1015,6 +1027,9 @@ void setupBuiltins() {
max_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)max, UNKNOWN, 1, 0, true, false), "max"); max_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)max, UNKNOWN, 1, 0, true, false), "max");
builtins_module->giveAttr("max", max_obj); builtins_module->giveAttr("max", max_obj);
builtins_module->giveAttr("next", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)next, UNKNOWN, 2, 1, false, false), "next", { NULL }));
builtins_module->giveAttr("sum", new BoxedBuiltinFunctionOrMethod( builtins_module->giveAttr("sum", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)sum, UNKNOWN, 2, 1, false, false), "sum", { boxInt(0) })); boxRTFunction((void*)sum, UNKNOWN, 2, 1, false, false), "sum", { boxInt(0) }));
......
...@@ -51,6 +51,10 @@ def G(): ...@@ -51,6 +51,10 @@ def G():
yield "A"; yield "B"; yield "C" yield "A"; yield "B"; yield "C"
print list(enumerate(G())) print list(enumerate(G()))
print next(iter([]), "default")
print next(iter([]), None)
print next(iter([1]), "default")
class C(object): class C(object):
def __init__(self): def __init__(self):
self.a = 1 self.a = 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment