Skip to content

Commit ca6d75f

Browse files
committed
py: Small simplifications in tuple and list accessors.
1 parent 4abff75 commit ca6d75f

File tree

4 files changed

+19
-34
lines changed

4 files changed

+19
-34
lines changed

py/obj.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,10 @@ void mp_obj_get_array(mp_obj_t o, mp_uint_t *len, mp_obj_t **items) {
319319
}
320320

321321
void mp_obj_get_array_fixed_n(mp_obj_t o, mp_uint_t len, mp_obj_t **items) {
322-
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple) || MP_OBJ_IS_TYPE(o, &mp_type_list)) {
323-
mp_uint_t seq_len;
324-
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
325-
mp_obj_tuple_get(o, &seq_len, items);
326-
} else {
327-
mp_obj_list_get(o, &seq_len, items);
328-
}
329-
if (seq_len != len) {
330-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "requested length %d but object has length %d", len, seq_len));
331-
}
332-
} else {
333-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
322+
mp_uint_t seq_len;
323+
mp_obj_get_array(o, &seq_len, items);
324+
if (seq_len != len) {
325+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "requested length %d but object has length %d", len, seq_len));
334326
}
335327
}
336328

py/objtuple.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,8 @@ mp_obj_t mp_obj_new_tuple(mp_uint_t n, const mp_obj_t *items) {
241241
void mp_obj_tuple_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items) {
242242
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
243243
mp_obj_tuple_t *self = self_in;
244-
if (len) {
245-
*len = self->len;
246-
}
247-
if (items) {
248-
*items = &self->items[0];
249-
}
244+
*len = self->len;
245+
*items = &self->items[0];
250246
}
251247

252248
void mp_obj_tuple_del(mp_obj_t self_in) {

py/objzip.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
#include "misc.h"
3232
#include "qstr.h"
3333
#include "obj.h"
34+
#include "objtuple.h"
3435
#include "runtime.h"
3536

3637
typedef struct _mp_obj_zip_t {
3738
mp_obj_base_t base;
38-
int n_iters;
39+
mp_uint_t n_iters;
3940
mp_obj_t iters[];
4041
} mp_obj_zip_t;
4142

@@ -45,7 +46,7 @@ STATIC mp_obj_t zip_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
4546
mp_obj_zip_t *o = m_new_obj_var(mp_obj_zip_t, mp_obj_t, n_args);
4647
o->base.type = &mp_type_zip;
4748
o->n_iters = n_args;
48-
for (int i = 0; i < n_args; i++) {
49+
for (mp_uint_t i = 0; i < n_args; i++) {
4950
o->iters[i] = mp_getiter(args[i]);
5051
}
5152
return o;
@@ -54,22 +55,20 @@ STATIC mp_obj_t zip_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
5455
STATIC mp_obj_t zip_iternext(mp_obj_t self_in) {
5556
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_zip));
5657
mp_obj_zip_t *self = self_in;
57-
mp_obj_t *items;
5858
if (self->n_iters == 0) {
5959
return MP_OBJ_STOP_ITERATION;
6060
}
61-
mp_obj_t o = mp_obj_new_tuple(self->n_iters, NULL);
62-
mp_obj_tuple_get(o, NULL, &items);
61+
mp_obj_tuple_t *tuple = mp_obj_new_tuple(self->n_iters, NULL);
6362

64-
for (int i = 0; i < self->n_iters; i++) {
63+
for (mp_uint_t i = 0; i < self->n_iters; i++) {
6564
mp_obj_t next = mp_iternext(self->iters[i]);
6665
if (next == MP_OBJ_STOP_ITERATION) {
67-
mp_obj_tuple_del(o);
66+
mp_obj_tuple_del(tuple);
6867
return MP_OBJ_STOP_ITERATION;
6968
}
70-
items[i] = next;
69+
tuple->items[i] = next;
7170
}
72-
return o;
71+
return tuple;
7372
}
7473

7574
const mp_obj_type_t mp_type_zip = {

py/runtime.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "qstr.h"
3535
#include "obj.h"
3636
#include "objtuple.h"
37+
#include "objlist.h"
3738
#include "objmodule.h"
3839
#include "parsenum.h"
3940
#include "runtime0.h"
@@ -769,21 +770,18 @@ void mp_unpack_ex(mp_obj_t seq_in, mp_uint_t num_in, mp_obj_t *items) {
769770
}
770771
items[num_left + num_right + 1 - 1 - seq_len] = item;
771772
}
772-
mp_obj_t rest = mp_obj_new_list(0, NULL);
773+
mp_obj_list_t *rest = mp_obj_new_list(0, NULL);
773774
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
774775
mp_obj_list_append(rest, item);
775776
}
776-
mp_uint_t rest_len;
777-
mp_obj_t *rest_items;
778-
mp_obj_list_get(rest, &rest_len, &rest_items);
779-
if (rest_len < num_right) {
777+
if (rest->len < num_right) {
780778
goto too_short;
781779
}
782780
items[num_right] = rest;
783781
for (mp_uint_t i = 0; i < num_right; i++) {
784-
items[num_right - 1 - i] = rest_items[rest_len - num_right + i];
782+
items[num_right - 1 - i] = rest->items[rest->len - num_right + i];
785783
}
786-
mp_obj_list_set_len(rest, rest_len - num_right);
784+
mp_obj_list_set_len(rest, rest->len - num_right);
787785
}
788786
return;
789787

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy