Index: Lib/test/test_support.py =================================================================== --- Lib/test/test_support.py (revision 77609) +++ Lib/test/test_support.py (working copy) @@ -23,8 +23,8 @@ "is_resource_enabled", "requires", "find_unused_port", "bind_port", "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ", "findfile", "verify", "vereq", "sortdict", "check_syntax_error", - "open_urlresource", "check_warnings", "CleanImport", - "EnvironmentVarGuard", "captured_output", + "open_urlresource", "check_warnings", "CleanImport", "temp_cwd", + "with_writable_cwd", "EnvironmentVarGuard", "captured_output", "captured_stdout", "TransientResource", "transient_internet", "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup", @@ -401,6 +401,28 @@ unlink(TESTFN) del fp +_test_cwd = TESTFN[:-3] + 'cwd' + +@contextlib.contextmanager +def temp_cwd(name='tempcwd'): + """Create a temporary directory, and switch to it.""" + saved_dir = os.getcwd() + new_dir = os.path.join(_test_cwd, name) + os.makedirs(new_dir) + try: + os.chdir(new_dir) + yield new_dir + finally: + os.chdir(saved_dir) + rmtree(_test_cwd) + +def with_writable_cwd(func): + """Decorator which switches 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