Skip to content

Conversation

@dpgeorge
Copy link
Member

@dpgeorge dpgeorge commented Jun 21, 2020

Split off frm #6130, this PR adds support for passing in an OrderedDict for the locals of a new class, using type(name, bases, locals), where locals can now be an OrderedDict.

The changes here introduce a new helper mp_obj_is_dict_or_ordereddict() and update existing code to use it (which decreases code size in extmod/moductypes.c). The actual feature of using OrderedDict as class locals costs 0 bytes.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jun 21, 2020
#define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_type(o, &mp_type_int))
#define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_type(o, &mp_type_str))
#define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op))
#define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->make_new == mp_obj_dict_make_new)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice optimisation saving the extra conditional check!

@andrewleech
Copy link
Contributor

Thanks for separating / re-implementing this so cleanly, looks great to me.

dpgeorge added 3 commits June 24, 2020 12:05
And use it in py/objdict.c instead of mp_obj_is_dict_type.

Signed-off-by: Damien George <[email protected]>
An OrderedDict can now be used for the locals when creating a type
explicitly via type(name, bases, locals).

Signed-off-by: Damien George <[email protected]>
@dpgeorge dpgeorge force-pushed the py-objtype-ordereddict branch from 32d7fe7 to 457fdf6 Compare June 24, 2020 02:06
@dpgeorge dpgeorge merged commit 457fdf6 into micropython:master Jun 24, 2020
@dpgeorge dpgeorge deleted the py-objtype-ordereddict branch June 24, 2020 02:21
tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Mar 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants