Skip to content

Assigning (try …) to a variable name used for an exception may fail #2684

@Kodiologist

Description

@Kodiologist
(setv e (try
  (/ 1 0)
  (except [e ZeroDivisionError]
    e)))
(print e)  ; NameError: name 'e' is not defined

This compiles to:

try:
    e = 1 / 0
except ZeroDivisionError as e:
    e = e
print(e)

We're trying to assign to an outer e in the except block, but that doesn't work, of course. We probably need a temporary variable for this.

@scauligi You may be interested in this, since it involves scoping.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions