diff --git a/contextlib2.py b/contextlib2.py index f99bbee..f02a381 100644 --- a/contextlib2.py +++ b/contextlib2.py @@ -141,13 +141,14 @@ class closing(object): self.thing.close() +# Inspired by discussions on http://bugs.python.org/issue13585 class ContextStack(object): - """Context for programmatic management of resource cleanup + """Context manager for programmatic management of resource cleanup For example: with ContextStack() as stack: - files = [stack.enter_context(fname) for fname in filenames] + files = [stack.enter_context(open(fname)) for fname in filenames] # All opened files will automatically be closed at the end of # the with statement, even if attempts to open files later # in the list throw an exception diff --git a/docs/index.rst b/docs/index.rst index 93192aa..e4be3a3 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -199,7 +199,7 @@ API Reference statement as follows:: with ContextStack() as stack: - files = [stack.enter_context(fname) for fname in filenames] + files = [stack.enter_context(open(fname)) for fname in filenames] # All opened files will automatically be closed at the end of # the with statement, even if attempts to open files later # in the list throw an exception