04 — Context Managers (with), RAII & Exception Hierarchies
At a glance
After completing this lesson you can:
- Guarantee database transactions, locks, and temporary files clean up reliably using
__enter__and__exit__. - Build generator context managers with
@contextlib.contextmanager. - Structure clean domain exception hierarchies and avoid bare
except Exception:traps.
Time: ~45 minutes · Prerequisites: 03 — Strict Typing & Protocols
Reading Companion
- Book: Fluent Python (2nd Edition) — Luciano Ramalho
- Chapter 18: with, match, and else Blocks (Context managers,
contextlibutilities).
Why this matters in production
When a database query raises an unexpected exception inside a request handler, what happens to the transaction? If you don't use context managers, the connection remains in an uncommitted state or fails to return to the connection pool, eventually exhausting the pool and causing an outage for all users.
Deep dive
1. The Context Manager Protocol
with managed_resource() as target:
1. __enter__() is executed -> target bound
2. Body runs
3. __exit__(exc_type, exc_val, exc_tb) GUARANTEED to execute
Hands on
python3 phase0-foundations/03-python-internals/04-context-managers-and-exceptions/examples/01_demo.py
python3 phase0-foundations/03-python-internals/04-context-managers-and-exceptions/examples/02_trap.py
AI Trap & Code Review
# BAD AI-GENERATED CODE — SWALLOWING EXCEPTIONS BLINDLY
try:
process_order()
except Exception:
pass # Swallows syntax errors, KeyboardInterrupt, memory errors silently!
Exercises
- Rung 1 (
TimerContext): Measure elapsed execution time of a code block in milliseconds usingtime.perf_counter. - Rung 2 (
safe_transaction— Fix the AI Bug): Implement a context manager that callscommit()on clean exit androllback()if an exception occurs. - Rung 3 (
AppException Hierarchy): Build a hierarchy:AppError -> (DatabaseError, ValidationError, NotFoundError)with custom HTTP status codes and error codes. - Rung 4 (
suppress_and_log): Context manager that suppresses specified exception types while logging diagnostics to a buffer. - Rung 5 (
atomic_directory_scope): Context manager that stages file operations in a temp folder and renames it to the target directory only if no errors occur.
Wrapping up
Can you explain?
- What does returning
Truefrom__exit__do? - Why should you never catch
BaseException? - How does
@contextlib.contextmanagerturn a generator into a context manager?
Further Reading & References
- CPython Internals & Data Model Reference
- PEP 544 — Protocols: Structural subtyping (static duck typing)
- PEP 557 — Data Classes
- Python Garbage Collector (gc module) Architecture
Show reference solution notes
Solution & Engineering Notes — 04 Context Managers & Exceptions
- Suppressing exceptions: Returning
Truefrom__exit__tells Python to swallow the exception. ReturningFalseorNonelets the exception propagate up the call stack. BaseException: Catches system-exiting signals likeKeyboardInterruptandSystemExit. Only inherit from or catchExceptionfor standard application errors.
Self-check
Run python3 -m unittest phase0-foundations/03-python-internals/04-context-managers-and-exceptions/test_exercises.py -v until green, then record it here. Saved to progress/rungs.json; commit it.
Completion edits PROGRESS.md; commit it with the lesson.