summaryrefslogtreecommitdiff
path: root/py/modstruct.c
AgeCommit message (Collapse)Author
2024-03-07all: Remove the "STATIC" macro and just use "static" instead.Angus Gratton
The STATIC macro was introduced a very long time ago in commit d5df6cd44a433d6253a61cb0f987835fbc06b2de. The original reason for this was to have the option to define it to nothing so that all static functions become global functions and therefore visible to certain debug tools, so one could do function size comparison and other things. This STATIC feature is rarely (if ever) used. And with the use of LTO and heavy inline optimisation, analysing the size of individual functions when they are not static is not a good representation of the size of code when fully optimised. So the macro does not have much use and it's simpler to just remove it. Then you know exactly what it's doing. For example, newcomers don't have to learn what the STATIC macro is and why it exists. Reading the code is also less "loud" with a lowercase static. One other minor point in favour of removing it, is that it stops bugs with `STATIC inline`, which should always be `static inline`. Methodology for this commit was: 1) git ls-files | egrep '\.[ch]$' | \ xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/" 2) Do some manual cleanup in the diff by searching for the word STATIC in comments and changing those back. 3) "git-grep STATIC docs/", manually fixed those cases. 4) "rg -t python STATIC", manually fixed codegen lines that used STATIC. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2023-09-01py/modstruct: Support pad bytes in struct format.Daniël van de Giessen
This adds support for the x format code in struct.pack and struct.unpack. The primary use case for this is ignoring bytes while unpacking. When interfacing with existing systems, it may often happen that you either have fields in a struct that aren't properly specified or you simply don't care about them. Being able to easily skip them is useful. Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2023-06-08all: Use MP_REGISTER_EXTENSIBLE_MODULE for overrideable built-ins.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-08all: Rename mp_umodule*, mp_module_umodule* to remove the "u" prefix.Jim Mussared
This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-08all: Rename MP_QSTR_umodule to MP_QSTR_module everywhere.Jim Mussared
This renames the builtin-modules, such that help('modules') and printing the module object will show "module" rather than "umodule". This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-08-26py/objstr: Split mp_obj_str_from_vstr into bytes/str versions.Jim Mussared
Previously the desired output type was specified. Now make the type part of the function name. Because this function is used in a few places this saves code size due to smaller call-site. This makes `mp_obj_new_str_type_from_vstr` a private function of objstr.c (which is almost the only place where the output type isn't a compile-time constant). This saves ~140 bytes on PYBV11. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-06-02all: Remove third argument to MP_REGISTER_MODULE.Damien George
It's no longer needed because this macro is now processed after preprocessing the source code via cpp (in the qstr extraction stage), which means unused MP_REGISTER_MODULE's are filtered out by the preprocessor. Signed-off-by: Damien George <damien@micropython.org>
2022-05-18py: Make builtin modules use MP_REGISTER_MODULE.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
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.
2019-09-02py/binary: Change mp_uint_t to size_t for index, size, align args.Damien George
Reduces code size for nan-box builds, otherwise changes nothing.
2019-09-02py/modstruct: Fix struct.pack_into with unaligned offset of native type.Damien George
Following the same fix for unpack.
2019-09-02py/modstruct: Fix struct.unpack with unaligned offset of native type.Tom McDermott
With this patch alignment is done relative to the start of the buffer that is being unpacked, not the raw pointer value, as per CPython. Fixes issue #3314.
2017-09-01py/modstruct: Check and prevent buffer-write overflow in struct packing.Damien George
Prior to this patch, the size of the buffer given to pack_into() was checked for being too small by using the count of the arguments, not their actual size. For example, a format spec of '4I' would only check that there was 4 bytes available, not 16; and 'I' would check for 1 byte, not 4. The pack() function is ok because its buffer is created to be exactly the correct size. The fix in this patch calculates the total size of the format spec at the start of pack_into() and verifies that the buffer is large enough. This adds some computational overhead, to iterate through the whole format spec. The alternative is to check during the packing, but that requires extra code to handle alignment, and the check is anyway not needed for pack(). So to maintain minimal code size the check is done using struct_calcsize.
2017-09-01py/modstruct: Check and prevent buffer-read overflow in struct unpackingDamien George
Prior to this patch, the size of the buffer given to unpack/unpack_from was checked for being too small by using the count of the arguments, not their actual size. For example, a format spec of '4I' would only check that there was 4 bytes available, not 16; and 'I' would check for 1 byte, not 4. This bug is fixed in this patch by calculating the total size of the format spec at the start of the unpacking function. This function anyway needs to calculate the number of items at the start, so calculating the total size can be done at the same time.
2017-09-01py/modstruct: In struct.pack, stop converting if there are no args left.Damien George
This patch makes a repeat counter behave the same as repeating the typecode, when there are not enough args. For example: struct.pack('2I', 1) now behave the same as struct.pack('II', 1).
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-01-17py/binary: mp_binary_get_size: Raise error on unsupported typecodes.Paul Sokolovsky
Previouly, we had errors checked in callers, which led to duplicate code or missing checks in some places.
2016-10-07py/modstruct: Remove unreachable code, and add comment about CPy diff.Damien George
The deleted code is unreachable because calcsize_items guarantees that num_items corresponds to how many items there are in fmt to unpack.
2016-09-22all: Remove 'name' member from mp_obj_module_t struct.Damien George
One can instead lookup __name__ in the modules dict to get the value.
2016-08-26py/modstruct: Use more compact mp_raise_ValueError function.Damien George
Saves a few bytes of code size.
2016-05-28py/modstruct: Allow to have "0s" in struct format.Damien George
2016-05-14py/modstruct: Raise ValueError on unsupported format char.Paul Sokolovsky
2016-01-19py: Add ustruct.pack_into and unpack_fromDave Hylands
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.
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-10-31py/modstruct: Support repetition counters for all types, not just string.Paul Sokolovsky
This makes format specifiers ~ fully compatible with CPython. Adds 24 bytes for stmhal port (because previosuly we had to catch and report it's unsupported to user).
2015-07-05modstruct: Raise NotImplementedError for unsupported repeat specification.Paul Sokolovsky
2015-05-04modstruct: Rename module to "ustruct", to allow full Python-level impl.Paul Sokolovsky
2015-02-08py: Parse big-int/float/imag constants directly in parser.Damien George
Previous to this patch, a big-int, float or imag constant was interned (made into a qstr) and then parsed at runtime to create an object each time it was needed. This is wasteful in RAM and not efficient. Now, these constants are parsed straight away in the parser and turned into objects. This allows constants with large numbers of digits (so addresses issue #1103) and takes us a step closer to #722.
2015-01-21py: Remove mp_obj_str_builder and use vstr instead.Damien George
With this patch str/bytes construction is streamlined. Always use a vstr to build a str/bytes object. If the size is known beforehand then use vstr_init_len to allocate only required memory. Otherwise use vstr_init and the vstr will grow as needed. Then use mp_obj_new_str_from_vstr to create a str/bytes object using the vstr memory. Saves code ROM: 68 bytes on stmhal, 108 bytes on bare-arm, and 336 bytes on unix x64.
2015-01-01py: Move to guarded includes, everywhere in py/ core.Damien George
Addresses issue #1022.
2014-11-29Use MP_DEFINE_CONST_DICT macro to define module dicts.Damien George
This is just a clean-up of the code. Generated code is exactly the same.
2014-10-06py: Make mp_binary_set_val work on big endian machine.Damien George
2014-10-03py: Convert [u]int to mp_[u]int_t where appropriate.Damien George
Addressing issue #50.
2014-08-30py: Change uint to mp_uint_t in runtime.h, stackctrl.h, binary.h.Damien George
Part of code cleanup, working towards resolving issue #50.
2014-07-03Rename machine_(u)int_t to mp_(u)int_t.Damien George
See discussion in issue #50.
2014-06-21py: Include mpconfig.h before all other includes.Paul Sokolovsky
It defines types used by all other headers. Fixes #691.
2014-06-19Prefix ARRAY_SIZE with micropython prefix MP_Emmanuel Blot
2014-06-02modstruct: Add one more extension to typecodes - 'S', a pointer to C string.Paul Sokolovsky
Also, add comment with description of extension to CPython's typecodes.
2014-05-24Rename configuration variables controling Python features.Damien George
Now of the form MICROPY_PY_*. See issue #35.
2014-05-20modstruct: struct_calcsize: Fix case of uninitialized var.Paul Sokolovsky
2014-05-13py, unix: Add copyright for modules I worked closely on.Paul Sokolovsky
2014-05-12modstruct: Implement count specifier for strings (e.g. "100s").Paul Sokolovsky
Infra for counts of other types is there, need last mile to be implemented.
2014-05-11modstruct: Use MP_OBJ_FUN_ARGS_MAX instead of -1.v1.0.1Paul Sokolovsky
2014-05-03Add license header to (almost) all files.Damien George
Blanket wide to all .c and .h files. Some files originating from ST are difficult to deal with (license wise) so it was left out of those. Also merged modpyb.h, modos.h, modstm.h and modtime.h in stmhal/.
2014-04-26Add ARRAY_SIZE macro, and use it where possible.Damien George
2014-04-19modstruct: Initial implementation of struct.pack().Paul Sokolovsky
2014-04-19modstruct: Fix .calcsize() to account for struct type/alignment.Paul Sokolovsky