Macros in Python?

Dominic oblivious at web.de
Wed Apr 9 14:58:04 EDT 2003


(defmacro with-root-privs (() &body body)
   (let ((oldidsym (gensym)))
     `(let ((,oldidsym (geteuid)))
        (seteuid 0)
        (unwind-protect
	   (progn , at body)
	 (seteuid ,oldidsym)))))

Is there an easy way to produce similar code
in Python which gurantees that certain things
happen around some code? (Maybe by using
generators?)

The normal approach would be to enclose everything
with try: finally: but this would scatter the
code all over.

Is something like this feasible?

def server(a,b):
   if cond:
       ...
   else:
     With_Root_Privs:
        if x in user:
           active.append(x)
          (...)

Ciao,
  Dominic





More information about the Python-list mailing list