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: free vars sometimes show up in locals()
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jhylton Nosy List: douady, jhylton
Priority: normal Keywords:

Created on 2002-01-18 10:14 by douady, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Messages (4)
msg8835 - (view) Author: Cesar Douady (douady) Date: 2002-01-18 10:14
Python 2.2 under Linux.

Whether free variables are part of the local dictionary
or not is
unclear from the doc, but certainly the following
description is buggy:

> in :
>
> def foo():
>     x=1
>     def bar():
>         x
>         print locals()
>     bar()
> foo()
>
> the result is "{}", indicating that x is not in
locals(). But in :
>
> def foo():
>     x=1
>     def bar():
>         x
>         y=1
>         print locals()
>     bar()
> foo()
>
> the result is "{'y':1, 'x':1}", indicating that the
presence of y has
> made x part of locals().

The above is supported by Michael Hudson and he
suggested to assign this
bug report to Jeremy.

Also in :

def foo():
    x=1
    class bar:
        x
        y=1
    print dir(bar)
foo()

the result is "['__doc__', '__module__', 'y']", showing
that x was not
included.

This means that the local dictionary for a class (which
is supposed to
make up the class dictionary) and for a function is not
the same if free
variables are ever to be considered as part of the
local dictionary.

Looking at the interpreter code, the problem lies in
PyFrame_FastToLocals() (Objects/frameobject.c:406)
where there is a test
for (f->f_nlocals == NULL) which prevents free and cell
variables from
being transfered.
It is unclear whether this is an optimization fossile
from before nested
scopes or whether this is done on purpose to
differenciate the behavior
between functions (which almost always have locals) and
classes (for
which nlocals is 0 when PyFrame_FastToLocals is called).

In my opinion, the test should be suppressed and cell
variables should not be transfered to the local dict,
but this breaks the current test suite (in particular
test_scope).
msg8836 - (view) Author: Cesar Douady (douady) Date: 2002-01-18 13:48
Logged In: YES 
user_id=428521

I made a typo in the last paragraph.
I actually meant cell variables should always be transfered
to the local dict while free variables should not.
msg8837 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2002-03-13 21:41
Logged In: YES 
user_id=31392

Yes.  I think cell variables should always be transferred.

Free variables should if eval() is supposed to work as
currently advertised, but I don't think that's desirable.
msg8838 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2002-04-20 05:14
Logged In: YES 
user_id=31392

Fixed in rev 2.62 of frameobject.c
and in rev 2.59.6.3 on the 2.2 branch

Still needs to be backported in 2.1 branch, but that has
other nested scopes bugs to fix, too.
History
Date User Action Args
2022-04-10 16:04:53adminsetgithub: 35940
2002-01-18 10:14:52douadycreate