2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* 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 15:27:58 -04:00
|
|
|
#include <stdlib.h>
|
2014-01-24 19:17:36 -05:00
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
2014-01-24 19:17:36 -05:00
|
|
|
|
|
|
|
// this is a wrapper object that is turns something that has a __getitem__ method into an iterator
|
|
|
|
|
|
|
|
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 11:15:40 -05:00
|
|
|
STATIC mp_obj_t it_iternext(mp_obj_t self_in) {
|
2014-01-24 19:17:36 -05:00
|
|
|
mp_obj_getitem_iter_t *self = self_in;
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
// try to get next item
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_obj_t value = mp_call_method_n_kw(1, 0, self->args);
|
2014-01-24 19:17:36 -05: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
|
2014-02-15 11:10:44 -05:00
|
|
|
if (mp_obj_get_type(nlr.ret_val) == &mp_type_StopIteration) {
|
2014-04-17 18:19:36 -04:00
|
|
|
// return MP_OBJ_STOP_ITERATION instead of raising StopIteration
|
|
|
|
return MP_OBJ_STOP_ITERATION;
|
2014-01-24 19:17:36 -05:00
|
|
|
} else {
|
|
|
|
// re-raise exception
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(nlr.ret_val);
|
2014-01-24 19:17:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC const mp_obj_type_t it_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_iterator,
|
2014-03-30 15:00:12 -04:00
|
|
|
.getiter = mp_identity,
|
2015-04-04 10:53:11 -04:00
|
|
|
.iternext = it_iternext,
|
2014-01-24 19:17:36 -05:00
|
|
|
};
|
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
// args are those returned from mp_load_method_maybe (ie either an attribute or a method)
|
2014-01-24 19:17:36 -05:00
|
|
|
mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args) {
|
|
|
|
mp_obj_getitem_iter_t *o = m_new_obj(mp_obj_getitem_iter_t);
|
|
|
|
o->base.type = &it_type;
|
|
|
|
o->args[0] = args[0];
|
|
|
|
o->args[1] = args[1];
|
|
|
|
o->args[2] = MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
return o;
|
|
|
|
}
|