summaryrefslogtreecommitdiffstatshomepage
path: root/py/modbuiltins.c
Commit message (Collapse)AuthorAge
* all: Remove the "STATIC" macro and just use "static" instead.Angus Gratton2024-03-07
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* py/modbuiltins: Share vstr_add_char's implementation of utf8 encoding.Jeff Epler2023-11-28
| | | | | | This saves ~84 bytes on trinket m0, and saves 112 bytes on PYBV10. Signed-off-by: Jeff Epler <jepler@gmail.com>
* all: Fix spelling mistakes based on codespell check.Damien George2023-04-27
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/obj: Convert make_new into a mp_obj_type_t slot.Jim Mussared2022-09-19
| | | | | | | | | | | Instead of being an explicit field, it's now a slot like all the other methods. This is a marginal code size improvement because most types have a make_new (100/138 on PYBV11), however it improves consistency in how types are declared, removing the special case for make_new. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/obj: Add accessors for type slots and use everywhere.Jim Mussared2022-09-19
| | | | | | | This is a no-op, but sets the stage for changing the mp_obj_type_t representation. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/objstr: Optimise mp_obj_new_str_from_vstr for known-safe strings.Jim Mussared2022-08-26
| | | | | | | The new `mp_obj_new_str_from_utf8_vstr` can be used when you know you already have a unicode-safe string. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/objstr: Split mp_obj_str_from_vstr into bytes/str versions.Jim Mussared2022-08-26
| | | | | | | | | | | | | | 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>
* all: Remove third argument to MP_REGISTER_MODULE.Damien George2022-06-02
| | | | | | | | 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>
* py/builtin: Clean up and simplify import_stat and builtin_open config.Damien George2022-05-25
| | | | | | | | | | | | | | The following changes are made: - If MICROPY_VFS is enabled then mp_vfs_import_stat and mp_vfs_open are automatically used for mp_import_stat and mp_builtin_open respectively. - If MICROPY_PY_IO is enabled then "open" is automatically included in the set of builtins, and points to mp_builtin_open_obj. This helps to clean up and simplify the most common port configuration. Signed-off-by: Damien George <damien@micropython.org>
* py: Make builtin modules use MP_REGISTER_MODULE.Jim Mussared2022-05-18
| | | | Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/modbuiltins: Add additional macro for extending builtins.stijn2022-01-07
| | | | Mainly useful for defining additional globals in boards and variants.
* py: Support single argument to optimised MP_OBJ_STOP_ITERATION.Damien George2021-07-15
| | | | | | | | | | | | | The MP_OBJ_STOP_ITERATION optimisation is a shortcut for creating a StopIteration() exception object, and means that heap memory does not need to be allocated for the exception (in cases where it can be used). This commit allows this optimised object to take an optional argument (before, it could only have no argument). The commit also adds some new tests to cover corner cases with StopIteration and generators that previously did not work. Signed-off-by: Damien George <damien@micropython.org>
* all: Update to point to files in new shared/ directory.Damien George2021-07-12
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py: Add option to compile without any error messages at all.Damien George2021-04-27
| | | | | | | | This introduces a new option, MICROPY_ERROR_REPORTING_NONE, which completely disables all error messages. To be used in cases where MicroPython needs to fit in very limited systems. Signed-off-by: Damien George <damien@micropython.org>
* py/modbuiltins: Fix getattr to work with class raising AttributeError.Damien George2020-06-02
| | | | Fixes issue #6089.
* all: Fix implicit floating point to integer conversions.stijn2020-04-18
| | | | These are found when building with -Wfloat-conversion.
* all: Use MP_ERROR_TEXT for all error messages.Jim Mussared2020-04-05
|
* py: Use preprocessor to detect error reporting level (terse/detailed).Jim Mussared2020-04-05
| | | | | | Instead of compiler-level if-logic. This is necessary to know what error strings are included in the build at the preprocessor stage, so that string compression can be implemented.
* all: Convert exceptions to use mp_raise_XXX helpers in remaining places.Damien George2020-03-18
|
* all: Reformat C and Python source code with tools/codeformat.py.Damien George2020-02-28
| | | | This is run with uncrustify 0.70.1, and black 19.10b0.
* py: Add mp_raise_msg_varg helper and use it where appropriate.Damien George2020-02-13
| | | | | | | | | | | | | | | | | | This commit adds mp_raise_msg_varg(type, fmt, ...) as a helper for nlr_raise(mp_obj_new_exception_msg_varg(type, fmt, ...)). It makes the C-level API for raising exceptions more consistent, and reduces code size on most ports: bare-arm: +28 +0.042% minimal x86: +100 +0.067% unix x64: -56 -0.011% unix nanbox: -300 -0.068% stm32: -204 -0.054% PYBV10 cc3200: +0 +0.000% esp8266: -64 -0.010% GENERIC esp32: -104 -0.007% GENERIC nrf: -136 -0.094% pca10040 samd: +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
* py: Add mp_raise_type helper macro and use it where appropriate.Damien George2020-02-13
| | | | | | This provides a more consistent C-level API to raise exceptions, ie moving away from nlr_raise towards mp_raise_XXX. It also reduces code size by a small amount on some ports.
* py: Clean up commented-out code and comments about exception hierarchy.Damien George2019-12-28
| | | | | | | | In CPython, EnvironmentError and IOError are now aliases of OSError so no need to have them listed in the code. OverflowError inherits from ArithmeticError because it's intended to be raised "when the result of an arithmetic operation is too large to be represented" (per CPython docs), and MicroPython aims to match the CPython exception hierarchy.
* py: Downcase MP_xxx_SLOT_IS_FILLED inline functions.Damien George2019-02-12
|
* py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API.Damien George2019-02-12
| | | | | | | | | | | | | | | | | | | | | 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.
* py: Add optional support for 2-argument version of built-in next().stijn2019-01-27
| | | | Configurable via MICROPY_PY_BUILTINS_NEXT2, disabled by default.
* py/modbuiltins: Make oct/hex work when !MICROPY_PY_BUILTINS_STR_OP_MODULOPaul Sokolovsky2018-09-20
| | | | Instead of redirecting to str.__mod__(), use str.format() in this case.
* py: Add checks for stream objects in print() and sys.print_exception().Damien George2018-06-20
|
* py/modbuiltins: Add support for rounding integers.Jan Klusacek2018-05-22
| | | | | As per CPython semantics. This feature is controlled by MICROPY_PY_BUILTINS_ROUND_INT which is disabled by default.
* py/modbuiltins: Make built-in dir support the __dir__ special method.Damien George2018-05-10
| | | | | If MICROPY_PY_ALL_SPECIAL_METHODS is enabled then dir() will now delegate to the special method __dir__ if the object it is listing has this method.
* py/modbuiltins: In built-in dir make use of mp_load_method_protected.Damien George2018-05-10
| | | | | | | This gives dir() better behaviour when listing the attributes of a user type that defines __getattr__: it will now not list those attributes for which __getattr__ raises AttributeError (meaning the attribute is not supported by the object).
* py/modbuiltins: Make built-in hasattr work properly for user types.Damien George2018-05-10
| | | | | It now allows __getattr__ in a user type to raise AttributeError when the attribute does not exist.
* py/{modbuiltins,repl}: Start qstr probing from after empty qstr.Damien George2018-05-09
| | | | | The list of qstrs starts with MP_QSTR_NULL followed by MP_QSTR_, and these should never appear in dir() or REPL tab completion, so skip them.
* py/modbuiltins: Simplify and generalise dir() by probing qstrs.Damien George2018-02-19
| | | | | | | | | | | | | | | | | | | | | | | | This patch improves the builtin dir() function by probing the target object with all possible qstrs via mp_load_method_maybe. This is very simple (in terms of implementation), doesn't require recursion, and allows to list all methods of user-defined classes (without duplicates) even if they have multiple inheritance with a common parent. The downside is that it can be slow because it has to iterate through all the qstrs in the system, but the "dir()" function is anyway mostly used for testing frameworks and user introspection of types, so speed is not considered a priority. In addition to providing a more complete implementation of dir(), this patch is simpler than the previous implementation and saves some code space: bare-arm: -80 minimal x86: -80 unix x64: -56 unix nanbox: -48 stm32: -80 cc3200: -80 esp8266: -104 esp32: -64
* py/modbuiltins: Simplify casts from char to byte ptr in builtin ord.Damien George2018-02-14
|
* py/unicode: Clean up utf8 funcs and provide non-utf8 inline versions.Damien George2018-02-14
| | | | | | | | | | This patch provides inline versions of the utf8 helper functions for the case when unicode is disabled (MICROPY_PY_BUILTINS_STR_UNICODE set to 0). This saves code size. The unichar_charlen function is also renamed to utf8_charlen to match the other utf8 helper functions, and the signature of this function is adjusted for consistency (const char* -> const byte*, mp_uint_t -> size_t).
* py/modbuiltins: For builtin_chr, use uint8_t instead of char for array.Damien George2018-02-07
| | | | | | The array should be of type unsigned byte because that is the type of the values being stored. And changing to uint8_t helps to prevent warnings from some static analysers.
* py/modbuiltins: Use standard arg-parsing helper func for builtin print.Damien George2017-12-05
| | | | | | | | | | | | | | | | This allows the function to raise an exception when unknown keyword args are passed in. This patch also reduces code size by (in bytes): bare-arm: -24 minimal x86: -76 unix x64: -56 unix nanbox: -84 stm32: -40 esp8266: -68 cc3200: -48 Furthermore, this patch adds space (" ") to the set of ROM qstrs which means it doesn't need to be put in RAM if it's ever used.
* py/modbuiltins: Slightly simplify code in builtin round().Damien George2017-11-22
|
* py/objstr: Remove "make_qstr_if_not_already" arg from mp_obj_new_str.Damien George2017-11-16
| | | | | | | | | | | This patch simplifies the str creation API to favour the common case of creating a str object that is not forced to be interned. To force interning of a new str the new mp_obj_new_str_via_qstr function is added, and should only be used if warranted. Apart from simplifying the mp_obj_new_str function (and making it have the same signature as mp_obj_new_bytes), this patch also reduces code size by a bit (-16 bytes for bare-arm and roughly -40 bytes on the bare-metal archs).
* py/modbuiltins: Use existing utf8_get_char helper in builtin ord func.Damien George2017-10-11
|
* all: Remove inclusion of internal py header files.Damien George2017-10-04
| | | | | | | | | | | | | | | | 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.
* py/modbuiltins: Implement abs() by dispatching to MP_UNARY_OP_ABS.Paul Sokolovsky2017-09-18
| | | | | | | | | | This allows user classes to implement __abs__ special method, and saves code size (104 bytes for x86_64), even though during refactor, an issue was fixed and few optimizations were made: * abs() of minimum (negative) small int value is calculated properly. * objint_longlong and objint_mpz avoid allocating new object is the argument is already non-negative.
* py,extmod,stmhal: Use "static inline" for funcs that should be inline.Damien George2017-08-02
| | | | | "STATIC inline" can expand to "inline" if STATIC is defined to nothing, and this case can lead to link errors.
* all: Use the name MicroPython consistently in commentsAlexander Steffen2017-07-31
| | | | | There were several different spellings of MicroPython present in comments, when there should be only one.
* py,extmod: Some casts and minor refactors to quiet compiler warnings.Tom Collins2017-07-07
|
* py: Change mp_uint_t to size_t in builtins code.Damien George2017-07-04
|
* all: Make more use of mp_raise_{msg,TypeError,ValueError} helpers.Damien George2017-06-15
|
* py/modbuiltins: Add core-provided version of input() function.Damien George2017-06-01
| | | | | | | The implementation is taken from stmhal/input.c, with code added to handle ctrl-C. This built-in is controlled by MICROPY_PY_BUILTINS_INPUT and is disabled by default. It uses readline() to capture input but this can be overridden by defining the mp_hal_readline macro.
* py/modsys: update conditionals for code referencing sys.stdoutTom Collins2017-05-14
| | | | Working on a build with PY_IO enabled (for PY_UJSON support) but PY_SYS_STDFILES disabled (no filesystem). There are multiple references to mp_sys_stdout_obj that should only be enabled if both PY_IO and PY_SYS_STDFILES are enabled.