@contextlib.contextmanager
def temp_cwd(name='tempcwd', quiet=False):
 if (have_unicode and isinstance(name, unicode)):
  try:
   name = name.encode((sys.getfilesystemencoding() or 'ascii'))
  except UnicodeEncodeError:
   if (not quiet):
    raise unittest.SkipTest('unable to encode the cwd name with the filesystem encoding.')
 saved_dir = os.getcwd()
 is_temporary = False
 try:
  os.mkdir(name)
  os.chdir(name)
  is_temporary = True
 except OSError:
  if (not quiet):
   raise
  warnings.warn(('tests may fail, unable to change the CWD to ' + name), RuntimeWarning, stacklevel=3)
 try:
  (yield os.getcwd())
 finally:
  os.chdir(saved_dir)
  if is_temporary:
   rmtree(name)

@ contextlib . contextmanager def temp_cwd ( name = 'tempcwd' , quiet = False ) : if ( have_unicode and isinstance ( name , unicode ) ) : try : name = name . encode ( ( sys . getfilesystemencoding ( ) or 'ascii' ) ) except UnicodeEncodeError : if ( not quiet ) : raise unittest . SkipTest ( 'unable to encode the cwd name with the filesystem encoding.' ) saved_dir = os . getcwd ( ) is_temporary = False try : os . mkdir ( name ) os . chdir ( name ) is_temporary = True except OSError : if ( not quiet ) : raise warnings . warn ( ( 'tests may fail, unable to change the CWD to ' + name ) , RuntimeWarning , stacklevel = 3 ) try : ( yield os . getcwd ( ) ) finally : os . chdir ( saved_dir ) if is_temporary : rmtree ( name )
context manager that creates a temporary directory and set it as cwd .