Top "Contextmanager" questions

A python context manager manages the context of a with statement.

Calling __enter__ and __exit__ manually

I've googled calling __enter__ manually but with no luck. So let's imagine I have MySQL connector class that uses __enter__ …

python python-3.x contextmanager
python 'with' statement, should I use contextlib.closing?

from contextlib import closing def init_db(): with closing(connect_db()) as db: with app.open_resource('schema.sql') as …

python with-statement contextmanager
Python logger is not printing debug messages, although it is set correctly

I have the following code, where I just want to play around with the logging module using contextmanager. from contextlib …

python python-3.x logging contextmanager
How do I write a null (no-op) contextmanager in Python?

Sometimes I need a dummy context manager that does nothing. It can then be used as a stand-in for a …

python contextmanager
Python timeout context manager with threads

I have timeout context manager that works perfectly with signals but it raises error in multithread mode because signals work …

python timeout contextmanager time-limiting
why does Contextmanager throws a runtime error 'generator didn't stop after throw()'?

In my utility.py I have, @contextmanager def rate_limit_protection(max_tries=3, wait=300): tries = 0 while max_tries > tries: …

python contextmanager
Alternative to contextlib.nested with variable number of context managers

We have code that invokes a variable number of context managers depending on runtime parameters: from contextlib import nested, contextmanager @…

python deprecated with-statement contextmanager
Meaning of "with" statement without "as" keyword

I'm familiar with using python's with statement as a means of ensuring finalization of an object in the event of …

python with-statement contextmanager
How can I use pytest.raises with multiple exceptions?

I'm testing code where one of two exceptions can be raised: MachineError or NotImplementedError. I would like to use pytest.…

python-3.x exception pytest contextmanager
with and closing of files in Python

I have read, that file opened like this is closed automatically when leaving the with block: with open("x.txt") …

python with-statement urlopen contextmanager