def _open_shelve(shelffn, withclosing=False):
    """
    Opens a shelf file.  If ``withclosing`` is True, it will be opened with
    closing, allowing use like:

        with _open_shelve('somefile',True) as s:
            ...
    """
    import shelve
    import contextlib
    shelf = shelve.open(shelffn, protocol=2)
    if withclosing:
        return contextlib.closing(shelf)
    else:
        return shelf
