8000 py: Add optional support for 2-argument version of built-in next(). · micropython/micropython@4286383 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4286383

Browse files
stinosdpgeorge
authored andcommitted
py: Add optional support for 2-argument version of built-in next().
Configurable via MICROPY_PY_BUILTINS_NEXT2, disabled by default.
1 parent 5157762 commit 4286383

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

ports/unix/mpconfigport_coverage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define MICROPY_MODULE_GETATTR (1)
4040
#define MICROPY_PY_DELATTR_SETATTR (1)
4141
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1)
42+
#define MICROPY_PY_BUILTINS_NEXT2 (1)
4243
#define MICROPY_PY_BUILTINS_RANGE_BINOP (1)
4344
#define MICROPY_PY_BUILTINS_HELP (1)
4445
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)

py/builtin.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ MP_DECLARE_CONST_FUN_OBJ_1(mp_builtin_len_obj);
6363
MP_DECLARE_CONST_FUN_OBJ_0(mp_builtin_locals_obj);
6464
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_max_obj);
6565
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_min_obj);
66+
#if MICROPY_PY_BUILTINS_NEXT2
67+
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_next_obj);
68+
#else
6669
MP_DECLARE_CONST_FUN_OBJ_1(mp_builtin_next_obj);
70+
#endif
6771
MP_DECLARE_CONST_FUN_OBJ_1(mp_builtin_oct_obj);
6872
MP_DECLARE_CONST_FUN_OBJ_1(mp_builtin_ord_obj);
6973
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj);

py/modbuiltins.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_min_obj, 1, mp_builtin_min);
316316

317317
#endif
318318

319+
#if MICROPY_PY_BUILTINS_NEXT2
320+
STATIC mp_obj_t mp_builtin_next(size_t n_args, const mp_obj_t *args) {
321+
if (n_args == 1) {
322+
mp_obj_t ret = mp_iternext_allow_raise(args[0]);
323+
if (ret == MP_OBJ_STOP_ITERATION) {
324+
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
325+
} else {
326+
return ret;
327+
}
328+
} else {
329+
mp_obj_t ret = mp_iternext(args[0]);
330+
return ret == MP_OBJ_STOP_ITERATION ? args[1] : ret;
331+
}
332+
}
333+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_next_obj, 1, 2, mp_builtin_next);
334+
#else
319335
STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
320336
mp_obj_t ret = mp_iternext_allow_raise(o);
321337
if (ret == MP_OBJ_STOP_ITERATION) {
@@ -325,6 +341,7 @@ STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
325341
}
326342
}
327343
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
344+
#endif
328345

329346
STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
330347
#if MICROPY_PY_BUILTINS_STR_OP_MODULO

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,11 @@ typedef double mp_float_t;
881881
#define MICROPY_PY_BUILTINS_RANGE_BINOP (0)
882882
#endif
883883

884+
// Support for callling next() with second argument
885+
#ifndef MICROPY_PY_BUILTINS_NEXT2
886+
#define MICROPY_PY_BUILTINS_NEXT2 (0)
887+
#endif
888+
884889
// Whether to support rounding of integers (incl bignum); eg round(123,-1)=120
885890
#ifndef MICROPY_PY_BUILTINS_ROUND_INT
886891
#define MICROPY_PY_BUILTINS_ROUND_INT (0)

tests/basics/builtin_next_arg2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# test next(iter, default)
2+
3+
try:
4+
next(iter([]), 42)
5+
except TypeError: # 2-argument version not supported
6+
print('SKIP')
7+
raise SystemExit
8+
9+
print(next(iter([]), 42))
10+
print(next(iter(range(0)), 42))
11+
print(next((x for x in [0] if x == 1), 43))
12+
13+
def gen():
14+
yield 1
15+
yield 2
16+
17+
g = gen()
18+
print(next(g, 42))
19+
print(next(g, 43))
20+
print(next(g, 44))
21+
22+
class Gen:
23+
def __init__(self):
24+
self.b = False
25+
26+
def __next__(self):
27+
if self.b:
28+
raise StopIteration
29+
self.b = True
30+
return self.b
31+
32+
g = Gen()
33+
print(next(g, 44))
34+
print(next(g, 45))

0 commit comments

Comments
 (0)
0