Exception feature creep! (was: re-entering in the normal flow after an exception is raised)

Paul Foley see at below.invalid
Sat Oct 2 03:00:35 EDT 2004


On 1 Oct 2004 12:52:38 -0700, Lonnie Princehouse wrote:

> In a recent post, Michele Simionato asked about resumable (or
> re-entrant) exceptions, the basic idea being that when raised and
> uncaught, these exceptions would only propagate backwards through a
> limited number of frames before being implicitly ignored.  Alex

What a strange idea.

> Nonetheless, I tried to write some demo code that implemented
> resumable exceptions, but ran face-first into a brick wall:  I
> couldn't find any way to manipulate the call stack (to execute code in
> a frame other than the current one)

The mistake you're making is to conflate Python's exceptions with the
resumable exceptions you're trying to implement.  Python exceptions
are more like Common Lisp's catch/throw mechanism (sort of a
dynamically scoped "goto") than they are like the condition system.
You can use Python's exception mechanism for the primitive control
transfer, and build something on top of that (but since you can't
write syntax in Python it's going to be butt-ugly).  That's what Shai
Berger was trying to do.

Keep a list of (exception-type, function) pairs.  Where you want a
try/except, write a try/finally instead, with the "try" part pushing
"except" handlers on the front of this list, and the "finally" part
taking them back off (to emulate dynamic binding...don't use threads).
Instead of using raise, look up the exception type in this list and
call the associated function, so it runs above the failure point in
the call stack (but note that it has to transfer control somewhere
else if you don't want to return).  For restarts, do a similar thing,
mapping restart names to ("randomly" named) exception subclasses (as
catch tags); where you want a restart, write try/except using that
subclass name, push it on the restarts list, and to invoke it look up
the name and use raise on the tag.  Or something.

-- 
Malum est consilium quod mutari non potest             -- Publilius Syrus

(setq reply-to
  (concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz>"))



More information about the Python-list mailing list