From 1b9ecabf8b632912f3dd2230687df431a73fb04a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 2 Oct 2023 09:08:03 -0500 Subject: [PATCH] Fix constructing empty namedtuple this change from micropython was not taken with the merge --- py/objnamedtuple.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index e1e84c8242..ed2b1de42e 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -115,9 +115,10 @@ mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t #endif } - // Create a tuple and set the type to this namedtuple - mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_fields, NULL)); - tuple->base.type = type_in; + // Create a namedtuple with explicit malloc. Calling mp_obj_new_tuple + // with num_fields=0 returns a read-only object. + mp_obj_tuple_t *tuple = mp_obj_malloc_var(mp_obj_tuple_t, mp_obj_t, num_fields, type_in); + tuple->len = num_fields; // Copy the positional args into the first slots of the namedtuple memcpy(&tuple->items[0], args, sizeof(mp_obj_t) * n_args);