04 — Context Managers (with), RAII & Exception Hierarchies

At a glance

After completing this lesson you can:

Time: ~45 minutes · Prerequisites: 03 — Strict Typing & Protocols

Reading Companion

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

  1. Rung 1 (TimerContext): Measure elapsed execution time of a code block in milliseconds using time.perf_counter.
  2. Rung 2 (safe_transaction — Fix the AI Bug): Implement a context manager that calls commit() on clean exit and rollback() if an exception occurs.
  3. Rung 3 (AppException Hierarchy): Build a hierarchy: AppError -> (DatabaseError, ValidationError, NotFoundError) with custom HTTP status codes and error codes.
  4. Rung 4 (suppress_and_log): Context manager that suppresses specified exception types while logging diagnostics to a buffer.
  5. 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?

  1. What does returning True from __exit__ do?
  2. Why should you never catch BaseException?
  3. How does @contextlib.contextmanager turn a generator into a context manager?

Further Reading & References

Show reference solution notes

Solution & Engineering Notes — 04 Context Managers & Exceptions

  1. Suppressing exceptions: Returning True from __exit__ tells Python to swallow the exception. Returning False or None lets the exception propagate up the call stack.
  2. BaseException: Catches system-exiting signals like KeyboardInterrupt and SystemExit. Only inherit from or catch Exception for 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.

Confidence:

Completion edits PROGRESS.md; commit it with the lesson.