Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
id modern/context_manager
name Context Manager
aliases
with-statement
RAII
resource-management
guide_url
problem Guarantee acquire/release pairing around a block of code, even when it raises.
symptoms
forgot to close
cleanup on exception
try/finally everywhere
temporary state that must be restored
verdict pythonic
caveats
@contextlib.contextmanager wants the yield inside try/finally — without it, an exception in the body skips your cleanup.
Returning True from __exit__ swallows the exception; do it only on purpose.
stdlib_sightings
open
contextlib.contextmanager
contextlib.ExitStack
tempfile.TemporaryDirectory

Context Manager

Pair acquire with release on every exit path, structurally — Python's RAII. Verdict: pythonic — any acquire/release pair you write twice deserves one.

Where What
pattern/ The importable code: AtomicWrite (protocol form), temporarily (generator form)
docs/ Fundamentals · Implementation guide · External examples
examples/atomic_deploy/ Mini-project: all-or-nothing config deployment via ExitStack
tests/ Behavioral tests for both managers and the mini-project
uv run python -m patterns.modern.context_manager.examples.atomic_deploy.main