Skip to content
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

Builtins treated as free variables? #48470

Closed
dfugate mannequin opened this issue Oct 28, 2008 · 4 comments
Closed

Builtins treated as free variables? #48470

dfugate mannequin opened this issue Oct 28, 2008 · 4 comments
Assignees
Labels
build The build process and cross-build docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@dfugate
Copy link
Mannequin

dfugate mannequin commented Oct 28, 2008

BPO 4220
Nosy @loewis, @birkenfeld, @terryjreedy, @abalkin
Files
  • garbage.py: repro
  • 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 = 'https://github.com/birkenfeld'
    closed_at = <Date 2009-01-11.13:45:03.507>
    created_at = <Date 2008-10-28.16:19:47.511>
    labels = ['interpreter-core', 'build', 'docs']
    title = 'Builtins treated as free variables?'
    updated_at = <Date 2009-01-11.13:45:03.505>
    user = 'https://bugs.python.org/dfugate'

    bugs.python.org fields:

    activity = <Date 2009-01-11.13:45:03.505>
    actor = 'georg.brandl'
    assignee = 'georg.brandl'
    closed = True
    closed_date = <Date 2009-01-11.13:45:03.507>
    closer = 'georg.brandl'
    components = ['Documentation', 'Interpreter Core']
    creation = <Date 2008-10-28.16:19:47.511>
    creator = 'dfugate'
    dependencies = []
    files = ['11900']
    hgrepos = []
    issue_num = 4220
    keywords = []
    message_count = 4.0
    messages = ['75289', '75438', '77515', '79596']
    nosy_count = 5.0
    nosy_names = ['loewis', 'georg.brandl', 'terry.reedy', 'belopolsky', 'dfugate']
    pr_nums = []
    priority = 'low'
    resolution = 'wont fix'
    stage = None
    status = 'closed'
    superseder = None
    type = 'compile error'
    url = 'https://bugs.python.org/issue4220'
    versions = ['Python 2.6', 'Python 3.0']

    @dfugate
    Copy link
    Mannequin Author

    dfugate mannequin commented Oct 28, 2008

    #----------------------------------------------------------------------
    VERSION AFFECTED: 2.5 and 2.6
    BUILD TYPE: x86
    FLAGS PASSED TO PYTHON.EXE: None
    OPERATING SYSTEM: 32-bit Windows Vista SP1

    #----------------------------------------------------------------------
    BRIEF DESCRIPTION:
    According to
    http://docs.python.org/dev/reference/executionmodel.html#interaction-
    with-dynamic-features, a free variable is a variable used from some
    local scope that is not defined there. The behavior of Python 2.5/2.6
    is that it treats certain Python keywords and builtin types/functions
    used in nested scopes as free variables as well. This seems a little
    odd.

    Could the documentation be clarified to state: usage of certain Python
    keywords are considered to be free variables. I.e., a line simply
    containing "int" triggers the free variable behavior. Alternatively,
    usage of builtin types/functions could be allowed from nested functions
    with calls to "exec"…

    Thanks,

    Dave

    #----------------------------------------------------------------------
    REPRODUCTION SNIPPET:
    def a():
    def b():
    x = long(3) #Replace me with "x = 3L" and this works
    int #This must be commented out
    exec ""

    emits the following:
    File "garbage.py", line 4
    exec ""
    SyntaxError: unqualified exec is not allowed in function 'b' it is
    a nested function
    unless the "x = long(3)" and "int" lines are commented out.

    @dfugate dfugate mannequin assigned birkenfeld Oct 28, 2008
    @dfugate dfugate mannequin added docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) build The build process and cross-build labels Oct 28, 2008
    @terryjreedy
    Copy link
    Member

    The request as stated is invalid.
    'Int' and 'long' are built-in names, not keywords.

    The context of the cited sentence is
    "If a name is bound in a block, it is a local variable of that block,
    unless declared as nonlocal. If a name is bound at the module level, it
    is a global variable. (The variables of the module code block are local
    and global.) If a variable is used in a code block but not defined
    there, it is a free variable."

    Built-in names are not defined in the code block. Therefore, they are
    free names (variables). However, there is NO mention of builtins in
    this chapter up to this point*, so I can see how one could miss that.

    • The previous 'identifiers' chapter has only this:
      "The special identifier _ is used in the interactive interpreter to
      store the result of the last evaluation; it is stored in the builtins
      module."

    So something like the following could be added:
    "Free variables include the names of built-in objects."

    where *built-in objects* is linked to that section near the top of the
    Library manual. (The references later in the chapter are to the Lib
    manual entry on the builtins module, which pretty much repeats what is
    said later in the chapter.)

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 10, 2008

    There is no proposed patch yet, so this is out of scope for 2.5.3.

    @birkenfeld
    Copy link
    Member

    There is no way for the interpreter to distinguish between builtins and
    "other" types of free variables.

    If you need unqualified exec to work in an innner function, use function
    parameters with defaults, like this:

    def a():
        def b(long=long):
            x = long(3)
            exec ""

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    build The build process and cross-build docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants