Top "Contextmanager" questions

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

Function acting as both decorator and context manager in Python?

This might be pushing things a little too far, but mostly out of curiosity.. Would it be possible to have …

python decorator 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 I am …

python exception contextmanager
AttributeError: __enter__ using with statement SqlAlchemy session

I'm getting this AttributeError: __enter__ when I try to use SQLAalchemy session like in this guide. My code: Session = scoped_…

python session sqlalchemy contextmanager
Context manager for Python's MySQLdb

I am used to (spoiled by?) python's SQLite interface to deal with SQL databases. One nice feature in python's SQLite's …

python mysql with-statement contextmanager
Python nested context manager on multiple lines

In Python 2.6, we used to format our nested context manager that way: with nested( context1, context2 ) as a, b: pass …

python with-statement contextmanager
In python, is there a good idiom for using context managers in setup/teardown

I am finding that I am using plenty of context managers in Python. However, I have been testing a number …

python unit-testing contextmanager
Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

I have searched and I'm unable to come up with any good reason to use python's __enter__ /__exit__ rather than __…

python constructor destructor with-statement contextmanager
Trying to understand python with statement and context managers

I am new to this, and am just trying to understand the with statement. I understand that it is supposed …

python contextmanager
Is it good practice to depend on python's with...as statement

I'm curious if it is considered safe or good practice to depend on python's with...as statement. For example when …

python with-statement contextmanager
Python mock builtin 'open' in a class using two different files

I am having trouble figuring out how to mock two file opens in a class when they both use context …

python unit-testing mocking contextmanager