Fix for using "with"-statements in Python 2.5

A fix for #5852

I have not added the imports to tests though. Please let me know if we need that too and I'll add do that.