summaryrefslogtreecommitdiffstats
path: root/Lib/test/support.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2010-08-17 13:06:11 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2010-08-17 13:06:11 (GMT)
commitd26c18adccf02321592cd58a2dadb0ab68af6906 (patch)
tree0429b57ab0d5cc0b36ca68af24f382c888cbcf9c /Lib/test/support.py
parent46e63805638e0fac20aeae837e1f93b4a675446a (diff)
downloadcpython-d26c18adccf02321592cd58a2dadb0ab68af6906.zip
cpython-d26c18adccf02321592cd58a2dadb0ab68af6906.tar.gz
cpython-d26c18adccf02321592cd58a2dadb0ab68af6906.tar.bz2
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Diffstat (limited to 'Lib/test/support.py')
-rw-r--r--Lib/test/support.py29
1 files changed, 20 insertions, 9 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py
index c69dd94..8a8e410 100644
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -419,21 +419,32 @@ elif sys.platform != 'darwin':
SAVEDCWD = os.getcwd()
@contextlib.contextmanager
-def temp_cwd(name='tempcwd', quiet=False):
+def temp_cwd(name='tempcwd', quiet=False, path=None):
"""
- Context manager that creates a temporary directory and set it as CWD.
+ Context manager that temporarily changes the 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.
+ An existing path may be provided as *path*, in which case this
+ function makes no changes to the file system.
+
+ Otherwise, 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.
"""
saved_dir = os.getcwd()
is_temporary = False
+ if path is None:
+ path = name
+ try:
+ os.mkdir(name)
+ is_temporary = True
+ except OSError:
+ if not quiet:
+ raise
+ warnings.warn('tests may fail, unable to create temp CWD ' + name,
+ RuntimeWarning, stacklevel=3)
try:
- os.mkdir(name)
- os.chdir(name)
- is_temporary = True
+ os.chdir(path)
except OSError:
if not quiet:
raise