My question is, how can I execute any context manager without using with
?
Python has the idea of context managers,
instead of
You can call app.test_request.context('/?name=Peter')
to a variable (e.g. ctx
), and then call ctx.__enter__()
on it to enter the context manager, and ctx.__exit__(None, None, None)
to perform the cleanup. Note that you lose the safety guarantees of context managers, unless you put the ctx.__exit__
in a finally
clause.