[docs] [issue24800] exec docs should note that the no argument form in a local scope is really the two argument form
eryksun
report at bugs.python.org
Thu Aug 6 03:08:48 CEST 2015
eryksun added the comment:
> If exec gets two separate objects as globals and locals,
> the code will be executed as if it were embedded in a
> class definition.
Probably there needs to be more clarification of the compilation context. Class definitions support lexical closures, whereas source code passed to exec is compiled at the time of the call, independent of the lexical context.
In the following example, the code objects for both the class body and the comprehension can access the free variable "a". In CPython, the class body references the free variable via the LOAD_CLASSDEREF op, and the comprehension uses the LOAD_DEREF op.
def f():
a = 5
class C:
print(a)
print([a for i in range(5)])
>>> f()
5
[5, 5, 5, 5, 5]
>>> dis.dis(f.__code__.co_consts[2])
3 0 LOAD_NAME 0 (__name__)
3 STORE_NAME 1 (__module__)
6 LOAD_CONST 0 ('f.<locals>.C')
9 STORE_NAME 2 (__qualname__)
4 12 LOAD_NAME 3 (print)
15 LOAD_CLASSDEREF 0 (a)
18 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
21 POP_TOP
5 22 LOAD_NAME 3 (print)
25 LOAD_CLOSURE 0 (a)
28 BUILD_TUPLE 1
31 LOAD_CONST 1 (<code object <listcomp> ...>)
34 LOAD_CONST 2 ('f.<locals>.C.<listcomp>')
37 MAKE_CLOSURE 0
40 LOAD_NAME 4 (range)
43 LOAD_CONST 3 (5)
46 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
49 GET_ITER
50 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
53 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
56 POP_TOP
57 LOAD_CONST 4 (None)
60 RETURN_VALUE
>>> dis.dis(f.__code__.co_consts[2].co_consts[1])
5 0 BUILD_LIST 0
3 LOAD_FAST 0 (.0)
>> 6 FOR_ITER 12 (to 21)
9 STORE_FAST 1 (i)
12 LOAD_DEREF 0 (a)
15 LIST_APPEND 2
18 JUMP_ABSOLUTE 6
>> 21 RETURN_VALUE
----------
nosy: +eryksun
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24800>
_______________________________________
More information about the docs
mailing list