467,136 Members | 1,252 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,136 developers. It's quick & easy.

unboundlocalerror with cgi module

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?

--
David Bear
-- let me buy your intellectual property, I want to own your thoughts --
Apr 10 '06 #1
  • viewed: 1434
Share:
8 Replies
Em Seg, 2006-04-10 *s 11:29 -0700, David Bear escreveu:
However, the except block does not seem to catch the exception and an
unboundlocalerror is thrown anyway. What am I missing?


See http://docs.python.org/tut/node10.html :

"""
A try statement may have more than one except clause, to specify
handlers for different exceptions. At most one handler will be executed.
Handlers only handle exceptions that occur in the corresponding try
clause, not in other handlers of the same try statement. An except
clause may name multiple exceptions as a parenthesized tuple, for
example:

.... except (RuntimeError, TypeError, NameError):
.... pass
"""

--
Felipe.

Apr 10 '06 #2
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
Apr 10 '06 #3
Kent Johnson wrote:
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):


One possibility is that there are no keys. Then the loop finishes
without ever setting item or values. This would give an unbound local
error. I assume that the OP would have noticed that in the traceback,
but perhaps he missed it.

OT, using i for the field namelike that makes my head hurt. Perhaps "for
fieldname in fo.keys():" would be in order instead.

Regards,

-tim

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

Kent


Apr 10 '06 #4
Tim Hochberg wrote:
Kent Johnson wrote:
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):


One possibility is that there are no keys. Then the loop finishes
without ever setting item or values. This would give an unbound local
error. I assume that the OP would have noticed that in the traceback,
but perhaps he missed it.


I think that would be a NameError for the code shown because item and
value are global variables. But anyway you raise a good point that
perhaps the reason the exception is not being caught is because it is
raised by code outside the scope of the try.

Also UnboundLocalError is a subclass of NameError so catching NameError
should catch UnboundLocalError as well.

Kent
Apr 10 '06 #5
Tim Hochberg wrote:
Kent Johnson wrote:
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):

One possibility is that there are no keys. Then the loop finishes
without ever setting item or values. This would give an unbound local
error. I assume that the OP would have noticed that in the traceback,
but perhaps he missed it.

OT, using i for the field namelike that makes my head hurt. Perhaps "for
fieldname in fo.keys():" would be in order instead.


This is bugging me. What's the above supposed to do? Right now, it
iterates over all of the keys and returns item,value for the last key.
That doesn't seem like that's what you want. Is it supposed to return a
sequence of item, value pairs? If that's the case, I think you could
just use:

return [fieldname.split(':') for fieldname in fo]

Or, if you just want the last one, you could just tack a [-1] on the end
of that, although it'll still bomb if there's no keys, so you might want
to check.

The above assumes that FieldStorage faithfully implements the iter part
of the dictionary protocol, something I'm not entirely sure about.

Regards,

-tim

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

Kent



Apr 10 '06 #6
"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?


why on earth do you expect a string split to result in either a NameError
or an UnboundLocalError ? (both of which signify *programming* errors,
most likely in *your* code, and should be fixed rather than ignored)

</F>

Apr 10 '06 #7
Kent Johnson wrote:
Tim Hochberg wrote:
Kent Johnson wrote:
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):
One possibility is that there are no keys. Then the loop finishes
without ever setting item or values. This would give an unbound local
error. I assume that the OP would have noticed that in the traceback,
but perhaps he missed it.

I think that would be a NameError for the code shown because item and
value are global variables.


Note that there is a return at the end of the posted code, so I suspect
that this problem actually occurs in a function somewhere and the OP
"simplified" things in order to post it.

But anyway you raise a good point that perhaps the reason the exception is not being caught is because it is
raised by code outside the scope of the try.
Also UnboundLocalError is a subclass of NameError so catching NameError
should catch UnboundLocalError as well.


Good point.

-tim

Apr 10 '06 #8
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
Which one ?-)
and an
unboundlocalerror is thrown anyway. What am I missing?


Err...

1/ the correct try/except syntax is:
try:
whatever_that_may_raise()
except (ExceptionClass1, ExceptionClassN):
handle_the_exception()

What you code would actually do (if it raised a NameError, which will
not be the case, cf next point) is to *bind* the exception object to the
name 'UnboundLocalError'.

2/ the line:
item,value=i.split(':')
*won't* raise NameError nor UnboundLocalError
3/ if fo.keys() returns a non-empty list, your code (that I assume is
the body of a function) will return the result of the split() for the
*last* item in the list (after having uselessly split'ed each and every
key...)

4/ else (ie : fo.keys() returns an empty list), *then* you'll get an
UnboundLocalError (once again, assuming this code is the body of a
function - but else you'd get a SyntaxError, since the return statement
is not allowed outside a function body), *but* this exception will be
raised when the 'return' statement is executed - not in the loop.
A corrected version of your code could be:

def extract_last_key(field_storage):
# form field names are in the form if 'name:part'
keys = field_storage.keys()
if keys:
return keys[-1].split(':', 1)
else:
return (None, None)

import cgi
fo = cgi.FieldStorage()
name, part = extract_last_key(fo)

I doubt this is actually what you *really* want, but this is at least
what your code effectively try to do.

FWIW, trying things at random until it seems to work is the worst way to
program (some call this 'programming by accident').
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 11 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Brad Clements | last post: by
6 posts views Thread by Alex Gittens | last post: by
15 posts views Thread by Paddy | last post: by
2 posts views Thread by Matthew Franz | last post: by
2 posts views Thread by Konstantinos Pachopoulos | last post: by
1 post views Thread by AmeL | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.