Remove Python 2.4-incompatible 'with' statement

This commit replaces 'with' statement in wait_for module with try-finally block. Fixes #5044