More Pythonic?

Aahz Maruch aahz at netcom.com
Mon Sep 13 23:58:54 EDT 1999


For a variety of reasons, my group is unhappy with urllib (obvious to
anyone who has tried to make extensive use of it, I think).  I'm getting
ready to write a wrapper for httplib, but I'm having trouble deciding
what is a better way of dealing with error conditions:

Obviously, the proper way to deal with e.g. socket errors is to throw an
exception.  However, this wrapper code is going to handle HTTP
redirects, and it's important to know where in the redirect chain the
error occurred.  If I do something simple like

    try :
        page = httpWrapper.open ( url )
    except IOError :
        print page.info()  # oops!

it doesn't work very well, because the page object is never
instantiated.  Of course, I could do something like

    try :
        page = httpWrapper()
        page.open ( url )
    except IOError :
        print page.info()

but I could also do something like

    try :
        page = httpWrapper.open ( url )
    except IOError :
        print sys.exc_info()[1][1].info()

where the exception tuple contains the page object (or something like a
page object that contains the necessary info).

Which is more in keeping with the Pythonic tradition?  Or am I missing
some other technique that's even better?
--
                      --- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het    <*>      http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6  (if you want to know, do some research)




More information about the Python-list mailing list