@@ -740,45 +740,85 @@ def _is_ipv6_enabled():
740740SAVEDCWD = os .getcwd ()
741741
742742@contextlib .contextmanager
743- def temp_cwd (name = 'tempcwd' , quiet = False , path = None ):
744- """
745- Context manager that temporarily changes the CWD.
743+ def temp_dir (path = None , quiet = False ):
744+ """Return a context manager that creates a temporary directory.
745+
746+ Arguments:
747+
748+ path: the directory to create temporarily. If omitted or None,
749+ defaults to creating a temporary directory using tempfile.mkdtemp.
746750
747- An existing path may be provided as *path*, in which case this
748- function makes no changes to the file system.
751+ quiet: if False (the default), the context manager raises an exception
752+ on error. Otherwise, if the path is specified and cannot be
753+ created, only a warning is issued.
749754
750- Otherwise, the new CWD is created in the current directory and it's
751- named *name*. If *quiet* is False (default) and it's not possible to
752- create or change the CWD, an error is raised. If it's True, only a
753- warning is raised and the original CWD is used.
754755 """
755- saved_dir = os .getcwd ()
756- is_temporary = False
756+ dir_created = False
757757 if path is None :
758- path = name
758+ path = tempfile .mkdtemp ()
759+ dir_created = True
760+ path = os .path .realpath (path )
761+ else :
759762 try :
760- os .mkdir (name )
761- is_temporary = True
763+ os .mkdir (path )
764+ dir_created = True
762765 except OSError :
763766 if not quiet :
764767 raise
765- warnings .warn ('tests may fail, unable to create temp CWD ' + name ,
768+ warnings .warn ('tests may fail, unable to create temp dir: ' + path ,
766769 RuntimeWarning , stacklevel = 3 )
770+ try :
771+ yield path
772+ finally :
773+ if dir_created :
774+ shutil .rmtree (path )
775+
776+ @contextlib .contextmanager
777+ def change_cwd (path , quiet = False ):
778+ """Return a context manager that changes the current working directory.
779+
780+ Arguments:
781+
782+ path: the directory to use as the temporary current working directory.
783+
784+ quiet: if False (the default), the context manager raises an exception
785+ on error. Otherwise, it issues only a warning and keeps the current
786+ working directory the same.
787+
788+ """
789+ saved_dir = os .getcwd ()
767790 try :
768791 os .chdir (path )
769792 except OSError :
770793 if not quiet :
771794 raise
772- warnings .warn ('tests may fail, unable to change the CWD to ' + path ,
795+ warnings .warn ('tests may fail, unable to change CWD to: ' + path ,
773796 RuntimeWarning , stacklevel = 3 )
774797 try :
775798 yield os .getcwd ()
776799 finally :
777800 os .chdir (saved_dir )
778- if is_temporary :
779- rmtree (name )
780801
781802
803+ @contextlib .contextmanager
804+ def temp_cwd (name = 'tempcwd' , quiet = False ):
805+ """
806+ Context manager that temporarily creates and changes the CWD.
807+
808+ The function temporarily changes the current working directory
809+ after creating a temporary directory in the current directory with
810+ name *name*. If *name* is None, the temporary directory is
811+ created using tempfile.mkdtemp.
812+
813+ If *quiet* is False (default) and it is not possible to
814+ create or change the CWD, an error is raised. If *quiet* is True,
815+ only a warning is raised and the original CWD is used.
816+
817+ """
818+ with temp_dir (path = name , quiet = quiet ) as temp_path :
819+ with change_cwd (temp_path , quiet = quiet ) as cwd_dir :
820+ yield cwd_dir
821+
782822if hasattr (os , "umask" ):
783823 @contextlib .contextmanager
784824 def temp_umask (umask ):
0 commit comments