Issue 26825: Variable defined in exec(code) unreachable inside function call with visible name in dir() results (original) (raw)
Issue26825
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(): exec('ans=1') print(dir()) return ans >>> func() ['ans'] Traceback (most recent call last): File "<pyshell#5>", line 1, in func() 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) * ![]() |
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) * ![]() |
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 1 >>> bar.__code__.co_flags & inspect.CO_OPTIMIZED 0 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. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:58:29 | admin | set | github: 71012 |
2016-04-22 09:01:55 | eryksun | set | status: open -> closednosy: + eryksunmessages: + resolution: not a bugstage: resolved |
2016-04-22 08:55:46 | larry | set | nosy: - larry |
2016-04-22 08:43:12 | xiang.zhang | set | nosy: + xiang.zhang, - 324857679type: crash -> behaviormessages: + components: + Interpreter Core, - Argument Clinic |
2016-04-22 06:54:50 | 324857679 | create |