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: symtable.Symbol.is_referenced() returns false for valid use
Type: Stage:
Components: Extension Modules, Library (Lib) Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, luke.schubert, xiang.zhang
Priority: normal Keywords:

Created on 2016-02-01 09:57 by luke.schubert, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg259316 - (view) Author: Luke Schubert (luke.schubert) Date: 2016-02-01 09:57
If the following function is saved in

def encode(inputLetters):
    code = {'C':'D', 'F':'E'}
    return set(code[x] for x in inputLetters)

and the following code is used to create a symtable for this function:

import symtable

if __name__ == "__main__":
    fileName = ''
    f = open(fileName, 'r')
    source =
    table = symtable.symtable(source, fileName, 'exec')
    children = table.get_children()
    for childTable in children:
        symbols = childTable.get_symbols()
        for s in symbols:
            if (not s.is_referenced()):
                print ("Unused symbol '%s' in function '%s'"
                       % (s.get_name(), childTable.get_name()))

then is_referenced() returns false for the 'code' symbol.

If the following function is saved instead:

def encode2(inputLetters):
    code = {'C':'D', 'F':'E'}
    return [ code[x] for x in inputLetters ]

then is_referenced() returns true for the 'code' symbol.

Possibly I'm misunderstanding what is_referenced() means, but I thought it should return true in both cases?
msg259356 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2016-02-02 05:01
The genexp implicitly creates a nested function in which *code* is referenced.
Date User Action Args
2022-04-11 14:58:27adminsetgithub: 70443
2016-02-02 05:01:19benjamin.petersonsetstatus: open -> closed
resolution: not a bug
messages: + msg259356
2016-02-01 11:01:29SilentGhostsetnosy: + benjamin.peterson
components: + Extension Modules, Library (Lib)
2016-02-01 10:51:56xiang.zhangsetnosy: + xiang.zhang
2016-02-01 09:57:05luke.schubertcreate