Resume after exception

Richard Lewis richardlewis at fastmail.co.uk
Tue Jun 14 09:26:16 EDT 2005


On Tue, 14 Jun 2005 10:09:30 +0100, "Richard Lewis"
<richardlewis at fastmail.co.uk> said:
> Hi there,
> 
> Is it possible to have an 'except' case which passes control back to the
> point after the exception occurred?
> 
> e.g.
> 
> # a function to open the file
> # raises FileLockedException is file contains 'locked' information
> def open_file(file_name):
>     f = file(file_name, 'r')
>     {read first line for file lock info}
>     if first_line == "FILE LOCKED":
>         raise FileLockedException(lock_user, lock_timestamp)
>     {read remainder of file}
>     return True
> 
> # elsewhere in a user interface module
> def open_command():
>     try:
>         open_file("foo.bar")
>     except FileLockException, X:
>         ans = tkMessageBox.askyesno(title="File Locked", message="File
>         locked by '" + X.user + "' on " + X.time_stamp + "\nContinue
>         anyway?")
>         if ans == tkMessageBox.YES:
>             # return control to the remainder of the open_file function.
>             How?
>         else:
>             return False
> 
Thanks for your suggestions.

I've gone with the passing an 'ignore_lock' option to the open_file
function:

def open_file(self, ignore_lock=False):
    "Retrieves content file from FTP server and parses it into local DOM
    tree."
    ftp = ftplib.FTP(self.host)
    ftp.login(self.login, self.passwd)
	
    content_file = file(self.local_content_file_name, 'w+b')
    ftp.retrbinary("RETR " + self.path, content_file.write)
    ftp.quit()
    content_file.close()

    self.document = parse(self.local_content_file_name)

    root = self.document.documentElement
    if not(ignore_lock) and root.getAttribute("locked") == "1":
        raise ContentLocked(root.getAttribute("user"),
        root.getAttribute("time-stamp"))

    self.set_file_lock()

    self.opened = True

    return True

#..... elsewhere .....

def open_command(self):
    "Command to handle 'open' actions."
    try:
        self.site.load_from_server('user','host.name','login','passwd','path/to/content.xml',
        False)
    except ContentLocked, e:
        ans = QMessageBox.question("Content Locked", "The content file
        is locked!\n\nIt seems that the user '" + e.user + "' is already
        working on the website. They left the time stamp:\n" +
        e.time_stamp + "\n\nChoose 'Yes' to carry on working on the
        website and risk losing another user's changes, or 'No' to
        quit.", "Yes", "No", "Cancel")
        if ans == "Yes":
            self.site.load_from_server('user','host.name','login','passwd','path/to/content.xml',
            True)
        else:
        qApp.quit()
    except Exception, e:
        #...

I don't know why I put return False in my original open_command
function, it was supposed to be a quit call.

This solution allows me to keep my exception mechanism (which passes the
lock information [user and timestamp] out of the open_file function)
because it is dependent on ignore_lock being false.

Cheers,
Richard



More information about the Python-list mailing list