[Python-Dev] A Hygienic Macro System in Python? (original) (raw)

Daniel Mahler mahler@cyc.com
Tue, 19 Mar 2002 02:19:56 -0600


Greg Ewing writes:

Tim Peters <tim.one@comcast.net>:

Maybe Guido can introduce a canned "withlock" macro just for that :

withlock lock:
    do stuff

Daniel Mahler <mahler@cyc.com>:

There are many APIs that lead you to write code with certain fixed parts ... I think the thing worth noting here is that the introduced constructs would usually be colon&indent type entities

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()

which would be (sort of) equivalent to

def thunk(): do_something() lock.do(thunk)

How about introducing a defsyn construct, which takes an aditional *** argument, whose evaluation is delayed. The trick is to come up with syntax to distinguish compile/run times. At worst one could use strings to quote the generated code in the macro definition. Something like:

defsyn lock (lck, ***body):

'expand' = a new keyword representing the macroexpansion operation

generated code is automatically indented to the calling level

expand """ %s.acquire() try: %s finally: %s.release() """ (lck, body, lck)

lock lck1: code

Clearly this is suboptimal and fuzzy as is, but I think there is a sane idea try to get out :). Maybe someone can give it more help than I can.

cheers Daniel

except that the thunk would have to have write access to the namespace it's embedded in, perhaps implemented by sharing the stack frame.

Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+


Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev