Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
py/objslice.c@0:5868e8752d44, 2016-04-16 (annotated)
- Committer:
- pythontech
- Date:
- Sat Apr 16 17:11:56 2016 +0000
- Revision:
- 0:5868e8752d44
Split off library from repl
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pythontech | 0:5868e8752d44 | 1 | /* |
| pythontech | 0:5868e8752d44 | 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| pythontech | 0:5868e8752d44 | 3 | * |
| pythontech | 0:5868e8752d44 | 4 | * The MIT License (MIT) |
| pythontech | 0:5868e8752d44 | 5 | * |
| pythontech | 0:5868e8752d44 | 6 | * Copyright (c) 2013, 2014 Damien P. George |
| pythontech | 0:5868e8752d44 | 7 | * |
| pythontech | 0:5868e8752d44 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| pythontech | 0:5868e8752d44 | 9 | * of this software and associated documentation files (the "Software"), to deal |
| pythontech | 0:5868e8752d44 | 10 | * in the Software without restriction, including without limitation the rights |
| pythontech | 0:5868e8752d44 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| pythontech | 0:5868e8752d44 | 12 | * copies of the Software, and to permit persons to whom the Software is |
| pythontech | 0:5868e8752d44 | 13 | * furnished to do so, subject to the following conditions: |
| pythontech | 0:5868e8752d44 | 14 | * |
| pythontech | 0:5868e8752d44 | 15 | * The above copyright notice and this permission notice shall be included in |
| pythontech | 0:5868e8752d44 | 16 | * all copies or substantial portions of the Software. |
| pythontech | 0:5868e8752d44 | 17 | * |
| pythontech | 0:5868e8752d44 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| pythontech | 0:5868e8752d44 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| pythontech | 0:5868e8752d44 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| pythontech | 0:5868e8752d44 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| pythontech | 0:5868e8752d44 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| pythontech | 0:5868e8752d44 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| pythontech | 0:5868e8752d44 | 24 | * THE SOFTWARE. |
| pythontech | 0:5868e8752d44 | 25 | */ |
| pythontech | 0:5868e8752d44 | 26 | |
| pythontech | 0:5868e8752d44 | 27 | #include <stdlib.h> |
| pythontech | 0:5868e8752d44 | 28 | #include <assert.h> |
| pythontech | 0:5868e8752d44 | 29 | |
| pythontech | 0:5868e8752d44 | 30 | #include "py/nlr.h" |
| pythontech | 0:5868e8752d44 | 31 | #include "py/obj.h" |
| pythontech | 0:5868e8752d44 | 32 | #include "py/runtime0.h" |
| pythontech | 0:5868e8752d44 | 33 | |
| pythontech | 0:5868e8752d44 | 34 | /******************************************************************************/ |
| pythontech | 0:5868e8752d44 | 35 | /* slice object */ |
| pythontech | 0:5868e8752d44 | 36 | |
| pythontech | 0:5868e8752d44 | 37 | #if MICROPY_PY_BUILTINS_SLICE |
| pythontech | 0:5868e8752d44 | 38 | |
| pythontech | 0:5868e8752d44 | 39 | // TODO: This implements only variant of slice with 2 integer args only. |
| pythontech | 0:5868e8752d44 | 40 | // CPython supports 3rd arg (step), plus args can be arbitrary Python objects. |
| pythontech | 0:5868e8752d44 | 41 | typedef struct _mp_obj_slice_t { |
| pythontech | 0:5868e8752d44 | 42 | mp_obj_base_t base; |
| pythontech | 0:5868e8752d44 | 43 | mp_obj_t start; |
| pythontech | 0:5868e8752d44 | 44 | mp_obj_t stop; |
| pythontech | 0:5868e8752d44 | 45 | mp_obj_t step; |
| pythontech | 0:5868e8752d44 | 46 | } mp_obj_slice_t; |
| pythontech | 0:5868e8752d44 | 47 | |
| pythontech | 0:5868e8752d44 | 48 | STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { |
| pythontech | 0:5868e8752d44 | 49 | (void)kind; |
| pythontech | 0:5868e8752d44 | 50 | mp_obj_slice_t *o = MP_OBJ_TO_PTR(o_in); |
| pythontech | 0:5868e8752d44 | 51 | mp_print_str(print, "slice("); |
| pythontech | 0:5868e8752d44 | 52 | mp_obj_print_helper(print, o->start, PRINT_REPR); |
| pythontech | 0:5868e8752d44 | 53 | mp_print_str(print, ", "); |
| pythontech | 0:5868e8752d44 | 54 | mp_obj_print_helper(print, o->stop, PRINT_REPR); |
| pythontech | 0:5868e8752d44 | 55 | mp_print_str(print, ", "); |
| pythontech | 0:5868e8752d44 | 56 | mp_obj_print_helper(print, o->step, PRINT_REPR); |
| pythontech | 0:5868e8752d44 | 57 | mp_print_str(print, ")"); |
| pythontech | 0:5868e8752d44 | 58 | } |
| pythontech | 0:5868e8752d44 | 59 | |
| pythontech | 0:5868e8752d44 | 60 | #if MICROPY_PY_BUILTINS_SLICE_ATTRS |
| pythontech | 0:5868e8752d44 | 61 | STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { |
| pythontech | 0:5868e8752d44 | 62 | if (dest[0] != MP_OBJ_NULL) { |
| pythontech | 0:5868e8752d44 | 63 | // not load attribute |
| pythontech | 0:5868e8752d44 | 64 | return; |
| pythontech | 0:5868e8752d44 | 65 | } |
| pythontech | 0:5868e8752d44 | 66 | mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 67 | if (attr == MP_QSTR_start) { |
| pythontech | 0:5868e8752d44 | 68 | dest[0] = self->start; |
| pythontech | 0:5868e8752d44 | 69 | } else if (attr == MP_QSTR_stop) { |
| pythontech | 0:5868e8752d44 | 70 | dest[0] = self->stop; |
| pythontech | 0:5868e8752d44 | 71 | } else if (attr == MP_QSTR_step) { |
| pythontech | 0:5868e8752d44 | 72 | dest[0] = self->step; |
| pythontech | 0:5868e8752d44 | 73 | } |
| pythontech | 0:5868e8752d44 | 74 | } |
| pythontech | 0:5868e8752d44 | 75 | #endif |
| pythontech | 0:5868e8752d44 | 76 | |
| pythontech | 0:5868e8752d44 | 77 | const mp_obj_type_t mp_type_slice = { |
| pythontech | 0:5868e8752d44 | 78 | { &mp_type_type }, |
| pythontech | 0:5868e8752d44 | 79 | .name = MP_QSTR_slice, |
| pythontech | 0:5868e8752d44 | 80 | .print = slice_print, |
| pythontech | 0:5868e8752d44 | 81 | #if MICROPY_PY_BUILTINS_SLICE_ATTRS |
| pythontech | 0:5868e8752d44 | 82 | .attr = slice_attr, |
| pythontech | 0:5868e8752d44 | 83 | #endif |
| pythontech | 0:5868e8752d44 | 84 | }; |
| pythontech | 0:5868e8752d44 | 85 | |
| pythontech | 0:5868e8752d44 | 86 | mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) { |
| pythontech | 0:5868e8752d44 | 87 | mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t); |
| pythontech | 0:5868e8752d44 | 88 | o->base.type = &mp_type_slice; |
| pythontech | 0:5868e8752d44 | 89 | o->start = ostart; |
| pythontech | 0:5868e8752d44 | 90 | o->stop = ostop; |
| pythontech | 0:5868e8752d44 | 91 | o->step = ostep; |
| pythontech | 0:5868e8752d44 | 92 | return MP_OBJ_FROM_PTR(o); |
| pythontech | 0:5868e8752d44 | 93 | } |
| pythontech | 0:5868e8752d44 | 94 | |
| pythontech | 0:5868e8752d44 | 95 | void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step) { |
| pythontech | 0:5868e8752d44 | 96 | assert(MP_OBJ_IS_TYPE(self_in, &mp_type_slice)); |
| pythontech | 0:5868e8752d44 | 97 | mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 98 | *start = self->start; |
| pythontech | 0:5868e8752d44 | 99 | *stop = self->stop; |
| pythontech | 0:5868e8752d44 | 100 | *step = self->step; |
| pythontech | 0:5868e8752d44 | 101 | } |
| pythontech | 0:5868e8752d44 | 102 | |
| pythontech | 0:5868e8752d44 | 103 | #endif |