On Fri, 2009-12-18 at 07:12 -0500, Neal Becker wrote:
> I haven't tried it, but it sounds really cool.  I suppose I should expect a 
> lot more overhead compared to try/except, since it's not built-in to python?

It's a pretty thin layer on top of try/except so I'll be surprised if
there is *too* much overhead.  The only overhead is at establishment
and/or invocation time - calling some extra functions, walking a few
stack frames, etc.

I've attached a highly scientific benchmark I threw together this
morning.  Results:

  Establishing a restart/tryexcept but not using it:

    test_tryexcept0:  1.23 usec
    test_restarts0:  34.90 usec

  Establishing a restart/tryexcept, returning a new value:

    test_tryexcept1:  3.56 usec
    test_restarts1:  67.30 usec

  Establishing a restart/tryexcept, raising a new error: 

    test_tryexcept2:  5.86 usec
    test_restarts2:  90.20 usec

  Not having to pass status flags or callback functions through every
layer of your API to properly recover from errors:

    tryexcept:    impossible :-(
    withrestart:  priceless  :-)


  Cheers,

      Ryan


-- 
Ryan Kelly
http://www.rfk.id.au  |  This message is digitally signed. Please visit
r...@rfk.id.au        |  http://www.rfk.id.au/ramblings/gpg/ for details

from withrestart import *


def test_tryexcept0():
    def raiser():
        return 7
    def invoker():
        return raiser()
    def catcher():
        try:
            return invoker()
        except ValueError:
            return 7
    assert catcher() == 7

def test_tryexcept1():
    def raiser():
        raise ValueError
    def invoker():
        return raiser()
    def catcher():
        try:
            return invoker()
        except ValueError:
            return 7
    assert catcher() == 7

def test_tryexcept2():
    def raiser():
        raise ValueError
    def invoker():
        raiser()
    def catcher():
        try:
            invoker()
        except ValueError:
            raise TypeError
    try:
        catcher()
    except TypeError:
        pass
    else:
        raise AssertionError



def test_restarts0():
    def raiser():
        return 7
    def invoker():
        with restarts(use_value) as invoke:
            return invoke(raiser)
    def catcher():
        with Handler(ValueError,"use_value",7):
            return invoker()
    assert catcher() == 7

def test_restarts1():
    def raiser():
        raise ValueError
    def invoker():
        with restarts(use_value) as invoke:
            return invoke(raiser)
    def catcher():
        with Handler(ValueError,"use_value",7):
            return invoker()
    assert catcher() == 7

def test_restarts2():
    def raiser():
        raise ValueError
    def invoker():
        with restarts(raise_error) as invoke:
            invoke(raiser)
    def catcher():
        with Handler(ValueError,"raise_error",TypeError):
            invoker()
    try:
        catcher()
    except TypeError:
        pass
    else:
        raise AssertionError


Attachment: signature.asc
Description: This is a digitally signed message part

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to