UnboundLocalError on global variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Konstantinos Pachopoulos

    UnboundLocalError on global variable

    Hi,
    i have a problem, the source of which is probably the fact, that i have
    not understood how to declare global variables - I use the Jython
    compiler, but i think this is a Python issue...

    First of all, i don not use any classes in this module. The problem is,
    that i declare and instantiate some vars outside the functions (global
    ones), but when i use them inside the functions, i get an
    "UnboundLocalEr ror" error.

    Here's the interesting part of the code:

    =============== =============== =============== =============== =============== ===============
    #imports
    ....

    #CONSTANTS
    ......


    ....
    #connection to DB
    dbcursor_=db.cu rsor()

    #GLOBAL VARS
    entryList_={}
    cmterID_=0 //VARIABLE DECLARATION
    projID_=0
    fileIDNumber_=0
    Commiter_={}
    Commit_={}
    Project_={}
    ProjectVersion_ ={}


    def updateCommiterT able(Commiter):
    query="INSERT INTO Commiter (pk_cmterID,cmt erName) VALUES
    ("+str(Commi ter[0])+",\""+str(Com miter[1])+"\");"
    dbcursor_.execu te(query)


    def updateCommitTab le(Commit):
    query="INSERT INTO Commit
    (pk_cmitID,cmit Time,fk_cmterID ,cmitProperties ,cmitComment,cm itCommentLines, fk_projID)
    VALUES (" \

    +str(Commit[0])+",\""+str(Com mit[1])+"\","+str(Com mit[2])+",\""+str(Com mit[3])+"\",\""+str(C ommit[4])+"\","+str(Com mit[5])+","+str(Commi t[6])+");"
    dbcursor_.execu te(query)


    def updateProjectTa ble(Project):
    dbcursor_.execu te("INSERT INTO Project
    (pk_projID,proj Name,projWebsit e,projContactPo int,projSrcPath ,projMailPath)
    VALUES (" \

    +str(Project[0])+",\""+str(Pro ject[1])+"\",\""+str(P roject[2])+"\",\""+str(P roject[3])+"\",\""+str(P roject[4])+"\",\""+str(P roject[5])+"\");")


    def updateProjectVe rsionTable(Proj ectVersion):
    dbcursor_.execu te("INSERT INTO ProjectVersion
    (pfk_projID,pro jName,projVersi on) VALUES (" \

    +str(ProjectVer sion[0])+",\""+str(Pro jectVersion[1])+"\",\""+str(P rojectVersion[2])+"\");");



    def getLogsLoop():

    while
    svnLogging_.get CurrentRevision Number()!=svnLo gging_.getLates tRevisionNumber ():


    try:
    entryList_=svnL ogging_.getNext Logs(PIVOT);
    except HeadRevisionRea chedException:
    print "Attempting to go over the HEAD revision..."

    for entry in entryList_:
    print "processing new SVN entry..."
    processLogEntry (entry)

    entryList_.clea r()


    def processLogEntry (entry):


    revision = int(entry.getRe vision())
    commiter = str(entry.getAu thor())
    datetime = getTimeStamp(en try.getDate())
    message = str(entry.getMe ssage())

    Commiter_[0] = cmterID_ //HERE's THE PROBLEM
    Commiter_[1] = commiter
    updateCommiterT able(Commiter_)


    Commit_[0] = revision
    Commit_[1] = datetime
    Commit_[2] = cmterID_
    Commit_[3] = "" #properties
    Commit_[4] = message
    Commit_[5] = getNumberOfLine s(message)
    Commit_[6] = projID_
    updateCommitTab le(Commit_)

    ProjectVersion_[0]=projID_
    ProjectVersion_[1]=""
    ProjectVersion_[2]=""
    updateProjectVe rsionTable(Proj ectVersion_)

    Project_[0]=projID_
    Project_[1]=""
    Project_[2]=""
    Project_[3]=""
    Project_[4]=""
    Project_[5]=""
    updateProjectTa ble(Project_)

    cmterID_+=1
    projID_+1

    ############### ############### HELPER##METHODS ############### ############### #

    ....
    ############### ############### HELPER##METHODS ############### ############### #


    getLogsLoop()
    =============== =============== =============== =============== =============== ===============


    And I get:
    =============== =============== =============== =============== =============== ===============
    Traceback (innermost last):
    File "ParseSVN2DB.py ", line 182, in ?
    File "ParseSVN2DB.py ", line 87, in getLogsLoop
    File "ParseSVN2DB.py ", line 100, in processLogEntry
    UnboundLocalErr or: local: 'cmterID_'
    =============== =============== =============== =============== =============== ===============

    The things is, that cmterID_ HAS BEEN instantiated... I don't understand.
    Can somebody please explain?
  • GuillaumeC

    #2
    Re: UnboundLocalErr or on global variable

    def processLogEntry (entry):
    # ADD THIS TO YOUR CODE
    global cmterID_
    >
    revision = int(entry.getRe vision())
    commiter = str(entry.getAu thor())
    datetime = getTimeStamp(en try.getDate())
    message = str(entry.getMe ssage())
    >
    Commiter_[0] = cmterID_ //HERE's THE PROBLEM
    The reason why it does not work is that there is an assignment to
    cmterID_ in your function - even if it is after the problem. As a
    consequence, the bytecode compiler makes this name local, and the name
    is unbound. The quick fix is to add the global statement as above.

    There is a good explanation here:
    Explaining why this works: n = [0] def list_access (): n[0] = n[0] + 1 return n try : print " \n list_access: ", list_access() e...


    May I suggest that you use less global variables, for instance
    enclosing your method in a class?

    Comment

    • GuillaumeC

      #3
      Re: UnboundLocalErr or on global variable

      def processLogEntry (entry):
      # ADD THIS TO YOUR CODE
      global cmterID_
      >
      revision = int(entry.getRe vision())
      commiter = str(entry.getAu thor())
      datetime = getTimeStamp(en try.getDate())
      message = str(entry.getMe ssage())
      >
      Commiter_[0] = cmterID_ //HERE's THE PROBLEM
      The reason why it does not work is that there is an assignment to
      cmterID_ in your function - even if it is after the problem. As a
      consequence, the bytecode compiler makes this name local, and the name
      is unbound. The quick fix is to add the global statement as above.

      There is a good explanation here:
      Explaining why this works: n = [0] def list_access (): n[0] = n[0] + 1 return n try : print " \n list_access: ", list_access() e...


      May I suggest that you use less global variables, for instance
      enclosing your method in a class?

      Comment

      Working...