Thanks to visit codestin.com
Credit goes to github.com

Skip to content

command line interface of symtable module is broken #85098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
isidentical opened this issue Jun 9, 2020 · 4 comments
Closed

command line interface of symtable module is broken #85098

isidentical opened this issue Jun 9, 2020 · 4 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@isidentical
Copy link
Member

isidentical commented Jun 9, 2020

BPO 40926
Nosy @benjaminp, @pablogsal, @isidentical
PRs
  • bpo-40926: Improve & fix command line usage of symtable #20757
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2020-06-09.10:20:39.563>
    labels = ['type-bug', 'library', '3.10']
    title = 'command line interface of symtable module is broken'
    updated_at = <Date 2020-06-09.10:40:22.701>
    user = 'https://github.com/isidentical'

    bugs.python.org fields:

    activity = <Date 2020-06-09.10:40:22.701>
    actor = 'BTaskaya'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-06-09.10:20:39.563>
    creator = 'BTaskaya'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40926
    keywords = ['patch']
    message_count = 1.0
    messages = ['371088']
    nosy_count = 3.0
    nosy_names = ['benjamin.peterson', 'pablogsal', 'BTaskaya']
    pr_nums = ['20757']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40926'
    versions = ['Python 3.10']

    Linked PRs

    @isidentical
    Copy link
    Member Author

    (.venv) (Python 3.10.0a0) [ 1:11ÖS ] [ isidentical@x200:~ ]
    $ cat t.py
    import x
    a = 1
    print(x)

    (.venv) (Python 3.10.0a0) [ 1:11ÖS ] [ isidentical@x200:~ ]
    $ python -m symtable t.py
    <symbol '_symtable'> True False
    <symbol 'USE'> True False
    <symbol 'DEF_GLOBAL'> True False
    <symbol 'DEF_NONLOCAL'> True False
    <symbol 'DEF_LOCAL'> True False
    ...

    It can clearly seen that the initial argument [t.py] is completely ignored, and this script prints out the symtable.py itself. This is because the script uses argv[0] (itself) instead of argv[1] (the first argument). I also find this output quite poor since we don't know what these boolean values are;

    <symbol 'DEF_LOCAL'> True False

    The fix I had in my mind is printing all properties instead of 2 boolean values

     $ ./cpython/cpython/python -m symtable t.py
    <symbol 'x'> ==> {'local', 'imported', 'referenced'}
    <symbol 'a'> ==> {'local', 'assigned'}
    <symbol 'print'> ==> {'referenced', 'global'}

    @isidentical isidentical added 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 9, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @serhiy-storchaka
    Copy link
    Member

    I also worked on improving the CLI of symtable. I do not remember why I did not finish the work and did not touch the code for 3.5 years. In any case I show it in #109112.

    @serhiy-storchaka
    Copy link
    Member

    Example of the output:

    $ ./python -m symtable Lib/symtable.py 
    local symbol '_symtable': def_import
    local symbol 'USE': use, def_import
    local symbol 'DEF_GLOBAL': def_import
    local symbol 'DEF_NONLOCAL': def_import
    local symbol 'DEF_LOCAL': def_import
    local symbol 'DEF_PARAM': def_import
    local symbol 'DEF_IMPORT': def_import
    local symbol 'DEF_BOUND': def_import
    local symbol 'DEF_ANNOT': def_import
    local symbol 'SCOPE_OFF': def_import
    local symbol 'SCOPE_MASK': def_import
    local symbol 'FREE': def_import
    local symbol 'LOCAL': def_import
    local symbol 'GLOBAL_IMPLICIT': def_import
    local symbol 'GLOBAL_EXPLICIT': def_import
    local symbol 'CELL': def_import
    local symbol 'weakref': def_import
    local symbol '__all__': def_local
    local symbol 'symtable': use, def_local
        local symbol 'code': use, def_param
        local symbol 'filename': use, def_param
        local symbol 'compile_type': use, def_param
        local symbol 'top': use, def_local
        global_implicit symbol '_symtable': use
        global_implicit symbol '_newSymbolTable': use
    
    local symbol 'SymbolTableFactory': use, def_local
        local symbol '__init__': def_local
            local symbol 'self': use, def_param
            global_implicit symbol 'weakref': use
    
        local symbol 'new': def_local
            local symbol 'self': def_param
            local symbol 'table': use, def_param
            local symbol 'filename': use, def_param
            global_implicit symbol '_symtable': use
            global_implicit symbol 'Function': use
            global_implicit symbol 'Class': use
            global_implicit symbol 'SymbolTable': use
    
        local symbol '__call__': def_local
            local symbol 'self': use, def_param
            local symbol 'table': use, def_param
            local symbol 'filename': use, def_param
            local symbol 'key': use, def_local
            local symbol 'obj': use, def_local
    
    ...
    

    It outputs the scope separately from flags. It prints not only top-level symbols, but also symbols in nested namespaces.

    It also changes the repr of Symbol.

    @serhiy-storchaka
    Copy link
    Member

    serhiy-storchaka commented Sep 8, 2023

    After some thought I changed the output formatЖ

    $ ./python -m symtable Lib/symtable.py
    symbol table for module from file 'Lib/symtable.py':
        local symbol '_symtable': def_import
        local symbol 'USE': use, def_import
        local symbol 'DEF_GLOBAL': def_import
        local symbol 'DEF_NONLOCAL': def_import
        local symbol 'DEF_LOCAL': def_import
        local symbol 'DEF_PARAM': def_import
        local symbol 'DEF_IMPORT': def_import
        local symbol 'DEF_BOUND': def_import
        local symbol 'DEF_ANNOT': def_import
        local symbol 'SCOPE_OFF': def_import
        local symbol 'SCOPE_MASK': def_import
        local symbol 'FREE': def_import
        local symbol 'LOCAL': def_import
        local symbol 'GLOBAL_IMPLICIT': def_import
        local symbol 'GLOBAL_EXPLICIT': def_import
        local symbol 'CELL': def_import
        local symbol 'weakref': def_import
        local symbol '__all__': def_local
        local symbol 'symtable': def_local
        local symbol 'SymbolTableFactory': use, def_local
        local symbol '_newSymbolTable': def_local
    ...
        local symbol 'sys': use, def_import
        local symbol 'n': use, def_local
    
        symbol table for function 'symtable':
            local symbol 'code': use, def_param
            local symbol 'filename': use, def_param
            local symbol 'compile_type': use, def_param
            local symbol 'top': use, def_local
            global_implicit symbol '_symtable': use
            global_implicit symbol '_newSymbolTable': use
    
        symbol table for class 'SymbolTableFactory':
            local symbol '__init__': def_local
            local symbol 'new': def_local
            local symbol '__call__': def_local
    
            symbol table for function '__init__':
                local symbol 'self': use, def_param
                global_implicit symbol 'weakref': use
    
    ...
    

    Now nested symbol tables are output after symbols and include header with some table related information. It also helps when specify several input files.

    pablogsal added a commit that referenced this issue Nov 7, 2023
    hugovk pushed a commit to hugovk/cpython that referenced this issue Nov 8, 2023
    aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
    Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants