Context Managers & with Statement
Also Known As
Python with statement
context manager
__enter__ __exit__
TL;DR
The with statement guarantees __exit__ runs even on exception — used for file handles, locks, transactions, and any resource needing cleanup.
Explanation
Context managers implement __enter__ (setup, returns the resource) and __exit__ (cleanup, always called) — or use @contextlib.contextmanager with yield. with open('file') as f: closes the file whether the body succeeds or raises. Multiple resources: with open(a) as f, open(b) as g. Custom managers: @contextlib.contextmanager def db_transaction(conn): try: yield conn; conn.commit() except: conn.rollback(). contextlib.suppress(FileNotFoundError) silently ignores specific exceptions. contextlib.ExitStack manages a dynamic number of resources. The PHP equivalent is try/finally blocks or RAII patterns — Python's with is more ergonomic and prevents resource leaks idiomatically.
Diagram
flowchart TD
WITH[with statement] --> ENTER[__enter__ called<br/>acquire resource]
ENTER --> BODY[Execute body]
BODY -->|normal exit| EXIT[__exit__ called<br/>release resource]
BODY -->|exception| EXIT2[__exit__ called<br/>even on exception]
EXIT & EXIT2 --> CLEAN[Resource always cleaned up]
subgraph Examples
FILE2[with open file as f<br/>auto close]
LOCK2[with lock:<br/>auto release]
DB2[with db.transaction:<br/>auto commit or rollback]
MOCK2[with patch target:<br/>auto unpatch]
end
style CLEAN fill:#238636,color:#fff
style EXIT2 fill:#d29922,color:#fff
Common Misconception
✗ Context managers are only for file handling. Any resource that needs guaranteed cleanup — database connections, locks, network sockets, temporary directories — benefits from a context manager. The with statement guarantees __exit__ is called even if an exception occurs.
Why It Matters
Context managers (with statements) guarantee cleanup code runs even when exceptions occur — replacing try/finally patterns with a reusable, readable protocol.
Common Mistakes
- Not using with for file operations — files are not closed on exception without it.
- Not implementing __exit__ to handle exceptions — a context manager that re-raises is fine, but it must return False.
- Using contextlib.contextmanager without wrapping the yield in try/finally — cleanup skipped on exception.
- Nested with statements instead of a single with statement with multiple context managers.
Code Examples
✗ Vulnerable
# File not closed on exception:
f = open('data.txt')
data = f.read() # Exception here leaves file open
f.close() # Never reached
# Context manager — always closes:
with open('data.txt') as f:
data = f.read() # Exception here still closes the file
✓ Fixed
from contextlib import contextmanager
@contextmanager
def managed_transaction(conn):
try:
yield conn
conn.commit()
except Exception:
conn.rollback()
raise
with managed_transaction(db) as conn:
conn.execute('INSERT ...')
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
28
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 9
Perplexity 6
Google 2
Ahrefs 2
ChatGPT 1
Unknown AI 1
Also referenced
How they use it
crawler 21
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Low
⚡ Quick Fix
Use 'with open(path) as f:' for all file operations — the context manager guarantees the file is closed even if an exception occurs
📦 Applies To
python 2.5
web
cli
🔍 Detection Hints
f = open() without 'with' statement; database connection not in context manager; lock acquired without guaranteed release
Auto-detectable:
✓ Yes
pylint
ruff
flake8
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✓ Auto-fixable
Fix: Low
Context: Function
Tests: Update
CWE-404