[Twisted-Python] failure.Failure

Christopher Armstrong carmstro at twistedmatrix.com
Sat May 4 01:20:13 MDT 2002


  So, I'm finally on my way to implementing robust PB errors, and I'm
getting close to completing phase 1, which is fixing up failure.Failure
in preparation for using it for PB errors. Some background:

  It was decided that we need to be able to trap an error based on a
hierarchy; that is, if I try to trap a "WordsError", it should also trap
"WordsParticipantError" and "WordsGroupError" (for example). 

  The way to do this is to get a list of all parents of an Exception
when a Failure is created with it; I use reflect.allYourBase for this.
However, these parents need to be serializable, so the solution is to
convert them to strings. The strings obviously need to be globally
unique, so I fully qualify them with reflect.qual().

  trap(), which is the method one uses for checking wether or not they
care about a particular failure, now just checks if any of its arguments
are in self.parents.

  The issue now is what the trap() API should be like. It used to be
that fail.trap(types, that, I, want, to, trap) would re-raise the
Failure if any of the types matched. This was kinda weird, as it
requires code like:

try:
   fail.trap(foo, bar)
except:
   print "Ok, so 'fail' matches 'foo' or 'bar'"

Yuck. My proposal is to do this:

if fail.trap(foo,bar): #trap() returns self when foo or bar matches
    print "This is similar to an 'except:' clause"

  So, if this is acceptable, should I worry about the two cases in
Twisted where fail() seems to be called? They don't even seem to make
sense in either case (one of them is obviously using trap() incorrectly,
the other just seems to use it in an odd way). So can I just change the
hardly-used API and fix the current places it's used, or should I remain
backwards-compatible?

  I've attached my diff -u of failure.py, if anyone wants to take a
look. It's very small. It makes test_failure fail, because it tests the
old behavior of trap().


-- 
                                Chris Armstrong
                         << radix at twistedmatrix.com >>
                http://twistedmatrix.com/users/carmstro.twistd/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: failure.diff
Type: text/x-patch
Size: 1581 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20020504/f97faccf/attachment.bin 


More information about the Twisted-Python mailing list