@@ -62,13 +62,20 @@ const mp_obj_ellipsis_t mp_const_ellipsis_obj = {{&mp_type_ellipsis}};
6262// CPython supports 3rd arg (step), plus args can be arbitrary Python objects.
6363typedef struct _mp_obj_slice_t {
6464 mp_obj_base_t base ;
65- machine_int_t start ;
66- machine_int_t stop ;
65+ mp_obj_t start ;
66+ mp_obj_t stop ;
67+ mp_obj_t step ;
6768} mp_obj_slice_t ;
6869
6970void slice_print (void (* print )(void * env , const char * fmt , ...), void * env , mp_obj_t o_in , mp_print_kind_t kind ) {
7071 mp_obj_slice_t * o = o_in ;
71- print (env , "slice(" INT_FMT ", " INT_FMT ")" , o -> start , o -> stop );
72+ print (env , "slice(" );
73+ mp_obj_print_helper (print , env , o -> start , PRINT_REPR );
74+ print (env , ", " );
75+ mp_obj_print_helper (print , env , o -> stop , PRINT_REPR );
76+ print (env , ", " );
77+ mp_obj_print_helper (print , env , o -> step , PRINT_REPR );
78+ print (env , ")" );
7279}
7380
7481const mp_obj_type_t mp_type_slice = {
@@ -77,39 +84,21 @@ const mp_obj_type_t mp_type_slice = {
7784 .print = slice_print ,
7885};
7986
80- // TODO: Make sure to handle "empty" values, which are signified by None in CPython
8187mp_obj_t mp_obj_new_slice (mp_obj_t ostart , mp_obj_t ostop , mp_obj_t ostep ) {
82- assert (ostep == NULL );
83- machine_int_t start = 0 , stop = 0 ;
84- if (ostart != mp_const_none ) {
85- start = mp_obj_get_int (ostart );
86- }
87- if (ostop != mp_const_none ) {
88- stop = mp_obj_get_int (ostop );
89- if (stop == 0 ) {
90- // [x:0] is a special case - in our slice object, stop = 0 means
91- // "end of sequence". Fortunately, [x:0] is an empty seqence for
92- // any x (including negative). [x:x] is also always empty sequence.
93- // but x also can be 0. But note that b""[x:x] is b"" for any x (i.e.
94- // no IndexError, at least in Python 3.3.3). So, we just use -1's to
95- // signify that. -1 is catchy "special" number in case someone will
96- // try to print [x:0] slice ever.
97- start = stop = -1 ;
98- }
99- }
100- mp_obj_slice_t * o = m_new (mp_obj_slice_t , 1 );
88+ mp_obj_slice_t * o = m_new_obj (mp_obj_slice_t );
10189 o -> base .type = & mp_type_slice ;
102- o -> start = start ;
103- o -> stop = stop ;
104- return (mp_obj_t )o ;
90+ o -> start = ostart ;
91+ o -> stop = ostop ;
92+ o -> step = ostep ;
93+ return o ;
10594}
10695
107- void mp_obj_slice_get (mp_obj_t self_in , machine_int_t * start , machine_int_t * stop , machine_int_t * step ) {
96+ void mp_obj_slice_get (mp_obj_t self_in , mp_obj_t * start , mp_obj_t * stop , mp_obj_t * step ) {
10897 assert (MP_OBJ_IS_TYPE (self_in , & mp_type_slice ));
10998 mp_obj_slice_t * self = self_in ;
11099 * start = self -> start ;
111100 * stop = self -> stop ;
112- * step = 1 ;
101+ * step = self -> step ;
113102}
114103
115104#endif
0 commit comments