[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Fab-user] exceptions, and finally()
From: |
Jeff Forcier |
Subject: |
Re: [Fab-user] exceptions, and finally() |
Date: |
Sat, 4 Jun 2011 10:23:42 -0700 |
Hi Rob,
On Fri, Jun 3, 2011 at 5:14 PM, Robert Terhaar
<address@hidden> wrote:
> Hi All,
>
> I know that Fabric does not currently use Python's built-in exceptions (as
> noted in bug http://code.fabfile.org/issues/show/277 ) However-
>
> It is possible to still wrap tasks inside a try(): for purposes of using
> finally(): as a way to implement pseudo roll-backs?
Fabric uses sys.exit() to abort, which raises SystemExit, so it does
play nicely with try/except/finally even though it's not specific like
it should be (re: the ticket). So you can use any combo of except and
finally as you would normally, or do both, eg:
>>> from fabric.api import abort
>>> try:
... abort("Ohnoes!") # or any Fab code that might call abort()
or sys.exit() at some point, namely most of it
... except SystemExit:
... print "Would exit here, but we're not gonna"
... finally:
... print "One more thing"
...
...
Fatal error: Ohnoes!
Aborting.
Would exit here, but we're not gonna
One more thing
>>> # haven't exited!
Again, you could just use try: / :finally and not even bother with the
SystemExit test, which would have the minor benefit of working in the
future when we do move to real exception raising.
HTH,
Jeff
--
Jeff Forcier
Unix sysadmin; Python/Ruby engineer
http://bitprophet.org