[Jython-checkins] jython: Added a method to test_support.

frank.wierzbicki jython-checkins at python.org
Fri Mar 23 06:38:07 CET 2012


http://hg.python.org/jython/rev/e82f8c36e441
changeset:   6491:e82f8c36e441
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Thu Mar 22 22:38:00 2012 -0700
summary:
  Added a method to test_support.

files:
  Lib/test/test_support.py |  47 ++++++++++++++++++++++++++++
  1 files changed, 47 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -6,6 +6,7 @@
 import contextlib
 import errno
 import functools
+import gc
 import socket
 import sys
 import os
@@ -14,7 +15,9 @@
 import warnings
 import unittest
 import importlib
+import UserDict
 import re
+import time
 try:
     import thread
 except ImportError:
@@ -445,6 +448,50 @@
     unlink(TESTFN)
 del fp
 
+# Disambiguate TESTFN for parallel testing, while letting it remain a valid
+# module name.
+TESTFN = "{}_{}_tmp".format(TESTFN, "1") #XXX "1" is a dummy for os.getpid()
+
+# Save the initial cwd
+SAVEDCWD = os.getcwd()
+
+ at contextlib.contextmanager
+def temp_cwd(name='tempcwd', quiet=False):
+    """
+    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.
+    """
+    if 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)
+
+
+
 def findfile(file, here=__file__, subdir=None):
     """Try to find a file on sys.path and the working directory.  If it is not
     found the argument passed to the function is returned (this does not

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list