classification
Title: Symbol table for comprehensions (list, dict, set) still includes temporary _[1] variable
Type: behavior Stage: patch review
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: miss-islington, mjpieters, ncoghlan, nitishch
Priority: normal Keywords: patch

Created on 2018-02-12 21:33 by mjpieters, last changed 2018-03-10 23:11 by miss-islington.

Pull Requests
URL Status Linked Edit
PR 5680 merged nitishch, 2018-02-14 07:31
PR 5913 merged miss-islington, 2018-02-26 21:32
Messages (4)
msg312081 - (view) Author: Martijn Pieters (mjpieters) * Date: 2018-02-12 21:33
In Python 2.6, a list comprehension was implemented in the current scope using a temporary _[1] variable to hold the list object:

>>> import dis
>>> dis.dis(compile('[x for x in y]', '?', 'exec'))
  1           0 BUILD_LIST               0
              3 DUP_TOP
              4 STORE_NAME               0 (_[1])
              7 LOAD_NAME                1 (y)
             10 GET_ITER
        >>   11 FOR_ITER                13 (to 27)
             14 STORE_NAME               2 (x)
             17 LOAD_NAME                0 (_[1])
             20 LOAD_NAME                2 (x)
             23 LIST_APPEND
             24 JUMP_ABSOLUTE           11
        >>   27 DELETE_NAME              0 (_[1])
             30 POP_TOP
             31 LOAD_CONST               0 (None)
             34 RETURN_VALUE

Nick Cochlan moved comprehensions into a separate scope in #1660500, and removed the need for a temporary variable in the process (the list / dict / set lives only on the stack).

However, the symbol table generates the _[1] name:

>>> import symtable
>>> symtable.symtable('[x for x in y]', '?', 'exec').get_children()[0].get_symbols()
[<symbol '.0'>, <symbol '_[1]'>, <symbol 'x'>]

Can this be dropped? I think all temporary variable handling can be ripped out.
msg312155 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-02-14 04:35
We still need to the ".0" style temporary variables that are used for argument names in the implicitly generated functions, but it's definitely plausible that we're not actually using the "_[1]" style hidden variables anywhere anymore.
msg312959 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-02-26 21:31
New changeset 3a087beddd9f0955eb9080a6fd1499ff89ca74bf by Nick Coghlan (Nitish Chandra) in branch 'master':
bpo-32836: Remove obsolete code from symtable pass (GH-5680)
https://github.com/python/cpython/commit/3a087beddd9f0955eb9080a6fd1499ff89ca74bf
msg313565 - (view) Author: miss-islington (miss-islington) Date: 2018-03-10 23:11
New changeset 5506d603021518eaaa89e7037905f7a698c5e95c by Miss Islington (bot) in branch '3.7':
bpo-32836: Remove obsolete code from symtable pass (GH-5680)
https://github.com/python/cpython/commit/5506d603021518eaaa89e7037905f7a698c5e95c
History
Date User Action Args
2018-03-10 23:11:49miss-islingtonsetnosy: + miss-islington
messages: + msg313565
2018-02-26 21:32:48miss-islingtonsetpull_requests: + pull_request5684
2018-02-26 21:31:27ncoghlansetmessages: + msg312959
2018-02-14 07:31:39nitishchsetkeywords: + patch
stage: patch review
pull_requests: + pull_request5476
2018-02-14 04:35:44ncoghlansettype: behavior
messages: + msg312155
2018-02-13 09:18:35serhiy.storchakasetnosy: + ncoghlan
2018-02-13 07:35:21nitishchsetnosy: + nitishch
2018-02-12 21:33:51mjpieterscreate