7 routes42 sections324 units

Unit 3.6 · about 4 h

15 / 324

Context managers, exceptions and deterministic cleanup

Deterministic cleanup separates scripts from services. Tracebacks are the primary diagnostic; the hierarchy decides their quality.

  • implement

You can now

  • Implement a resource-owning context manager in class and @contextmanager forms, with cleanup proven on success and exception paths.
  • Derive the cleanup sequence for nested managers with failing handlers.
  • Design a small exception hierarchy that preserves cause and maps to actionable command-line errors.

Covers

  • Context managers, ownership and deterministic cleanup
  • Exception hierarchies, chaining, cleanup and actionable messages

Study, in this order

6 steps

  1. Context managers
    Stas' Python Cookbook - selected path

    Classes and Objects: Context managers (the @contextmanager opened() pattern with try/finally); nullcontext for conditional with blocks

  2. Exceptions and error handling
    Stas' Python Cookbook - selected path

    Raising and catching (try/except/else/finally); Re-raising and chaining (raise from, from None); Custom exceptions; Good habits

  3. The try statement
    Python language reference

    Section 8.4 with 8.4.2 except*: groups split across clauses; else runs only without exceptions; finally always runs

  4. The with statement
    Python language reference

    Section 8.5: __enter__ and __exit__ protocol; a true __exit__ return suppresses the exception

  5. Exception Chaining
    AlgoMaster Python - structured revision

    The Exception Chaining lesson and its exercise; trace __cause__ versus __context__

  6. Context Managers
    AlgoMaster Python - structured revision

    The Context Managers lesson and Exercise: Context Managers; then the with Statement lesson

Focus on

  • Exceptions in the with body are thrown into the @contextmanager generator at the yield point; the finally clause still runs before propagation.
  • __exit__ returning true suppresses; anything falsy re-raises. Swallowing by accident hides the failure the caller must see.
  • raise New from e keeps the cause; from None hides context deliberately. A bare raise re-raises the same exception with its traceback.
  • ExceptionGroup splits across except* clauses since Python 3.11: handled members are removed and the rest keeps propagating.

Reproduce closed-book

Closed-book, write a @contextmanager owning a temp file plus a two-level ConfigError hierarchy with a CLI exit-code map. Prove the file closes on an inner ValueError and that the mapped error keeps its __cause__.

Traps

  • A bare except: swallows KeyboardInterrupt and SystemExit. Catch Exception at broadest, and re-raise what you do not handle.
  • An exception raised in finally or in __exit__ replaces the in-flight one. Keep cleanup paths total and narrow.
  • Catching an ExceptionGroup with except instead of except*. Plain except matches the whole group object and never the members.

Practice

AlgoMaster Python: Exercise: Context Managers

Exercises 1 to 5, especially Exercise 3: Close a File After an Exception and Exercise 5: Build a Generator Context Manager

Then prove cleanup on the exception path in your own file.

AlgoMaster Python: Exercise: Exception Chaining

Exercises 1 to 5: implicit context, raise from a cause, from None, the two traceback bridge sentences, and a cause that differs from the context

Draw the __cause__ chain for each answer.

Self-test

  1. Where does an exception raised in a with body surface inside a @contextmanager generator?

  2. What is the difference between raise E from e, raise E from None and bare raise?

  3. except* ValueError handles half an ExceptionGroup. What happens to the rest?

  4. When does the else clause of try run, and what is it for?

Completion criterion

Stop when

Your manager closes the resource on success, exception and inner-raise paths, and your hierarchy maps each failure to one actionable message with its cause intact.

Mark passed, then open 3.7 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Stas' Python Cookbook - selected path
    Stas Bekmanprimarycourse
    Run Part I on dictionaries, iteration, functions, closures, decorators, classes and context managers; Part II on modules, files, paths, environment and arguments; and Part IV on versions, dependencies, packaging and code quality. Finish by building one src-layout package from a blank directory.
  • Python language reference
    Python Software Foundationrequiredreference
    Check Data model, Execution model, Import system and Exceptions whenever a claim depends on Python semantics. These pages are the authority when the course and a runtime observation disagree.
  • AlgoMaster Python - structured revision
    AlgoMastersupportpractice
    Use Functions, Modules and Packages, OOP, Advanced OOP, Exception Handling, Iterators and Generators, Type Hints and Annotations only after the cookbook pass. Open it for recall practice or a diagnosed weak mechanism, not as a second complete course.

Also in this section

  • Packaging Python Projects
    Python Packaging Authorityrequiredimplementation
    Follow the packaging tutorial once from a blank directory, using a src layout, pyproject.toml, an isolated build and a clean-environment installation test.
  • dataclasses - data classes
    Python Software Foundationreferencereference
    Read the @dataclass parameters (eq, order, frozen, slots), field() with default_factory, frozen instances, post-init processing, replace, asdict and astuple. Use for the validated frozen dataclass in attributes-protocols-dataclasses.
  • typing - type hints and protocols
    Python Software Foundationreferencereference
    Read Protocols for structural typing and runtime_checkable limits, Generics for TypeVar generics, and assert_type for narrowing checks. Use for the Protocol and narrowing parts of type-hints-protocols; PEP 695 syntax itself is in the language reference.
  • gc - cycle garbage collector
    Python Software Foundationreferencereference
    Read the cycle collector interface: collect, get_count, garbage and disable/enable. Use for the cycle-collection and measurement parts of refcounting-gc-weakrefs.
  • weakref - weak references
    Python Software Foundationreferencereference
    Read weak-reference objects (ref callbacks, proxy objects) and the WeakValueDictionary example. Use for the weak-reference part of refcounting-gc-weakrefs.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels