Improvizing a drop-in replacement for the "with" statement for Python 2.4

python, with-statement

Solution

Just use try-finally.

Really, this may be nice as a mental exercise, but if you actually do it in code you care about you will end up with ugly, hard to maintain code.

Problem

Can you suggest a way to code a drop-in replacement for the "with" statement that will work in Python 2.4? It would be a hack, but it would allow me to port my project to Python 2.4 more nicely. EDIT: Removed irrelevant metaclass sketch

Original source