Hacking python frames

At the last ChiPy meeting, Ian Bicking showed a fun little hack to install a doctest output checker from within a running doctest. The hack goes so far as to backtrack through the internal frame stack, locate the doctest frame, change some of its local vars, then switch out a code object with a new one (the func_code attribute). Huzzah!

Take a look at lxml.doctestcompare—scroll to the tempinstall() method. Looks like there might even be a module forthcoming (dtopt?) to aid in this kind of on-the-fly monkey patching madness :)