[issue26174] Exception alias cause destruction of existing variable

2016-01-21 Thread Brett Cannon

Brett Cannon added the comment:

So the exception is explicitly deleted when the `except` block is exited to 
prevent leaking memory from the traceback attached to the exception. Hence 
there's an implicit `del e` at the end of the `except` block which is what 
you're running up against.

But I'm closing this as 'wont fix' because making this edge case work would be 
real troublesome and destroy performance whenever you bound the caught 
exception. Basically you would have to do the equivalent of:

e = 42
try:
  1/0
except ZeroDivisionError as _hidden_e:
  _overridden = False
  try:
_old_e = e
_overridden = True
  except NameError:
pass
  e = _hidden_e
  # `except` block ...
  del e, _hidden_e
  if _hidden_flag:
e = _old_e

That's a lot of code to run on every `except` clause that just happens to 
shadow a previously existing variable name. Because we try and not make 
exceptions too expensive in order to make them usable for occasional control 
flow, I don't think we can afford to add all of this for this edge case.

--
nosy: +brett.cannon
resolution:  -> wont fix
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26174] Exception alias cause destruction of existing variable

2016-01-21 Thread Zachary Ware

Zachary Ware added the comment:

I don't believe this is a bug.

1) try/except does not introduce a new scope
2) 'except E as N' is just name assignment
3) except clauses are documented to always delete the exception alias at the 
end of the except block[0].  The example in the docs could be clarified to show 
that

   except E as N:
   foo

is really more like

   except E:
   N = sys.exc_info()[1]
   try:
   foo
   finally:
   del N

Note that 'as' assignment with the 'with' statement also overrides a local 
variable of the same name.  The simplest way to avoid this issue is to use a 
different name for the exception alias.


[0] https://docs.python.org/3/reference/compound_stmts.html#the-try-statement

--
nosy: +zach.ware

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26174] Exception alias cause destruction of existing variable

2016-01-21 Thread Ethan Furman

Changes by Ethan Furman :


--
nosy: +ethan.furman

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26174] Exception alias cause destruction of existing variable

2016-01-21 Thread Jan Pokorny

New submission from Jan Pokorny:

Encountered a weird behavior when working with variable with the same name as 
exception's alias.

Observed behavior:

- In case variable with the same name (e.g. 'e') already exists when any 
'except Error as e' block is executed, the 'e' variable is removed after 
exception handling finishes (NameError: name 'e' is not defined)

- Happens only in Python 3
- Code reproducing the issue included

Expected behavior:

- Variable is kept in its pre-exception state

Proposed solution:

- Store colliding variable into temporary variable, restore it afterwards

--
components: Interpreter Core
files: schrodinger.py
messages: 258757
nosy: japokorn
priority: normal
severity: normal
status: open
title: Exception alias cause destruction of existing variable
type: behavior
versions: Python 3.4
Added file: http://bugs.python.org/file41683/schrodinger.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com