Skip to content

Commit 033d17a

Browse files
committed
py: built-in range now accepts variable number of arguments.
1 parent d793389 commit 033d17a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

py/runtime.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,13 @@ py_obj_t py_builtin___build_class__(py_obj_t o_class_fun, py_obj_t o_class_name)
992992
return o;
993993
}
994994

995-
py_obj_t py_builtin_range(py_obj_t o_arg) {
996-
return py_obj_new_range(0, py_obj_get_int(o_arg), 1);
995+
py_obj_t py_builtin_range(int n_args, const py_obj_t* args) {
996+
switch (n_args) {
997+
case 1: return py_obj_new_range(0, py_obj_get_int(args[0]), 1);
998+
case 2: return py_obj_new_range(py_obj_get_int(args[0]), py_obj_get_int(args[1]), 1);
999+
case 3: return py_obj_new_range(py_obj_get_int(args[0]), py_obj_get_int(args[1]), py_obj_get_int(args[2]));
1000+
default: nlr_jump(py_obj_new_exception_2(q_TypeError, "range expected at most 3 arguments, got %d", (void*)(machine_int_t)n_args, NULL));
1001+
}
9971002
}
9981003

9991004
#ifdef WRITE_CODE
@@ -1031,7 +1036,7 @@ void rt_init(void) {
10311036
py_qstr_map_lookup(&map_builtins, qstr_from_str_static("len"), true)->value = rt_make_function_1(py_builtin_len);
10321037
py_qstr_map_lookup(&map_builtins, qstr_from_str_static("abs"), true)->value = rt_make_function_1(py_builtin_abs);
10331038
py_qstr_map_lookup(&map_builtins, q___build_class__, true)->value = rt_make_function_2(py_builtin___build_class__);
1034-
py_qstr_map_lookup(&map_builtins, qstr_from_str_static("range"), true)->value = rt_make_function_1(py_builtin_range);
1039+
py_qstr_map_lookup(&map_builtins, qstr_from_str_static("range"), true)->value = rt_make_function_var(1, py_builtin_range);
10351040

10361041
next_unique_code_id = 2; // 1 is reserved for the __main__ module scope
10371042
unique_codes = NULL;

0 commit comments

Comments
 (0)