[Python-Dev] Advice in stat.py

Guido van Rossum guido@zope.com
Sun, 29 Jul 2001 13:00:59 -0400


> 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.

Hm.  I'm curious why it was not made a warning without a nested
function.  Perhaps because too much 3rd party code would trigger the
warning?  (I have a feeling that lots of amateur programmers are a lot
fonder of import * than they should be :-( ).

> 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.

That one is just fine I think.

--Guido van Rossum (home page: http://www.python.org/~guido/)