objslice.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdlib.h>
  27. #include <assert.h>
  28. #include "py/obj.h"
  29. /******************************************************************************/
  30. /* slice object */
  31. #if MICROPY_PY_BUILTINS_SLICE
  32. // TODO: This implements only variant of slice with 2 integer args only.
  33. // CPython supports 3rd arg (step), plus args can be arbitrary Python objects.
  34. typedef struct _mp_obj_slice_t {
  35. mp_obj_base_t base;
  36. mp_obj_t start;
  37. mp_obj_t stop;
  38. mp_obj_t step;
  39. } mp_obj_slice_t;
  40. STATIC void slice_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  41. (void)kind;
  42. mp_obj_slice_t *o = MP_OBJ_TO_PTR(o_in);
  43. mp_print_str(print, "slice(");
  44. mp_obj_print_helper(print, o->start, PRINT_REPR);
  45. mp_print_str(print, ", ");
  46. mp_obj_print_helper(print, o->stop, PRINT_REPR);
  47. mp_print_str(print, ", ");
  48. mp_obj_print_helper(print, o->step, PRINT_REPR);
  49. mp_print_str(print, ")");
  50. }
  51. #if MICROPY_PY_BUILTINS_SLICE_ATTRS
  52. STATIC void slice_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  53. if (dest[0] != MP_OBJ_NULL) {
  54. // not load attribute
  55. return;
  56. }
  57. mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
  58. if (attr == MP_QSTR_start) {
  59. dest[0] = self->start;
  60. } else if (attr == MP_QSTR_stop) {
  61. dest[0] = self->stop;
  62. } else if (attr == MP_QSTR_step) {
  63. dest[0] = self->step;
  64. }
  65. }
  66. #endif
  67. const mp_obj_type_t mp_type_slice = {
  68. { &mp_type_type },
  69. .name = MP_QSTR_slice,
  70. .print = slice_print,
  71. #if MICROPY_PY_BUILTINS_SLICE_ATTRS
  72. .attr = slice_attr,
  73. #endif
  74. };
  75. mp_obj_t mp_obj_new_slice(mp_obj_t ostart, mp_obj_t ostop, mp_obj_t ostep) {
  76. mp_obj_slice_t *o = m_new_obj(mp_obj_slice_t);
  77. o->base.type = &mp_type_slice;
  78. o->start = ostart;
  79. o->stop = ostop;
  80. o->step = ostep;
  81. return MP_OBJ_FROM_PTR(o);
  82. }
  83. void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step) {
  84. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_slice));
  85. mp_obj_slice_t *self = MP_OBJ_TO_PTR(self_in);
  86. *start = self->start;
  87. *stop = self->stop;
  88. *step = self->step;
  89. }
  90. #endif