Add a preserve() method to relinquish control of the context stack

Issue #1 resolved
Nick Coghlan
repo owner created an issue

Add a new preserve() method that transfers all the registered callbacks to a new ContextStack instance and returns it, clearing them from the current instance without invoking them. Example usage: {{{ def open_files(*filenames):
"""Returns an (opened_files, context_stack) 2-tuple

The context stack will automatically close all of the opened files.
If any file fails to open, all previously opened file handles will be released immediately.
"""
with ContextStack() as stack:
    files = [stack.enter_context(open(fname)) for fname in filenames]
    return files, stack.preserve()

}}}

Comments (6)

  1. Log in to comment