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

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

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

Catching An Exceptions In __enter__ In The Calling Code In Python

Is there a way I can catch exceptions in the __enter__ method of a context manager without wrapping… Read more Catching An Exceptions In __enter__ In The Calling Code In Python

How To Return From With Statement?

I have a function that tries some list of params to connect to ftp and connects to the first server… Read more How To Return From With Statement?

Python: Standard Function And Context Manager?

In python, there are many functions that work as both standard functions and context managers. For … Read more Python: Standard Function And Context Manager?