contextmanager

__init__ vs __enter__ in context managers

__init__ vs __enter__ in context managers Question: As far as I understand, __init__() and __enter__() methods of the context manager are called exactly once each, one after another, leaving no chance for any other code to be executed in between. What is the purpose of separating them into two methods, and what should I put …

Total answers: 2

Asynchronous context manager

Asynchronous context manager Question: I have an asynchronous API which I’m using to connect and send mail to an SMTP server which has some setup and tear down to it. So it fits nicely into using a contextmanager from Python 3’s contextlib. Though, I don’t know if it’s possible write because they both use the …

Total answers: 4

Handling exceptions inside context managers

Handling exceptions inside context managers Question: I have some code where I try to reach a resource but sometimes it is unavailable, and results in an exception. I tried to implement a retry engine using context managers, but I can’t handle the exception raised by the caller inside the __enter__ context for my context manager. …

Total answers: 5

Getting "NoneType object has no attribute" when using "with … as" for custom context manager

Getting "NoneType object has no attribute" when using "with … as" for custom context manager Question: I wrote a simple context manager in Python for handling unit tests (and to try to learn context managers): class TestContext(object): test_count=1 def __init__(self): self.test_number = TestContext.test_count TestContext.test_count += 1 def __enter__(self): pass def __exit__(self, exc_type, exc_value, exc_traceback): if …

Total answers: 4

Python nested context manager on multiple lines

Python nested context manager on multiple lines Question: In Python 2.6, we used to format our nested context manager that way: with nested( context1, context2 ) as a, b: pass From Python 2.7 and on, nested is deprecated. I’ve seen lots of example of multiple context manager on a single line, but I can’t find …

Total answers: 4

How to safely handle an exception inside a context manager

How to safely handle an exception inside a context manager Question: I think I’ve read that exceptions inside a with do not allow __exit__ to be call correctly. If I am wrong on this note, pardon my ignorance. So I have some pseudo code here, my goal is to use a lock context that upon …

Total answers: 2

How to __enter__ n context managers?

How to __enter__ n context managers? Question: Using the with statement, we can enter many context handlers using only one level of indentation/nesting: >>> from contextlib import contextmanager >>> @contextmanager … def frobnicate(n): … print(‘frobbing {}’.format(n)) … yield … >>> frob1 = frobnicate(1) >>> frob2 = frobnicate(2) >>> with frob1, frob2: … pass … frobbing …

Total answers: 1

Python mock builtin 'open' in a class using two different files

Python mock builtin 'open' in a class using two different files Question: I am having trouble figuring out how to mock two file opens in a class when they both use context managers. I know how to do it for one context-managed file using the mock module like this: @patch(‘__builtin__.open’) def test_interface_mapping(self, mock_config): m = …

Total answers: 5

Calling __enter__ and __exit__ manually

Calling __enter__ and __exit__ manually Question: I’ve googled calling __enter__ manually but with no luck. So let’s imagine I have MySQL connector class that uses __enter__ and __exit__ functions (originally used with with statement) to connect/disconnect from a database. And let’s have a class that uses 2 of these connections (for example for data sync). …

Total answers: 3

Meaning of "with" statement without "as" keyword

Meaning of "with" statement without "as" keyword Question: I’m familiar with using python’s with statement as a means of ensuring finalization of an object in the event of an exception being thrown. This usually looks like with file.open(‘myfile.txt’) as f: do stuff… which is short-hand for f = file.open(‘myfile.txt’): try: do stuff… finally: f.close() or …

Total answers: 2