This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Variable defined in exec(code) unreachable inside function call with visible name in dir() results
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, xiang.zhang
Priority: normal Keywords:

Created on 2016-04-22 06:54 by 324857679, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg263967 - (view) Author: ganix (324857679) Date: 2016-04-22 06:54
here is a code show what happend:
>>> def func():
	return ans

>>> func()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
  File "<pyshell#4>", line 4, in func
    return ans
NameError: name 'ans' is not defined

i tried this code in version 2.7,it is ok
msg263984 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-04-22 08:43
This seems to a behaviour change. In 2.7, return ans compiles to LOAD_NAME so it can find ans. But in 3.x, it compiles to LOAD_GLOBAL which searches ans from global scope. I don't know when this change is introduced.
msg263986 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-04-22 09:01
In Python 2, using the exec statement makes the compiler disable fast locals: 

    >>> def foo(): pass
    >>> def bar(): exec ''
    >>> foo.__code__.co_flags & inspect.CO_OPTIMIZED
    >>> bar.__code__.co_flags & inspect.CO_OPTIMIZED

This has never been the case in Python 3, in which exec() is a function instead of a statement. The exec function can be shadowed by a global named "exec", so the hack to disable fast locals was removed.
Date User Action Args
2022-04-11 14:58:29adminsetgithub: 71012
2016-04-22 09:01:55eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg263986

resolution: not a bug
stage: resolved
2016-04-22 08:55:46larrysetnosy: - larry
2016-04-22 08:43:12xiang.zhangsetnosy: + xiang.zhang, - 324857679
type: crash -> behavior
messages: + msg263984
components: + Interpreter Core, - Argument Clinic
2016-04-22 06:54:50324857679create