[Python-Dev] Advice in stat.py

Thomas Wouters thomas@xs4all.net
Sun, 29 Jul 2001 17:33:43 +0200


On Sat, Jul 28, 2001 at 09:54:21AM -0400, Guido van Rossum wrote:
> > Worse, according to the Reference Manual,

> >     The "from" form with "*" may only occur in a module scope.

> I don't know when that snuck in, but it's not enforced.  If we're
> serious, we should at least add a warning!

Eh, last I looked, you and Jeremy were most serious about this :) It came up
during the nested-scopes change in 2.1, where it was first made illegal, and
later just illegal in the presence of a nested scope:

(without future statement)
>>> def spam(x):
...      from stat import *
...      def eggs():
...          print x
... 
<stdin>:1: SyntaxWarning: local name 'x' in 'spam' shadows use of 'x' as
global in nested scope 'eggs'
<stdin>:1: SyntaxWarning: import * is not allowed in function 'spam' because
it contains a nested function with free variables

(with future statement)
>>> def spam(x):
...       from stat import *
...       def eggs():
...           print x
... 
  File "<stdin>", line 2
SyntaxError: import * is not allowed in function 'spam' because it contains
a nested function with free variables

> I'll add a bug report.

Should we warn about exec (without 'in' clause) in functions as well ?

(without future statement)
>>> def spam(x,y):
...     exec y
...     def eggs():
...         print x
... 
<stdin>:1: SyntaxWarning: local name 'x' in 'spam' shadows use of 'x' as
global in nested scope 'eggs'
<stdin>:1: SyntaxWarning: unqualified exec is not allowed in function 'spam'
it contains a nested function with free variables

(with future statement)
>>> def spam(x,y):
...      exec y
...      def eggs():
...          print x
... 
  File "<stdin>", line 2
SyntaxError: unqualified exec is not allowed in function 'spam' it contains
a nested function with free variables

The warnings *only* occur in the presence of a nested scope, though.

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!