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.

Author eryksun
Recipients Peter Eastman, docs@python, eryksun, r.david.murray
Date 2015-08-06.01:08:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1438823328.42.0.375857493154.issue24800@psf.upfronthosting.co.za>
In-reply-to
Content
> 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
History
Date User Action Args
2015-08-06 01:08:48eryksunsetrecipients: + eryksun, r.david.murray, docs@python, Peter Eastman
2015-08-06 01:08:48eryksunsetmessageid: <1438823328.42.0.375857493154.issue24800@psf.upfronthosting.co.za>
2015-08-06 01:08:48eryksunlinkissue24800 messages
2015-08-06 01:08:47eryksuncreate