@@ -738,45 +738,85 @@ def _is_ipv6_enabled():
738738SAVEDCWD = os .getcwd ()
739739
740740@contextlib .contextmanager
741- def temp_cwd (name = 'tempcwd' , quiet = False , path = None ):
742- """
743- Context manager that temporarily changes the CWD.
741+ def temp_dir (path = None , quiet = False ):
742+ """Return a context manager that creates a temporary directory.
743+
744+ Arguments:
745+
746+ path: the directory to create temporarily. If omitted or None,
747+ defaults to creating a temporary directory using tempfile.mkdtemp.
744748
745- An existing path may be provided as *path*, in which case this
746- function makes no changes to the file system.
749+ quiet: if False (the default), the context manager raises an exception
750+ on error. Otherwise, if the path is specified and cannot be
751+ created, only a warning is issued.
747752
748- Otherwise, the new CWD is created in the current directory and it's
749- named *name*. If *quiet* is False (default) and it's not possible to
750- create or change the CWD, an error is raised. If it's True, only a
751- warning is raised and the original CWD is used.
752753 """
753- saved_dir = os .getcwd ()
754- is_temporary = False
754+ dir_created = False
755755 if path is None :
756- path = name
756+ path = tempfile .mkdtemp ()
757+ dir_created = True
758+ path = os .path .realpath (path )
759+ else :
757760 try :
758- os .mkdir (name )
759- is_temporary = True
761+ os .mkdir (path )
762+ dir_created = True
760763 except OSError :
761764 if not quiet :
762765 raise
763- warnings .warn ('tests may fail, unable to create temp CWD ' + name ,
766+ warnings .warn ('tests may fail, unable to create temp dir: ' + path ,
764767 RuntimeWarning , stacklevel = 3 )
768+ try :
769+ yield path
770+ finally :
771+ if dir_created :
772+ shutil .rmtree (path )
773+
774+ @contextlib .contextmanager
775+ def change_cwd (path , quiet = False ):
776+ """Return a context manager that changes the current working directory.
777+
778+ Arguments:
779+
780+ path: the directory to use as the temporary current working directory.
781+
782+ quiet: if False (the default), the context manager raises an exception
783+ on error. Otherwise, it issues only a warning and keeps the current
784+ working directory the same.
785+
786+ """
787+ saved_dir = os .getcwd ()
765788 try :
766789 os .chdir (path )
767790 except OSError :
768791 if not quiet :
769792 raise
770- warnings .warn ('tests may fail, unable to change the CWD to ' + path ,
793+ warnings .warn ('tests may fail, unable to change CWD to: ' + path ,
771794 RuntimeWarning , stacklevel = 3 )
772795 try :
773796 yield os .getcwd ()
774797 finally :
775798 os .chdir (saved_dir )
776- if is_temporary :
777- rmtree (name )
778799
779800
801+ @contextlib .contextmanager
802+ def temp_cwd (name = 'tempcwd' , quiet = False ):
803+ """
804+ Context manager that temporarily creates and changes the CWD.
805+
806+ The function temporarily changes the current working directory
807+ after creating a temporary directory in the current directory with
808+ name *name*. If *name* is None, the temporary directory is
809+ created using tempfile.mkdtemp.
810+
811+ If *quiet* is False (default) and it is not possible to
812+ create or change the CWD, an error is raised. If *quiet* is True,
813+ only a warning is raised and the original CWD is used.
814+
815+ """
816+ with temp_dir (path = name , quiet = quiet ) as temp_path :
817+ with change_cwd (temp_path , quiet = quiet ) as cwd_dir :
818+ yield cwd_dir
819+
780820if hasattr (os , "umask" ):
781821 @contextlib .contextmanager
782822 def temp_umask (umask ):
0 commit comments