◐ Shell
clean mode source ↗

Issue 7669: test_unicode_file fails with non-ascii path

/tmp/py2u…→unicode $ ./python Lib/test/regrtest.py test_unicode_file
test_unicode_file
test test_unicode_file failed -- Traceback (most recent call last):
  File "/tmp/py2u…→unicode/Lib/test/test_unicode_file.py", line 173, in test_single_files
    self._test_single(TESTFN_UNICODE)
  File "/tmp/py2u…→unicode/Lib/test/test_unicode_file.py", line 147, in _test_single
    self._do_single(filename)
  File "/tmp/py2u…→unicode/Lib/test/test_unicode_file.py", line 48, in _do_single
    self.assertTrue(os.path.exists(os.path.abspath(filename)))
  File "/tmp/py2u…→unicode/Lib/posixpath.py", line 338, in abspath
    path = join(os.getcwd(), path)
  File "/tmp/py2u…→unicode/Lib/posixpath.py", line 70, in join
    path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 29: ordinal not in range(128)

1 test failed:
    test_unicode_file
This is because in "path = join(os.getcwd(), path)" os.getcwd() returns a non-ascii byte string and path is unicode, so the cwd is implicitly decoded with the ascii codec in join and the error is raised.
Using getcwdu() when the path is unicode seems to fix the problem:

 def abspath(path):
     """Return an absolute path."""
     if not isabs(path):
-        path = join(os.getcwd(), path)
+        if isinstance(path, unicode):
+            path = join(os.getcwdu(), path)
+        else:
+            path = join(os.getcwd(), path)
     return normpath(path)