def with_rw_directory(func):
    """Create a temporary directory which can be written to, remove it if the
    test succeeds, but leave it otherwise to aid additional debugging."""

    @wraps(func)
    def wrapper(self, *args, **kwargs):
        path = tempfile.mkdtemp(prefix=func.__name__)
        keep = False
        try:
            return func(self, path, *args, **kwargs)
        except Exception:
            _logger.info(
                "%s %s.%s failed, output is at %r\n",
                "Test" if func.__name__.startswith("test_") else "Helper",
                type(self).__name__,
                func.__name__,
                path,
            )
            keep = True
            raise
        finally:
            # Need to collect here to be sure all handles have been closed. It appears
            # a Windows-only issue. In fact things should be deleted, as well as
            # memory maps closed, once objects go out of scope. For some reason
            # though this is not the case here unless we collect explicitly.
            gc.collect()
            if not keep:
                rmtree(path)

    return wrapper
Create a temporary directory which can be written to, remove it if the
    test succeeds, but leave it otherwise to aid additional debugging.
test/lib/helper.py