Skip to content Skip to sidebar Skip to footer
Showing posts with the label Contextmanager

Writing A Contextmanager That Patches An Object

In my Python 3 test code, I have a lot of these statements: from unittest.mock import patch user =… Read more Writing A Contextmanager That Patches An Object

Python 'with' Statement, Should I Use Contextlib.closing?

from contextlib import closing def init_db(): with closing(connect_db()) as db: with a… Read more Python 'with' Statement, Should I Use Contextlib.closing?

How To Safely Handle An Exception Inside A Context Manager

I think I've read that exceptions inside a with do not allow __exit__ to be call correctly. If … Read more How To Safely Handle An Exception Inside A Context Manager

Conditional Or Optional Context Managers In With Statement

Suppose I have some kind of context manager (from a third-party library) that I am using like so: w… Read more Conditional Or Optional Context Managers In With Statement