Dynamic Wind

def wind

wind(
before: () → AbstractContextManager[T, B],
after: (...) → Any | None = None,
*,
auto_exit: Literal[True] = True,
) Wind[T, B]
wind(
before: () → AbstractContextManager[T, B],
after: (...) → Any | None = None,
*,
auto_exit: Literal[False],
) Wind[T, Literal[False]]
wind(
before: () → T,
after: (...) → Any | None = None,
) Wind[T, Literal[False]]
wind(
before: None = None,
after: (...) → Any | None = None,
) Wind[None, Literal[False]]

class wind_range

class wind_range(stop: int, /)
class wind_range(start: int, stop: int, step: int = 1, /)

Bases: WindBase[wind_range, int]

Context manager providing multi-shot-safe range iteration.

Usage:

with wind_range(10) as r:
    for i in r:
        v = choose()   # multi-shot safe

On multi-shot re-entry, the iterator position is restored to the value it had when the continuation was captured, so the for loop resumes from the correct position.

property start: int
property stop: int
property step: int
__iter__() wind_range
__next__() int

class WindBase

class WindBase[T, S]

Bases: ABC, Generic

Base class for dynamic-wind context managers.

Subclasses implement the _wind_* protocol methods. The base class manages the wind stack and drives the snapshot/restore lifecycle for multi-shot continuations.

T: type of the value yielded by __enter__ (the as target) S: type of the snapshot state for multi-shot re-entry

Protocol methods:

_wind_enter()

Called on initial entry and on multi-shot re-entry. Returns the value that __enter__ yields (the as target).

_wind_exit(exc_type, exc_val, exc_tb)

Called on exit. Returns True to suppress the exception.

_wind_snapshot()

Called at continuation capture time. Returns an opaque value that will be passed to _wind_restore on re-entry. Default: returns None.

_wind_restore(state)

Called before _wind_enter on multi-shot re-entry. Receives the value returned by _wind_snapshot at capture time. Default: no-op.

class Ref

class Ref[T]

Bases: Protocol, Generic

Indirect reference returned by wind context manager.

wind wraps the return value of before() in a Ref so that multi-shot continuation resumes can update the underlying value. Use unwrap() to retrieve the current value:

with wind(lambda: open("path.txt")) as ref:
    ref.unwrap().read()

On multi-shot re-entry, before() is called again and the same Ref object is updated with the new return value. Because the Ref is a heap object shared across shots, unwrap() always returns the latest value even after frame restoration.

unwrap() T

class Wind

class Wind[T, B]

Bases: Protocol, Generic

Protocol for the wind context manager.