Index: Lib/test/test_support.py =================================================================== --- Lib/test/test_support.py (revision 78250) +++ Lib/test/test_support.py (working copy) @@ -416,6 +416,38 @@ rmtree(name) +def any_cwd(func=None, unicode_only=False): + """Run the decorated test from any directory. + + If optional argument "unicode_only" is True, then the test is + restricted to cwd which can be decoded using filesystem encoding. + + Example: + + @any_cwd + def test_something(): + pass + """ + cwdirs = [TESTFN_UNICODE.encode(TESTFN_ENCODING or "ascii", "replace")] + if not unicode_only: + cwdirs.extend([TESTFN_UNICODE.encode('latin-1'), + TESTFN_UNICODE.encode('utf-8')]) + def decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + # First run from the current directory + func(*args, **kwargs) + # Then run from non-ASCII directories + for cwd in cwdirs: + with temp_cwd(cwd): + func(*args, **kwargs) + return wrapper + if func is None: + return decorator + else: + return decorator(func) + + def findfile(file, here=__file__): """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 Index: Lib/test/test_macpath.py =================================================================== --- Lib/test/test_macpath.py (revision 78250) +++ Lib/test/test_macpath.py (working copy) @@ -5,18 +5,21 @@ class MacPathTestCase(unittest.TestCase): + @test_support.any_cwd def test_abspath(self): self.assertEqual(macpath.abspath("xx:yy"), "xx:yy") - # Issue 3426: check that abspath retuns unicode when the arg is unicode - # and str when it's str, with both ASCII and non-ASCII cwds - for cwd in (u'cwd', u'\xe7w\xf0'): - with test_support.temp_cwd(cwd): - for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): - self.assertIsInstance(macpath.abspath(path), str) - for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'): - self.assertIsInstance(macpath.abspath(upath), unicode) - + # Issue 3426: check that abspath returns bytes when the arg is bytes + for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): + self.assertIsInstance(macpath.abspath(path), str) + + self.assertRaises(TypeError, macpath.abspath) + + @test_support.any_cwd(unicode_only=True) + def test_abspath_unicode(self): + # Issue 3426: check that abspath returns unicode when the arg is unicode + for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'): + self.assertIsInstance(macpath.abspath(path), unicode) def test_isabs(self): isabs = macpath.isabs Index: Lib/test/test_ntpath.py =================================================================== --- Lib/test/test_ntpath.py (revision 78250) +++ Lib/test/test_ntpath.py (working copy) @@ -154,6 +154,7 @@ tester('ntpath.expandvars("%foo%%bar")', "bar%bar") tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar") + @test_support.any_cwd def test_abspath(self): # ntpath.abspath() can only be used on a system with the "nt" module # (reasonably), so we protect this test with "import nt". This allows @@ -169,15 +170,20 @@ else: tester('ntpath.abspath("C:\\")', "C:\\") - # Issue 3426: check that abspath retuns unicode when the arg is - # unicode and str when it's str, with both ASCII and non-ASCII cwds - for cwd in (u'cwd', u'\xe7w\xf0'): - with test_support.temp_cwd(cwd): - for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): - self.assertIsInstance(ntpath.abspath(path), str) - for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'): - self.assertIsInstance(ntpath.abspath(upath), unicode) - + # Issue 3426: check that abspath returns bytes when the arg is bytes + for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): + self.assertIsInstance(ntpath.abspath(path), str) + + self.assertRaises(TypeError, ntpath.abspath) + + @test_support.any_cwd(unicode_only=True) + def test_abspath_unicode(self): + # Can only be used on a system with the "nt" module + test_support.import_module("nt") + + # Issue 3426: check that abspath returns unicode when the arg is unicode + for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'): + self.assertIsInstance(ntpath.abspath(path), unicode) def test_relpath(self): currentdir = os.path.split(os.getcwd())[-1] Index: Lib/test/test_posixpath.py =================================================================== --- Lib/test/test_posixpath.py (revision 78250) +++ Lib/test/test_posixpath.py (working copy) @@ -380,20 +380,22 @@ self.assertRaises(TypeError, posixpath.normpath) + @test_support.any_cwd def test_abspath(self): self.assertIn("foo", posixpath.abspath("foo")) - # Issue 3426: check that abspath retuns unicode when the arg is unicode - # and str when it's str, with both ASCII and non-ASCII cwds - for cwd in (u'cwd', u'\xe7w\xf0'): - with test_support.temp_cwd(cwd): - for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): - self.assertIsInstance(posixpath.abspath(path), str) - for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'): - self.assertIsInstance(posixpath.abspath(upath), unicode) + # Issue 3426: check that abspath returns bytes when the arg is bytes + for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): + self.assertIsInstance(posixpath.abspath(path), str) self.assertRaises(TypeError, posixpath.abspath) + @test_support.any_cwd(unicode_only=True) + def test_abspath_unicode(self): + # Issue 3426: check that abspath returns unicode when the arg is unicode + for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'): + self.assertIsInstance(posixpath.abspath(path), unicode) + def test_realpath(self): self.assertIn("foo", realpath("foo")) self.assertRaises(TypeError, posixpath.realpath)