Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python try except finally

It looks like I don't quite have the hang of Exception handling yet. I'm at a loss :( The following code sometimes returns this error:

File "applications/pingback/modules/plugin_h_pingback.py", line 190, in ping
    db(table.id==id_).update(status=status)
UnboundLocalError: local variable 'status' referenced before assignment

I would expect status to always have been assigned a value. Could it be that some other exception is thrown (perhaps in the inner try) and the finally obscures it?

...
try:
    server_url = self._get_pingback_server(target)
except PingbackClientError, e:
    status = e.message
else:
    try:
        server = xmlrpclib.ServerProxy(server_url)
        status = server.pingback.ping(self.source, target)
    except xmlrpclib.Fault, e:
        status = e
finally:
    db(table.id==id_).update(status=status) # <-- UnboundLocalError
...

Thanks, HC

like image 827
hcvst Avatar asked Dec 13 '22 17:12

hcvst


1 Answers

Your code doesn't always assign something to status. I can see a few ways that status might not be assigned and I've highlighted them below:

try:
    server_url = self._get_pingback_server(target)
except PingbackClientError, e:
    # If evaluating `e.message` raises an exception then status is not set.
    status = e.message  # <--- here
else:
    try:
        # If either of these two lines fails with something other than
        # xmlrcplib.Fault, then status is not set.
        server = xmlrpclib.ServerProxy(server_url)             # <--- here
        status = server.pingback.ping(self.source, target)     # <--- here
    # If xmlrpclib.Fault is not defined status is not set.
    except xmlrpclib.Fault, e:                                 # <--- here
        status = e
finally:
    db(table.id==id_).update(status=status)

I suspect that the most likely place for the error is in the inner try block where you are only catching xmlrpclib.Fault and not other types of exceptions.

like image 108
Mark Byers Avatar answered Dec 28 '22 23:12

Mark Byers