Skip to content

return in finally can swallow exception #1231

@iritkatriel

Description

@iritkatriel

In the expr_match function:

def expr_match(ob, ed, c=InstanceDict, r=0):
    e, md, push, pop = ed
    push(c(ob, md))
    try:
        r = e.eval(md)
    finally:
        pop()
        return r

The return in the finally will swallow any in-flight exception. This means that if r.eval(md) raises any exception (including KeyboardInterrupt), this exception will be swallowed. This is probably not what the author intended.

See also: https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions.

It is possible that the intention was to write:

def expr_match(ob, ed, c=InstanceDict, r=0):
    e, md, push, pop = ed
    push(c(ob, md))
    try:
        r = e.eval(md)
    finally:
        pop()
    return r

which would return r is there is no exception, but allow the exception to propagate if there is one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions