[ python-Bugs-1053604 ] Unjustified SyntaxWarning

SourceForge.net noreply at sourceforge.net
Tue Oct 26 03:50:23 CEST 2004


Bugs item #1053604, was opened at 2004-10-25 04:06
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1053604&group_id=5470

Category: Parser/Compiler
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Markus Gritsch (markus_gritsch)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: Unjustified SyntaxWarning

Initial Comment:
The following short script produces a "SyntaxWarning:
name 'cPickle' is assigned to before global
declaration", which is IMO not correct.


mode = 'pickle'

def main():
    if mode == 'pickle':
        global cPickle
        import cPickle
    elif mode == 'socket':
        global cPickle, socket
        import cPickle, socket
    elif mode == 'sqlite':
        global sqlite
        import sqlite
    else:
        print 'Warning: Unknown mode.'

def test():
    print cPickle.__doc__

if __name__ == '__main__':
    main()
    test()


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

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-10-25 20:50

Message:
Logged In: YES 
user_id=80475

Sorry, this isn't a bug.

Per the language reference, the global statement is not
executed like other python statements.  Instead, it is a
parser directive.  Hence, it doesn't matter whether the
globals are wrapped inside if blocks.  

The presence of the first occurrence of cPickle is
sufficient to define so that the subsequently parsed (not
executed) global directive cannot work.

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

Comment By: Jim Jewett (jimjjewett)
Date: 2004-10-25 16:59

Message:
Logged In: YES 
user_id=764593

The indenting was lost.  If I guess correctly, cPickle is 
global for modes pickle and socket, but not otherwise.

The problem is that it imports cpickle (for mode pickle) 
before running the global cpickle (for mode socket).

It would be nice if the interpreter were smart enough to 
realize that the paths are exclusive, but I'm inclined to call 
it a feature request rather than a bug.

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

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


More information about the Python-bugs-list mailing list