aboutsummaryrefslogtreecommitdiff
path: root/py/objdict.c
AgeCommit message (Collapse)Author
2022-05-03all: Use mp_obj_malloc everywhere it's applicable.Jim Mussared
This replaces occurences of foo_t *foo = m_new_obj(foo_t); foo->base.type = &foo_type; with foo_t *foo = mp_obj_malloc(foo_t, &foo_type); Excludes any places where base is a sub-field or when new0/memset is used. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-07extmod/modujson: Add support for dump/dumps separators keyword-argument.Peter Züger
Optionally enabled via MICROPY_PY_UJSON_SEPARATORS. Enabled by default. For dump, make sure mp_get_stream_raise is called after mod_ujson_separators since CPython does it in this order (if both separators and stream are invalid, separators will raise an exception first). Add separators argument in the docs as well. Signed-off-by: Peter Züger <zueger.peter@icloud.com> Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py: Introduce and use mp_raise_type_arg helper.Damien George
To reduce code size. Signed-off-by: Damien George <damien@micropython.org>
2021-03-26py: Rename remaining object types to be of the form mp_type_xxx.Damien George
For consistency with all other object types in the core. Signed-off-by: Damien George <damien@micropython.org>
2020-10-10py/objdict: Add mp_const_empty_dict_obj, use it for mp_const_empty_map.Jim Mussared
2020-06-24py/obj.h: Add public mp_obj_is_dict_or_ordereddict() helper macro.Damien George
And use it in py/objdict.c instead of mp_obj_is_dict_type. Signed-off-by: Damien George <damien@micropython.org>
2020-06-10py/objtype: Add __dict__ attribute for class objects.Andrew Leech
The behavior mirrors the instance object dict attribute where a copy of the local attributes are provided (unless the dict is read-only, then that dict itself is returned, as an optimisation). MicroPython does not support modifying this dict because the changes will not be reflected in the class. The feature is only enabled if MICROPY_CPYTHON_COMPAT is set, the same as the instance version.
2020-04-27py/objdict: Fix popitem for ordered dicts.Jim Mussared
The popitem method wasn't implemented for ordered dicts and would result in an invalid state. Fixes issue #5956.
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-28py: Removing dangling "else" to improve code format consistency.Damien George
2019-11-13py/objdict: Support ujson.dump() of OrderedDict objects.Andrew Leech
Following CPython, OrderedDict are dumped with the syntax of dict.
2019-07-30py/objdict: Quote non-string types when used as keys in JSON output.Eric Poulsen
JSON requires that keys of objects be strings. CPython will therefore automatically quote simple types (NoneType, bool, int, float) when they are used directly as keys in JSON output. To prevent subtle bugs and emit compliant JSON, MicroPython should at least test for such keys so they aren't silently let through. Then doing the actual quoting is a similar cost to raising an exception, so that's what is implemented by this patch. Fixes issue #4790.
2019-05-17various: Add and update my copyright line based on git history.Paul Sokolovsky
For modules I initially created or made substantial contributions to.
2019-02-12py: Downcase MP_xxx_SLOT_IS_FILLED inline functions.Damien George
2019-02-12py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API.Damien George
These macros could in principle be (inline) functions so it makes sense to have them lower case, to match the other C API functions. The remaining macros that are upper case are: - MP_OBJ_TO_PTR, MP_OBJ_FROM_PTR - MP_OBJ_NEW_SMALL_INT, MP_OBJ_SMALL_INT_VALUE - MP_OBJ_NEW_QSTR, MP_OBJ_QSTR_VALUE - MP_OBJ_FUN_MAKE_SIG - MP_DECLARE_CONST_xxx - MP_DEFINE_CONST_xxx These must remain macros because they are used when defining const data (at least, MP_OBJ_NEW_SMALL_INT is so it makes sense to have MP_OBJ_SMALL_INT_VALUE also a macro). For those macros that have been made lower case, compatibility macros are provided for the old names so that users do not need to change their code immediately.
2018-12-13py/objdict: Make .fromkeys() method configurable.Paul Sokolovsky
On by default, turned off for minimal/bare-arm. Saves 144 bytes on x86.
2018-09-27py/objdict: Reword TODO about inlining mp_obj_dict_get to a note.Damien George
2018-07-08py/objdict: Make mp_obj_dict_get_map an inline function.Damien George
It's a very simple function and saves code, and improves efficiency, by being inline. Note that this is an auxiliary helper function and so doesn't need mp_check_self -- that's used for functions that can be accessed directly from Python code (eg from a method table).
2018-02-18py/objdict: Disallow possible modifications to fixed dicts.Mike Wadsten
2017-11-27py/objdict: Reuse dict-view key iterator for standard dict iterator.Damien George
It has equivalent behaviour and reusing it saves some code bytes.
2017-11-24py/runtime: Add MP_BINARY_OP_CONTAINS as reverse of MP_BINARY_OP_IN.Damien George
Before this patch MP_BINARY_OP_IN had two meanings: coming from bytecode it meant that the args needed to be swapped, but coming from within the runtime meant that the args were already in the correct order. This lead to some confusion in the code and comments stating how args were reversed. It also lead to 2 bugs: 1) containment for a subclass of a native type didn't work; 2) the expression "{True} in True" would illegally succeed and return True. In both of these cases it was because the args to MP_BINARY_OP_IN ended up being reversed twice. To fix these things this patch introduces MP_BINARY_OP_CONTAINS which corresponds exactly to the __contains__ special method, and this is the operator that built-in types should implement. MP_BINARY_OP_IN is now only emitted by the compiler and is converted to MP_BINARY_OP_CONTAINS by swapping the arguments.
2017-10-04all: Remove inclusion of internal py header files.Damien George
Header files that are considered internal to the py core and should not normally be included directly are: py/nlr.h - internal nlr configuration and declarations py/bc0.h - contains bytecode macro definitions py/runtime0.h - contains basic runtime enums Instead, the top-level header files to include are one of: py/obj.h - includes runtime0.h and defines everything to use the mp_obj_t type py/runtime.h - includes mpstate.h and hence nlr.h, obj.h, runtime0.h, and defines everything to use the general runtime support functions Additional, specific headers (eg py/objlist.h) can be included if needed.
2017-08-29all: Convert mp_uint_t to mp_unary_op_t/mp_binary_op_t where appropriateDamien George
The unary-op/binary-op enums are already defined, and there are no arithmetic tricks used with these types, so it makes sense to use the correct enum type for arguments that take these values. It also reduces code size quite a bit for nan-boxing builds.
2017-08-11py/modsys: Initial implementation of sys.getsizeof().Paul Sokolovsky
Implemented as a new MP_UNARY_OP. This patch adds support lists, dicts and instances.
2017-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2017-07-04py/objdict: Factorise dict accessor helper to reduce code size.Damien George
Code size change in bytes for this patch is: bare-arm: -72 minimal x86: -48 unix x64: -32 unix nanbox: -120 stmhal: -68 cc3200: -64 esp8266: -56
2017-04-12py: Optimise types for common case where type has a single parent type.Damien George
The common cases for inheritance are 0 or 1 parent types, for both built-in types (eg built-in exceptions) as well as user defined types. So it makes sense to optimise the case of 1 parent type by storing just the type and not a tuple of 1 value (that value being the single parent type). This patch makes such an optimisation. Even though there is a bit more code to handle the two cases (either a single type or a tuple with 2 or more values) it helps reduce overall code size because it eliminates the need to create a static tuple to hold single parents (eg for the built-in exceptions). It also helps reduce RAM usage for user defined types that only derive from a single parent. Changes in code size (in bytes) due to this patch: bare-arm: -16 minimal (x86): -176 unix (x86-64): -320 unix nanbox: -384 stmhal: -64 cc3200: -32 esp8266: -108
2017-03-29py: Shorten a couple of error messages.Damien George
2017-03-28py: Use mp_raise_TypeError/mp_raise_ValueError helpers where possible.Damien George
Saves 168 bytes on bare-arm.
2017-02-16py: De-optimise some uses of mp_getiter, so they don't use the C stack.Damien George
In these cases the heap is anyway used to create a new object so no real need to use the C stack for iterating. It saves a few bytes of code size.
2017-02-16py: Add iter_buf to getiter type method.Damien George
Allows to iterate over the following without allocating on the heap: - tuple - list - string, bytes - bytearray, array - dict (not dict.keys, dict.values, dict.items) - set, frozenset Allows to call the following without heap memory: - all, any, min, max, sum TODO: still need to allocate stack memory in bytecode for iter_buf.
2017-02-16py/objdict: Convert mp_uint_t to size_t where appropriate.Damien George
2016-10-17py: Use mp_raise_msg helper function where appropriate.Damien George
Saves the following number of bytes of code space: 176 for bare-arm, 352 for minimal, 272 for unix x86-64, 140 for stmhal, 120 for esp8266.
2016-10-17py/objdict: Actually provide the key that failed in KeyError exception.Damien George
The failed key is available as exc.args[0], as per CPython.
2016-10-17py/objdict: Fix optimisation for allocating result in fromkeys.Damien George
Iterables don't respond to __len__, so call __len__ on the original argument.
2016-08-12py/objdict: Get rid of asserts (remove/replace with mp_check_self()).Paul Sokolovsky
2016-06-12py/objdict: Implemented OrderedDict equality check.Mark Anthony Palomer
2016-05-20py: Declare constant data as properly constant.Damien George
Otherwise some compilers (eg without optimisation) will put this read-only data in RAM instead of ROM.
2016-01-11py: Change first arg of type.make_new from mp_obj_t to mp_obj_type_t*.Damien George
The first argument to the type.make_new method is naturally a uPy type, and all uses of this argument cast it directly to a pointer to a type structure. So it makes sense to just have it a pointer to a type from the very beginning (and a const pointer at that). This patch makes such a change, and removes all unnecessary casting to/from mp_obj_t.
2016-01-11py: Change type signature of builtin funs that take variable or kw args.Damien George
With this patch the n_args parameter is changed type from mp_uint_t to size_t.
2016-01-11py: Change type of .make_new and .call args: mp_uint_t becomes size_t.Damien George
This patch changes the type signature of .make_new and .call object method slots to use size_t for n_args and n_kw (was mp_uint_t. Makes code more efficient when mp_uint_t is larger than a machine word. Doesn't affect ports when size_t and mp_uint_t have the same size.
2016-01-03py: Use polymorphic iterator type where possible to reduce code size.Damien George
Only types whose iterator instances still fit in 4 machine words have been changed to use the polymorphic iterator. Reduces Thumb2 arch code size by 264 bytes.
2015-11-29py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.Damien George
This allows the mp_obj_t type to be configured to something other than a pointer-sized primitive type. This patch also includes additional changes to allow the code to compile when sizeof(mp_uint_t) != sizeof(void*), such as using size_t instead of mp_uint_t, and various casts.
2015-11-29py: Add MP_ROM_* macros and mp_rom_* types and use them.Damien George
2015-11-20py: Use MP_OBJ_NULL instead of NULL when appropriate.Damien George
2015-10-11py: Rename MP_BOOL() to mp_obj_new_bool() for consistency in naming.Paul Sokolovsky
2015-04-16py: Add %q format support to mp_[v]printf, and use it.Damien George
2015-04-16py: Overhaul and simplify printf/pfenv mechanism.Damien George
Previous to this patch the printing mechanism was a bit of a tangled mess. This patch attempts to consolidate printing into one interface. All (non-debug) printing now uses the mp_print* family of functions, mainly mp_printf. All these functions take an mp_print_t structure as their first argument, and this structure defines the printing backend through the "print_strn" function of said structure. Printing from the uPy core can reach the platform-defined print code via two paths: either through mp_sys_stdout_obj (defined pert port) in conjunction with mp_stream_write; or through the mp_plat_print structure which uses the MP_PLAT_PRINT_STRN macro to define how string are printed on the platform. The former is only used when MICROPY_PY_IO is defined. With this new scheme printing is generally more efficient (less layers to go through, less arguments to pass), and, given an mp_print_t* structure, one can call mp_print_str for efficiency instead of mp_printf("%s", ...). Code size is also reduced by around 200 bytes on Thumb2 archs.
2015-03-26objdict: Cast mp_obj_t to concrete types explicitly.Paul Sokolovsky
Continuation of refactoring applied previously to objlist.