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.

Title: Unjustified SyntaxWarning
Type: Stage:
Components: Interpreter Core Versions:
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: jimjjewett, markus_gritsch, rhettinger
Priority: normal Keywords:

Created on 2004-10-25 09:06 by markus_gritsch, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg22843 - (view) Author: Markus Gritsch (markus_gritsch) Date: 2004-10-25 09:06
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
        print 'Warning: Unknown mode.'

def test():
    print cPickle.__doc__

if __name__ == '__main__':
msg22844 - (view) Author: Jim Jewett (jimjjewett) Date: 2004-10-25 21:59
Logged In: YES 

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.
msg22845 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-10-26 01:50
Logged In: YES 

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.
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 41072
2004-10-25 09:06:43markus_gritschcreate