[Python-bugs-list] [ python-Bugs-528274 ] Nested Scopes bug (Confirmed)

noreply@sourceforge.net noreply@sourceforge.net
Sun, 10 Mar 2002 23:16:19 -0800


Bugs item #528274, was opened at 2002-03-11 00:38
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=528274&group_id=5470

Category: None
Group: Python 2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Alex A. Naanou (alex_nanou)
Assigned to: Nobody/Anonymous (nobody)
Summary: Nested Scopes bug (Confirmed)

Initial Comment:
Bug confirmed.

description:
names that a in a scope just above the scome of the 
nested block (function) do not appear that functions 
globals thus bracking SOME (!) of the code that uses 
them. in particular the eval function, and all code 
that explicitly searces the name in the globals.

code:
//see the attached file.

output:
- a NameError raised in function nested_1:
'NameError: global name 'a' is not defined'
- a NameError raised in function nested_2:
'NameError: name 'a' is not defined'

workarround:
explicitly pass the variable to the function as its 
default argument (as in nested_0).
NOTE: declaring the name global does not work (as 
shown in function nested_1).
//see the attached file for more details.

test results:
 there appears to be a gap between the globals and the 
locals of a nested function.

test systems:
WIN2K v.5.00.2195 SP2
- ActivePython 2.2 Alpha 2 build 1 (ActiveState) 
Python 2.2a2+ (#22, Sep  5 2001, 14:10:41) [MSC 32 bit 
(Intel)] on win32
- Python 2.2 (#1, Dec 31 2001, 15:21:18) [GCC 2.95.3-5 
(cygwin special)] on cygwin
FreeBSD 4.5 (LocalBuild)
Python2.2 //I do not remember the exact build.

                                       With Respect..
                                             Alex.

----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2002-03-11 08:16

Message:
Logged In: YES 
user_id=21627

It is not clear from your report what bug you are reporting.
Please structure a bug report in the following way:
- what did you do (you've explained this, providing a script)
- what happened (you've also reported that)
- what did you expect to happen instead (this is hard to
find in your report)
- why do you believe that the bug is in Python and not in
your understanding of the language (optional)

Analizing what you got as output: in your report:

- NameError in nested_1: This is not a bug. You declare a as
a global variable, yet there is no global variable with that
name, hence the NameError.

- NameError in nested_2: This appears to be a bug. See

http://www.python.org/doc/current/ref/dynamic-features.html

  for a discussion of nested scopes and dynamic features. In
your example, you reference the variable 'a' before using it
in eval(), so it should be available to eval(). Notice,
however, that removing the variable access from nested_2
also makes it unavailable to eval().

In the script, you argue that the variable should be either
global or local. This is not true in Python 2.2, see

http://www.python.org/doc/current/ref/nested-scopes.html

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=528274&group_id=5470