12.07.2015 Views

Recoverable Exceptions in Python

Recoverable Exceptions in Python

Recoverable Exceptions in Python

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Recoverable</strong> <strong>Exceptions</strong> <strong>in</strong> <strong>Python</strong>Author:Michael HudsonEmail:mwh@python.netApologies for the very simple slides, I had toolproblems, and ran out of time to try to fix them.This was largely because I kept chang<strong>in</strong>g my m<strong>in</strong>dabout what I wanted to say, and then how I wanted tosay it...


What Do I Mean By A <strong>Recoverable</strong>Exception?Simply, an exception where it's possible to choosebetween various ways of deal<strong>in</strong>g with the problem,<strong>in</strong>clud<strong>in</strong>g carry<strong>in</strong>g on from the po<strong>in</strong>t where the problemwas found and return<strong>in</strong>g the error to call<strong>in</strong>g code (as <strong>in</strong><strong>Python</strong>).[CL demo]


Why Would We Want This?It's hard to really give a good answer to this. Errorhandl<strong>in</strong>g is a 'programm<strong>in</strong>g <strong>in</strong> the large' topic, notsuited to simple demos <strong>in</strong> conference presentations.Part of the reason for this is that error handl<strong>in</strong>g islargely about protocol, and you don't really needprotocol if you've written all the code <strong>in</strong>volved (more onthis later).


How Do We Do This?Th<strong>in</strong>k about how exception handl<strong>in</strong>g works <strong>in</strong> <strong>Python</strong>today: control is transferred to different code afterunw<strong>in</strong>d<strong>in</strong>g the stack.To allow resumption after the error, we must transfercontrol to different code without unw<strong>in</strong>d<strong>in</strong>g the stack.This is just a function call.


Instead of:How Might We Use This?def divide(a, b):if b == 0:raise ZeroDivisionError()else: return a / bwe might write:def divide(a, b):if b == 0:return handle(ZeroDivisionError())else: return a / bhandle can raise the error or return some "<strong>in</strong>f<strong>in</strong>ite"value as it sees fit.


A Sidestep: zope.eventPart of Zope 3, the zope.event module allows you totell <strong>in</strong>terested but unknown to you code aboutsituations you've encountered:def divide(a, b):if b == 0:zope.event.notify("divid<strong>in</strong>g by zero")return HUGE_VALelse: return a / bIt isn't an error handl<strong>in</strong>g system because it's only anotification: it's not expected that the event handlerswill try to <strong>in</strong>fluence the course taken by the notify<strong>in</strong>gcode.


subscribers = []The Entire Code Of zope.eventdef notify(event):for subscriber <strong>in</strong> subscribers:subscriber(event)Pretty simple huh?


Why write zope.event if it's so simple?So What's The Po<strong>in</strong>t?It's about protocol aga<strong>in</strong>: You or I could writezope.event at the drop of a hat, but for it to be useful,the code do<strong>in</strong>g the notify<strong>in</strong>g and the code do<strong>in</strong>g thenotic<strong>in</strong>g have to use the same mechanism.zope.event is designed to impose the m<strong>in</strong>imum amountof policy consistent with this goal (zope.app.event doessometh<strong>in</strong>g complicated with <strong>in</strong>terfaces).


Another Sidestep: PEP 343Exception handlers <strong>in</strong> <strong>Python</strong> today have what is called"dynamic extent"; they are active for a period of timedeterm<strong>in</strong>ed by where control is <strong>in</strong> a program.If we are to implement an error handl<strong>in</strong>g systemsupport<strong>in</strong>g system <strong>in</strong> <strong>Python</strong>, we want the sameproperty and PEP 343 lets us do this.The system <strong>in</strong> a few slides time will let you write:with handler(a_handler):... handler active here ....


More about PEP 343Roughly speak<strong>in</strong>g, part of PEP 343 proposes that:with expr as var:... code ...becomes:hidden_var = exprvar = hidden_var.__enter__()try:... code ...f<strong>in</strong>ally:var.__exit__()(ignor<strong>in</strong>g some stuff about exceptions).


On With The CodeIt was a bit of a stretch to fit it onto slides, so...[<strong>Python</strong> demo]


RestartsEven harder to fit examples of onto one side,experience <strong>in</strong> the common lisp community shows thatthere is value <strong>in</strong> add<strong>in</strong>g another abstraction to the mix:restarts.A restart is, essentially a means of cont<strong>in</strong>u<strong>in</strong>g. Codecan arrange for restarts to be available, and a handlercan choose among them.[Demo This]


So ... what?Just after I realised that restartable exceptions werereally easy to implement, I'd orig<strong>in</strong>ally <strong>in</strong>tended todevelop a fancy, amaz<strong>in</strong>g restartable exception systemand show it off now.This didn't last very long; code written today that raisesan exception doesn't expect control to be com<strong>in</strong>g backand would break mightily if it did.What I'd like you to take away from this talk is the ideathat when someth<strong>in</strong>g goes wrong and you don't knowwhat to do, don't just give up, maybe you can asksomeone else what to do.


More ConclusionsAs to whether core <strong>Python</strong> would benefit from thisapproach, I f<strong>in</strong>d that really hard to say.As mentioned above, there should be at most onerestartable exception implementation so there wouldbe some advantage to bundl<strong>in</strong>g it with <strong>Python</strong>. Also, itwould be nice if the <strong>Python</strong> core allowed cont<strong>in</strong>uationafter some errors (NameErrors, anyone?), thoughlack<strong>in</strong>g the ability to fix code while it's execut<strong>in</strong>g makesthis less useful.Anyway, I hope this talk was <strong>in</strong>terest<strong>in</strong>g!


Credits and ReferencesI should certa<strong>in</strong>ly credit two of Kent Pitman's papers:* "Condition Handl<strong>in</strong>g <strong>in</strong> theLisp Language Family"* "Exceptional Situations In Lisp"available on the web at:http://www.nhplace.com/kent/Papers/<strong>in</strong>dex.htmlI also read various th<strong>in</strong>gs about error handl<strong>in</strong>g <strong>in</strong>Smalltalk, but ran out of time to research it properly (ortalk about them).Steve Alexander po<strong>in</strong>ted out zope.event to me.


Questions?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!