[Python-Dev] A Hygienic Macro System in Python?

Bernhard Herzog bh@intevation.de
19 Mar 2002 12:18:59 +0100


Greg Ewing <greg@cosc.canterbury.ac.nz> writes:

> I've been trying to think of a Pythonic syntax for passing
> code blocks to procedures. The best thing I've come up with
> so far is something like
> 
>   lock.do:
>     something()

Another approach, which I've been thinking about for quite some time
now:

    under <expr>:
        something()

where <expr> evaluates to a controller object that implements a certain
interface. The code above would be more or less equivalent to this (the
__controller__ variable would only exist in the interpreter somewhere):

    __controller__ = <expr>
    while __controller__.should_run():
        try:
            try:
                __controller__.start()
                something(x)
                __controller__.end()
            except:
                __controller__.exception()
        finally:
            __controller__.exit()


This sort of thing would make some parts of Sketch a bit easier. I've
quite a few places with code like:

   doc.BeginTransaction()
   try:
        try:
            something()
        except:
            # an error occurred part way through. make sure everything
            # is reverted (sort of a rollback)
            doc.AbortTransaction()
   finally:
        doc.EndTransaction()


This could be replaced by:

    under doc.Transaction():
        something()


Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/