• Damien George's avatar
    py: Only store the exception instance on Py stack in bytecode try block. · f040685b
    Damien George authored
    When an exception is raised and is to be handled by the VM, it is stored
    on the Python value stack so the bytecode can access it.  CPython stores
    3 objects on the stack for each exception: exc type, exc instance and
    traceback.  uPy followed this approach, but it turns out not to be
    necessary.  Instead, it is enough to store just the exception instance on
    the Python value stack.  The only place where the 3 values are needed
    explicitly is for the __exit__ handler of a with-statement context, but
    for these cases the 3 values can be extracted from the single exception
    This patch removes the need to store 3 values on the stack, and instead
    just stores the exception instance.
    Code size is reduced by about 50-100 bytes, the compiler and VM are
    slightly simpler, generate bytecode is smaller (by 2 bytes for each try
    block), and the Python value stack is reduced in size for functions that
    handle exceptions.