Change in vdsm[master]: add exception ignoring utility

asegurap at redhat.com asegurap at redhat.com
Tue Aug 6 07:49:57 UTC 2013


Antoni Segura Puimedon has posted comments on this change.

Change subject: add exception ignoring utility
......................................................................


Patch Set 3:

(3 comments)

....................................................
File lib/vdsm/utils.py
Line 915:     sys.exit(-3)
Line 916: 
Line 917: 
Line 918: @contextmanager
Line 919: def ignored(*exceptions, **predicates):
Since it is similar in spirit to http://bugs.python.org/issue15806 I used the same name, but I can change it.
Line 920:     """
Line 921:     Provides an execution context in which exceptions present in the exceptions
Line 922:     argument are not raised. The raising of those exceptions can be made
Line 923:     conditional to a predicate. E.g.:


Line 921:     Provides an execution context in which exceptions present in the exceptions
Line 922:     argument are not raised. The raising of those exceptions can be made
Line 923:     conditional to a predicate. E.g.:
Line 924: 
Line 925:         with ignored(IOError, IOError=lambda e: e.errno == errno.ENOENT)
Done
Line 926:             print open('/root/.bashrc').read()
Line 927:     Will ignore the IOError only if it the file does not exist, not if the
Line 928:     permission is denied.
Line 929:     """


Line 931:         yield
Line 932:     except exceptions as e:
Line 933:         exc_class = e.__class__.__name__
Line 934:         if exc_class in predicates:
Line 935:             if predicates[exc_class](e):
1.
I thought about it but initially decided against it in pro of it being more general. I'm trying to think how the syntax could look like. My original thought was something like this:
    try:
        yield
    except exceptions as e:
        exc_class = e.__class__.__name__
        if exc_class in predicates:
            predicate = predicates[exc_class]
            if callable(predicate):
                if predicate(e):
                    pass
                else:
                    raise
            else:
                if e.errno == predicate:
                    pass
                else:
                    raise
        else:
            pass

As you can see, this would allow to have:
with ignored(IOError, IOError=errno.ENOENT)

However, I still prefer the lambda version because it also solves question #2:

Let me show it. I could declare the following (we could have it in utils):

    def ign_errno(*allowed_values):
        return lambda obj: obj.errno in allowed_values

then:
    with ignored(IOError, IOError=ign_errno(errno.ENOENT, errno.ENOPERM)):
        print open('/root/foo').read()
Line 936:                 pass
Line 937:             else:
Line 938:                 raise
Line 939:         else:


-- 
To view, visit http://gerrit.ovirt.org/17661
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: Ib043b027be306c8f9a9207f52cb1377e62b56dbc
Gerrit-PatchSet: 3
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Antoni Segura Puimedon <asegurap at redhat.com>
Gerrit-Reviewer: Antoni Segura Puimedon <asegurap at redhat.com>
Gerrit-Reviewer: Assaf Muller <amuller at redhat.com>
Gerrit-Reviewer: Dan Kenigsberg <danken at redhat.com>
Gerrit-Reviewer: Giuseppe Vallarelli <gvallare at redhat.com>
Gerrit-Reviewer: Mark Wu <wudxw at linux.vnet.ibm.com>
Gerrit-Reviewer: Saggi Mizrahi <smizrahi at redhat.com>
Gerrit-Reviewer: oVirt Jenkins CI Server
Gerrit-HasComments: Yes


More information about the vdsm-patches mailing list