unboundlocalerror with cgi module

Kent Johnson kent at kentsjohnson.com
Mon Apr 10 14:41:51 EDT 2006


David Bear wrote:
> I'm attempting to use the cgi module with code like this:
> 
> import cgi
> fo = cgi.FieldStorage()
> # form field names are in the form if 'name:part'
> keys = fo.keys()
> for i in keys:
>         try:
>                 item,value=i.split(':')
>         except NameError, UnboundLocalError:
>                 print "exception..."
>                 item,value=(None,None)
> return(item,value)
> 
> However, the except block does not seem to catch the exception and an
> unboundlocalerror is thrown anyway. What am I missing?
>         
I don't know why you would get an UnboundLocalError from the above code, 
but the correct syntax is
   except (NameError, UnboundLocalError):

Your code is binding the actual exception to the name "UnboundLocalError"

Kent



More information about the Python-list mailing list