@contextlib.contextmanager<EOL>def temp_cwd(name='<STR_LIT>', quiet=False):
if isinstance(name, str):<EOL><INDENT>try:<EOL><INDENT>name = name.encode(sys.getfilesystemencoding() or '<STR_LIT:ascii>')<EOL><DEDENT>except UnicodeEncodeError:<EOL><INDENT>if not quiet:<EOL><INDENT>raise unittest.SkipTest('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>saved_dir = os.getcwd()<EOL>is_temporary = False<EOL>try:<EOL><INDENT>os.mkdir(name)<EOL>os.chdir(name)<EOL>is_temporary = True<EOL><DEDENT>except OSError:<EOL><INDENT>if not quiet:<EOL><INDENT>raise<EOL><DEDENT>warnings.warn('<STR_LIT>' + name,<EOL>RuntimeWarning, stacklevel=<NUM_LIT:3>)<EOL><DEDENT>try:<EOL><INDENT>yield os.getcwd()<EOL><DEDENT>finally:<EOL><INDENT>os.chdir(saved_dir)<EOL>if is_temporary:<EOL><INDENT>rmtree(name)<EOL><DEDENT><DEDENT>
Context manager that creates a temporary directory and set it as CWD.

The new CWD is created in the current directory and it's named *name*.
If *quiet* is False (default) and it's not possible to create or change
the CWD, an error is raised.  If it's True, only a warning is raised
and the original CWD is used.