2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-26 19:27:58 +00:00
|
|
|
#include <stdlib.h>
|
2014-01-25 00:17:36 +00:00
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/runtime.h"
|
2014-01-25 00:17:36 +00:00
|
|
|
|
2016-01-23 21:27:05 +00:00
|
|
|
// this is a wrapper object that turns something that has a __getitem__ method into an iterator
|
2014-01-25 00:17:36 +00:00
|
|
|
|
|
|
|
typedef struct _mp_obj_getitem_iter_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t args[3];
|
|
|
|
} mp_obj_getitem_iter_t;
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t it_iternext(mp_obj_t self_in) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_getitem_iter_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-25 00:17:36 +00:00
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
// try to get next item
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t value = mp_call_method_n_kw(1, 0, self->args);
|
2014-01-25 00:17:36 +00:00
|
|
|
self->args[2] = MP_OBJ_NEW_SMALL_INT(MP_OBJ_SMALL_INT_VALUE(self->args[2]) + 1);
|
|
|
|
nlr_pop();
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
// an exception was raised
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_type_t *t = (mp_obj_type_t *)((mp_obj_base_t *)nlr.ret_val)->type;
|
2015-08-30 01:33:21 +01:00
|
|
|
if (t == &mp_type_StopIteration || t == &mp_type_IndexError) {
|
2014-04-17 23:19:36 +01:00
|
|
|
return MP_OBJ_STOP_ITERATION;
|
2014-01-25 00:17:36 +00:00
|
|
|
} else {
|
|
|
|
// re-raise exception
|
2015-11-27 17:01:44 +00:00
|
|
|
nlr_jump(nlr.ret_val);
|
2014-01-25 00:17:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:38:38 +01:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
mp_type_it,
|
|
|
|
MP_QSTR_iterator,
|
2022-09-16 14:57:38 +01:00
|
|
|
MP_TYPE_FLAG_ITER_IS_ITERNEXT,
|
|
|
|
iter, it_iternext
|
2021-07-14 05:38:38 +01:00
|
|
|
);
|
2014-01-25 00:17:36 +00:00
|
|
|
|
2014-03-30 13:35:08 +01:00
|
|
|
// args are those returned from mp_load_method_maybe (ie either an attribute or a method)
|
2016-01-09 23:14:54 +00:00
|
|
|
mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf) {
|
|
|
|
assert(sizeof(mp_obj_getitem_iter_t) <= sizeof(mp_obj_iter_buf_t));
|
|
|
|
mp_obj_getitem_iter_t *o = (mp_obj_getitem_iter_t *)iter_buf;
|
2021-03-26 02:48:34 +00:00
|
|
|
o->base.type = &mp_type_it;
|
2014-01-25 00:17:36 +00:00
|
|
|
o->args[0] = args[0];
|
|
|
|
o->args[1] = args[1];
|
|
|
|
o->args[2] = MP_OBJ_NEW_SMALL_INT(0);
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-01-25 00:17:36 +00:00
|
|
|
}
|