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.

classification
Title: Builtins treated as free variables?
Type: compile error Stage:
Components: Documentation, Interpreter Core Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: belopolsky, dfugate, georg.brandl, loewis, terry.reedy
Priority: low Keywords:

Created on 2008-10-28 16:19 by dfugate, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
garbage.py dfugate, 2008-10-28 16:19 repro
Messages (4)
msg75289 - (view) Author: David Fugate (dfugate) Date: 2008-10-28 16:19
#----------------------------------------------------------------------
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.
msg75438 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-11-01 06:42
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.)
msg77515 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-12-10 09:03
There is no proposed patch yet, so this is out of scope for 2.5.3.
msg79596 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-01-11 13:45
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 ""
History
Date User Action Args
2022-04-11 14:56:40adminsetgithub: 48470
2009-01-11 13:45:03georg.brandlsetstatus: open -> closed
resolution: wont fix
messages: + msg79596
2008-12-10 09:03:05loewissetnosy: + loewis
messages: + msg77515
versions: - Python 2.5.3
2008-11-01 06:43:15terry.reedysetversions: + Python 3.0
2008-11-01 06:42:09terry.reedysetpriority: low
nosy: + terry.reedy
messages: + msg75438
2008-10-29 02:59:39belopolskysetnosy: + belopolsky
2008-10-28 16:19:47dfugatecreate