summaryrefslogtreecommitdiffstatshomepage
path: root/py/objexcept.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/mphal: Move configuration of ATOMIC_SECTION macros to mphal.h.Damien George2023-12-01
| | | | | | | | | | | | | MICROPY_BEGIN_ATOMIC_SECTION/MICROPY_END_ATOMIC_SECTION belong more to the MicroPython HAL rather than build configuration settings, so move their default configuration to py/mphal.h, and require all users of these macros to include py/mphal.h (here, py/objexcept.c and py/scheduler.c). This helps ports separate configuration from their HAL implementations, and can improve build times (because mpconfig.h is included everywhere, whereas mphal.h is not). Signed-off-by: Damien George <damien@micropython.org>
* py/misc: Change sizeof to offsetof for variable-length alloc.Jim Mussared2023-11-03
| | | | | | | | | | | | | | | | | | | | This fixes the case where e.g. struct foo_t { mp_obj_t x; uint16_t y; char buf[]; }; will have `sizeof(struct foo_t)==8`, but `offsetof(struct foo_t, buf)==6`. When computing the size to allocate for `m_new_obj_var` we need to use offsetof to avoid over-allocating. This is important especially when it might cause it to spill over into another GC block. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* all: Rename UMODULE to MODULE in preprocessor/Makefile vars.Jim Mussared2023-06-08
| | | | | | This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* all: Fix spelling mistakes based on codespell check.Damien George2023-04-27
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/obj: Optimise code size and performance for make_new as a slot.Jim Mussared2022-09-19
| | | | | | | The check for make_new (i.e. used to determine something's type) is now more complicated due to the slot access. This commit changes the inlining of a few frequently-used helpers to overall improve code size and performance.
* 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>
* all: Make all mp_obj_type_t defs use MP_DEFINE_CONST_OBJ_TYPE.Jim Mussared2022-09-19
| | | | | | In preparation for upcoming rework of mp_obj_type_t layout. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/obj: Add static safety checks to mp_obj_is_type().Yonatan Goldschmidt2022-07-18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Commit d96cfd13e3a464862c introduced a regression by breaking existing users of mp_obj_is_type(.., &mp_obj_bool). This function (and associated helpers like mp_obj_is_int()) have some specific nuances, and mistakes like this one can happen again. This commit adds mp_obj_is_exact_type() which behaves like the the old mp_obj_is_type(). The new mp_obj_is_type() has the same prototype but it attempts to statically assert that it's not called with types which should be checked using mp_obj_is_type(). If called with any of these types: int, str, bool, NoneType - it will cause a compilation error. Additional checked types (e.g function types) can be added in the future. Existing users of mp_obj_is_type() with the now "invalid" types, were translated to use mp_obj_is_exact_type(). The use of MP_STATIC_ASSERT() is not bulletproof - usually GCC (and other compilers) can't statically check conditions that are only known during link-time (like variables' addresses comparison). However, in this case, GCC is able to statically detect these conditions, probably because it's the exact same object - `&mp_type_int == &mp_type_int` is detected. Misuses of this function with runtime-chosen types (e.g: `mp_obj_type_t *x = ...; mp_obj_is_type(..., x);` won't be detected. MSC is unable to detect this, so we use MP_STATIC_ASSERT_NOT_MSC(). Compiling with this commit and without the fix for d96cfd13e3a464862c shows that it detects the problem. Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
* py/modsys: Add optional sys.tracebacklimit attribute.Damien George2022-03-10
| | | | | | With behaviour as per CPython. Signed-off-by: Damien George <damien@micropython.org>
* py/objexcept: Make mp_obj_new_exception_arg1 inline.Damien George2021-07-15
| | | | | | This function is rarely used so making it inline reduces code size. Signed-off-by: Damien George <damien@micropython.org>
* py/objexcept: Make mp_obj_exception_get_value support subclassed excs.Damien George2021-07-15
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/objexcept: Pretty print OSError also when it has 2 arguments.David Lechner2021-07-01
| | | | | | | This extends pretty-printing of OSError's to handle two arguments when the exception name is known. Signed-off-by: David Lechner <david@pybricks.com>
* 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/objexcept: Support errno attribute on OSError exceptions.Damien George2021-04-23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit adds the errno attribute to exceptions, so code can retrieve errno codes from an OSError using exc.errno. The implementation here simply lets `errno` (and the existing `value`) attributes work on any exception instance (they both alias args[0]). This is for efficiency and to keep code size down. The pros and cons of this are: Pros: - more compatible with CPython, less difference to document and learn - OSError().errno will correctly return None, whereas the current way of doing it via OSError().args[0] will raise an IndexError - it reduces code size on most bare-metal ports (because they already have the errno qstr) - for Python code that uses exc.errno the generated bytecode is 2 bytes smaller and more efficient to execute (compared with exc.args[0]); so bytecode loaded to RAM saves 2 bytes RAM for each use of this attribute, and bytecode that is frozen saves 2 bytes flash/ROM for each use - it's easier/shorter to type, and saves 2 bytes of space in .py files that use it (for each use) Cons: - increases code size by 4-8 bytes on minimal ports that don't already have the `errno` qstr - all exceptions now have .errno and .value attributes (a cpydiff test is added to address this) See also #2407. Signed-off-by: Damien George <damien@micropython.org>
* py/objexcept: Compare mp_emergency_exception_buf_size signed.Emil Renner Berthing2020-10-22
| | | | | mp_emergency_exception_buf_size is signed, so let's make sure we compare it as such.
* py: Always give noop defines when MICROPY_ROM_TEXT_COMPRESSION disabled.Damien George2020-04-14
| | | | | | | | | This commit provides a typedef for mp_rom_error_text_t, and a macro define for MP_COMPRESSED_ROM_TEXT, when MICROPY_ROM_TEXT_COMPRESSION is disabled. This simplifies the configuration (it no longer has a special case for MICROPY_ENABLE_DYNRUNTIME) and makes it work for other cases that don't use compression (eg examples/embedding). This commit also ensures MICROPY_ROM_TEXT_COMPRESSION is defined during qstr processing.
* py/objexcept: Remove optional TimeoutError exception.Damien George2020-04-09
| | | | | | | | | | | | | | | | | | | TimeoutError was added back in 077812b2abe3f5e5325194f9694dad7eb38186dd for the cc3200 port. In f522849a4d5a978ac3d322d71a755f75d07e8ce6 the cc3200 port enabled use of it in the socket module aliased to socket.timeout. So it was never added to the builtins. Then it was replaced by OSError(ETIMEDOUT) in 047af9b10bfc6b0ec412f8450c6bec10ab95254b. The esp32 port enables this exception, since the very beginning of that port, but it could never be accessed because it's not in builtins. It's being removed: 1) to not encourage its use; 2) because there are a lot of other OSError subclasses which are not defined at all, and having TimeoutError is a bit inconsistent. Note that ports can add anything to the builtins via MICROPY_PORT_BUILTINS. And they can also define their own exceptions using the MP_DEFINE_EXCEPTION() macro.
* py/objexcept: Allow compression of exception message text.Jim Mussared2020-04-05
| | | | | | The decompression of error-strings is only done if the string is accessed via printing or via er.args. Tests are added for this feature to ensure the decompression works.
* 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.
* all: Add *FORMAT-OFF* in various places.Damien George2020-02-28
| | | | | | | | This string is recognised by uncrustify, to disable formatting in the region marked by these comments. This is necessary in the qstrdef*.h files to prevent modification of the strings within the Q(...). In other places it is used to prevent excessive reformatting that would make the code less readable.
* py/objexcept: Rename mp_obj_new_exception_msg_varg2 to ..._vlist.Damien George2020-02-18
| | | | | | | Follow up to recent commit ad7213d3c31bccb26a3f54f7492ccf4b0cc920f3, the name "varg2" is misleading, vlist describes better that the argument is a va_list. This name also matches CircuitPython, which already has such helper functions.
* 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/objexcept: Optimise mp_obj_new_exception[_arg1/_args] functions.Damien George2020-01-23
| | | | | This reduces code size by 10-70 bytes on all ports (except cc3200 which has no change).
* 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: Rename MP_QSTR_NULL to MP_QSTRnull to avoid intern collisions.Josh Lloyd2019-09-26
| | | | Fixes #5140.
* py/objgenerator: Move defn of mp_const_GeneratorExit_obj here.Damien George2019-08-22
| | | | Because the mp_obj_exception_t type is now globally available.
* various: Add and update my copyright line based on git history.Paul Sokolovsky2019-05-17
| | | | For modules I initially created or made substantial contributions to.
* py/objexcept: Fix hash of exc str created in mp_obj_new_exception_msg.Tom Collins2019-03-04
|
* 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/objexcept: Make sure mp_obj_new_exception_msg doesn't copy/format msgDamien George2018-12-10
| | | | | | | | | | mp_obj_new_exception_msg() assumes that the message passed to it is in ROM and so can use its data directly to create the string object for the argument of the exception, saving RAM. At the same time, this approach also makes sure that there is no attempt to format the message with printf, which could lead to faults if the message contained % characters. Fixes issue #3004.
* py/objexcept: Use macros to make offsets in emergency exc buf clearer.Damien George2018-12-10
|
* py/objexcept: Make MP_DEFINE_EXCEPTION public so ports can define excs.Damien George2018-03-17
|
* py/objexcept: Remove long-obsolete mp_const_MemoryError_obj.Damien George2018-02-15
| | | | | | | | This constant exception instance was once used by m_malloc_fail() to raise a MemoryError without allocating memory, but it was made obsolete long ago by 3556e45711c3b7ec712748d013e678d035185bdd. The functionality is now replaced by the use of mp_emergency_exception_obj which lives in the global uPy state, and which can handle any exception type, not just MemoryError.
* py/objtype: Implement better support for overriding native's __init__.Damien George2017-12-12
| | | | | | | | | | | This patch cleans up and generalises part of the code which handles overriding and calling a native base-class's __init__ method. It defers the call to the native make_new() function until after the user (Python) __init__() method has run. That user method now has the chance to call the native __init__/make_new and pass it different arguments. If the user doesn't call the super().__init__ method then it will be called automatically after the user code finishes, to finalise construction of the instance.
* py/objexcept: Use INT_FMT when printing errno value.Damien George2017-12-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/objexcept: Prevent infinite recursion when allocating exceptions.Damien George2017-09-21
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The aim of this patch is to rewrite the functions that create exception instances (mp_obj_exception_make_new and mp_obj_new_exception_msg_varg) so that they do not call any functions that may raise an exception. Otherwise it's possible to create infinite recursion with an exception being raised while trying to create an exception object. The two main things that are done to accomplish this are: 1. Change mp_obj_new_exception_msg_varg to just format the string, then call mp_obj_exception_make_new to actually create the exception object. 2. In mp_obj_exception_make_new and mp_obj_new_exception_msg_varg try to allocate all memory first using functions that don't raise exceptions If any of the memory allocations fail (return NULL) then degrade gracefully by trying other options for memory allocation, eg using the emergency exception buffer. 3. Use a custom printer backend to conservatively format strings: if it can't allocate memory then it just truncates the string. As part of this rewrite, raising an exception without a message, like KeyError(123), will now use the emergency buffer to store the arg and traceback data if there is no heap memory available. Memory use with this patch is unchanged. Code size is increased by: bare-arm: +136 minimal x86: +124 unix x64: +72 unix nanbox: +96 stm32: +88 esp8266: +92 cc3200: +80
* 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: Optimise types for common case where type has a single parent type.Damien George2017-04-12
| | | | | | | | | | | | | | | | | | | | | | | | 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
* py: Make sure that static emg-exc-buffer is aligned to size of mp_obj_t.Damien George2017-04-10
| | | | | | | | | | | | This buffer is used to allocate objects temporarily, and such objects require that their underlying memory be correctly aligned for their data type. Aligning for mp_obj_t should be sufficient for emergency exceptions, but in general the memory buffer should aligned to the maximum alignment of the machine (eg on a 32-bit machine with mp_obj_t being 4 bytes, a double may not be correctly aligned). This patch fixes a bug for certain nan-boxing builds, where mp_obj_t is 8 bytes and must be aligned to 8 bytes (even though the machine is 32 bit).
* py/objexcept: Convert mp_uint_t to size_t where appropriate.Damien George2017-02-16
|
* py/objexcept: Replace if-cond and assert(0) with simple assert.Damien George2017-01-17
|
* py/objexcept: Allow clearing traceback with 'exc.__traceback__ = None'.Paul Sokolovsky2016-11-14
| | | | | | | | | | | | | We allow 'exc.__traceback__ = None' assignment as a low-level optimization of pre-allocating exception instance and raising it repeatedly - this avoids memory allocation during raise. However, uPy will keep adding traceback entries to such exception instance, so before throwing it, traceback should be cleared like above. 'exc.__traceback__ = None' syntax is CPython compatible. However, unlike it, reading that attribute or setting it to any other value is not supported (and not intended to be supported, again, the only reason for adding this feature is to allow zero-memalloc exception raising).
* py/objexcept: Don't convert errno to str in constructor, do it in print.Damien George2016-05-12
| | | | | | | | OSError's are now printed like: OSError: [Errno 1] EPERM but only if the string corresponding to the errno is found.
* py: Add mp_errno_to_str() and use it to provide nicer OSError msgs.Damien George2016-05-12
| | | | | | | If an OSError is raised with an integer argument, and that integer corresponds to an errno, then the string for the errno is used as the argument to the exception, instead of the integer. Only works if the uerrno module is enabled.
* py: add async/await/async for/async with syntaxpohmelie2016-04-13
| | | | | | | | They are sugar for marking function as generator, "yield from" and pep492 python "semantically equivalents" respectively. @dpgeorge was the original author of this patch, but @pohmelie made changes to implement `async for` and `async with`.
* py: Change first arg of type.make_new from mp_obj_t to mp_obj_type_t*.Damien George2016-01-11
| | | | | | | | 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.
* py: Change type signature of builtin funs that take variable or kw args.Damien George2016-01-11
| | | | | With this patch the n_args parameter is changed type from mp_uint_t to size_t.
* py: Change type of .make_new and .call args: mp_uint_t becomes size_t.Damien George2016-01-11
| | | | | | | 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.