Index: Lib/test/test_support.py =================================================================== --- Lib/test/test_support.py (revision 77519) +++ Lib/test/test_support.py (working copy) @@ -24,7 +24,7 @@ "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ", "findfile", "verify", "vereq", "sortdict", "check_syntax_error", "open_urlresource", "check_warnings", "CleanImport", - "EnvironmentVarGuard", "captured_output", + "EnvironmentVarGuard", "captured_output", "temp_cwd", "writablecwd", "captured_stdout", "TransientResource", "transient_internet", "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup", @@ -401,6 +401,25 @@ unlink(TESTFN) del fp +@contextlib.contextmanager +def temp_cwd(dirname=TESTFN+'cwd'): + """Create a temporary directory, and switch to it.""" + saved_cwd = os.getcwd() + os.mkdir(dirname) + try: + os.chdir(dirname) + yield os.getcwd() + finally: + os.chdir(saved_cwd) + rmtree(dirname) + +def writablecwd(func): + """Decorator which switch to a writable directory.""" + def wrapper(*args, **kw): + with temp_cwd(): + func(*args, **kw) + return wrapper + def findfile(file, here=__file__): """Try to find a file on sys.path and the working directory. If it is not found the argument passed to the function is returned (this does not