Merged revisions 78247 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78247 | ezio.melotti | 2010-02-20 10:09:39 +0200 (Sat, 20 Feb 2010) | 1 line

  #3426: os.path.abspath now returns unicode when its arg is unicode.
........
This commit is contained in:
Ezio Melotti 2010-02-20 09:16:04 +00:00
parent aaa210e2fd
commit 502f8eb50b
8 changed files with 76 additions and 5 deletions

View File

@ -186,7 +186,11 @@ def walk(top, func, arg):
def abspath(path): def abspath(path):
"""Return an absolute path.""" """Return an absolute path."""
if not isabs(path): if not isabs(path):
path = join(os.getcwd(), path) if isinstance(path, unicode):
cwd = os.getcwdu()
else:
cwd = os.getcwd()
path = join(cwd, path)
return normpath(path) return normpath(path)
# realpath is a no-op on systems without islink support # realpath is a no-op on systems without islink support

View File

@ -449,7 +449,11 @@ except ImportError: # not running on Windows - mock up something sensible
def abspath(path): def abspath(path):
"""Return the absolute version of a path.""" """Return the absolute version of a path."""
if not isabs(path): if not isabs(path):
path = join(os.getcwd(), path) if isinstance(path, unicode):
cwd = os.getcwdu()
else:
cwd = os.getcwd()
path = join(cwd, path)
return normpath(path) return normpath(path)
else: # use native Windows method on Windows else: # use native Windows method on Windows
@ -461,6 +465,8 @@ else: # use native Windows method on Windows
path = _getfullpathname(path) path = _getfullpathname(path)
except WindowsError: except WindowsError:
pass # Bad path - return unchanged. pass # Bad path - return unchanged.
elif isinstance(path, unicode):
path = os.getcwdu()
else: else:
path = os.getcwd() path = os.getcwd()
return normpath(path) return normpath(path)

View File

@ -146,7 +146,11 @@ def normpath(path):
def abspath(path): def abspath(path):
"""Return the absolute version of a path""" """Return the absolute version of a path"""
if not isabs(path): if not isabs(path):
path = join(os.getcwd(), path) if isinstance(path, unicode):
cwd = os.getcwdu()
else:
cwd = os.getcwd()
path = join(cwd, path)
return normpath(path) return normpath(path)
# realpath is a no-op on systems without islink support # realpath is a no-op on systems without islink support

View File

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

View File

@ -1,3 +1,4 @@
import os
import macpath import macpath
from test import test_support from test import test_support
import unittest import unittest
@ -8,6 +9,22 @@ class MacPathTestCase(unittest.TestCase):
def test_abspath(self): def test_abspath(self):
self.assert_(macpath.abspath("xx:yy") == "xx:yy") self.assert_(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
saved_cwd = os.getcwd()
for cwd in (u'cwd', u'\xe7w\xf0'):
try:
os.mkdir(cwd)
os.chdir(cwd)
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
self.assertTrue(isinstance(macpath.abspath(path), str))
for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
self.assertTrue(isinstance(macpath.abspath(upath), unicode))
finally:
os.chdir(saved_cwd)
os.rmdir(cwd)
def test_isabs(self): def test_isabs(self):
isabs = macpath.isabs isabs = macpath.isabs
self.assert_(isabs("xx:yy")) self.assert_(isabs("xx:yy"))

View File

@ -164,13 +164,32 @@ class TestNtpath(unittest.TestCase):
# the rest of the tests for the ntpath module to be run to completion # the rest of the tests for the ntpath module to be run to completion
# on any platform, since most of the module is intended to be usable # on any platform, since most of the module is intended to be usable
# from any platform. # from any platform.
# XXX this needs more tests
try: try:
import nt import nt
except ImportError: except ImportError:
pass # check that the function is there even if we are not on Windows
ntpath.abspath
else: else:
tester('ntpath.abspath("C:\\")', "C:\\") 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
saved_cwd = os.getcwd()
for cwd in (u'cwd', u'\xe7w\xf0'):
try:
os.mkdir(cwd)
os.chdir(cwd)
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
self.assertTrue(isinstance(ntpath.abspath(path), str))
for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
self.assertTrue(isinstance(ntpath.abspath(upath),
unicode))
finally:
os.chdir(saved_cwd)
os.rmdir(cwd)
def test_relpath(self): def test_relpath(self):
currentdir = os.path.split(os.getcwd())[-1] currentdir = os.path.split(os.getcwd())[-1]
tester('ntpath.relpath("a")', 'a') tester('ntpath.relpath("a")', 'a')

View File

@ -390,6 +390,21 @@ class PosixPathTest(unittest.TestCase):
def test_abspath(self): def test_abspath(self):
self.assert_("foo" in posixpath.abspath("foo")) self.assert_("foo" in 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
saved_cwd = os.getcwd()
for cwd in (u'cwd', u'\xe7w\xf0'):
try:
os.mkdir(cwd)
os.chdir(cwd)
for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
self.assertTrue(isinstance(posixpath.abspath(path), str))
for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
self.assertTrue(isinstance(posixpath.abspath(upath), unicode))
finally:
os.chdir(saved_cwd)
os.rmdir(cwd)
self.assertRaises(TypeError, posixpath.abspath) self.assertRaises(TypeError, posixpath.abspath)
def test_realpath(self): def test_realpath(self):

View File

@ -66,6 +66,8 @@ Core and Builtins
Library Library
------- -------
- Issue #3426: ``os.path.abspath`` now returns unicode when its arg is unicode.
- Issue #7835: shelve should no longer produce mysterious warnings during - Issue #7835: shelve should no longer produce mysterious warnings during
interpreter shutdown. interpreter shutdown.