summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2010-01-12 03:38:53 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2010-01-12 03:38:53 (GMT)
commit698037a232543437a6ac0ef3ab5067da2930dda0 (patch)
tree2db2a3ee945f9668e0d092374358109782e5ba25 /Lib
parent0d54bd5122b9787ff5bbbf06003df40c9e7809a6 (diff)
downloadcpython-698037a232543437a6ac0ef3ab5067da2930dda0.zip
cpython-698037a232543437a6ac0ef3ab5067da2930dda0.tar.gz
cpython-698037a232543437a6ac0ef3ab5067da2930dda0.tar.bz2
Merged revisions 77442 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r77442 | ezio.melotti | 2010-01-12 05:32:05 +0200 (Tue, 12 Jan 2010) | 1 line #5827: make sure that normpath preserves unicode ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ntpath.py10
-rw-r--r--Lib/posixpath.py10
-rw-r--r--Lib/test/test_ntpath.py5
-rw-r--r--Lib/test/test_posixpath.py5
4 files changed, 22 insertions, 8 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 37f32f0..02d8584 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -397,6 +397,8 @@ def expandvars(path):
def normpath(path):
"""Normalize path, eliminating double slashes, etc."""
+ # Preserve unicode (if path is unicode)
+ backslash, dot = (u'\\', u'.') if isinstance(path, unicode) else ('\\', '.')
path = path.replace("/", "\\")
prefix, path = splitdrive(path)
# We need to be careful here. If the prefix is empty, and the path starts
@@ -411,12 +413,12 @@ def normpath(path):
if prefix == '':
# No drive letter - preserve initial backslashes
while path[:1] == "\\":
- prefix = prefix + "\\"
+ prefix = prefix + backslash
path = path[1:]
else:
# We have a drive letter - collapse initial backslashes
if path.startswith("\\"):
- prefix = prefix + "\\"
+ prefix = prefix + backslash
path = path.lstrip("\\")
comps = path.split("\\")
i = 0
@@ -435,8 +437,8 @@ def normpath(path):
i += 1
# If the path is now empty, substitute '.'
if not prefix and not comps:
- comps.append('.')
- return prefix + "\\".join(comps)
+ comps.append(dot)
+ return prefix + backslash.join(comps)
# Return an absolute path.
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index cdce5af..35dcffb 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -307,8 +307,10 @@ def expandvars(path):
def normpath(path):
"""Normalize path, eliminating double slashes, etc."""
+ # Preserve unicode (if path is unicode)
+ slash, dot = (u'/', u'.') if isinstance(path, unicode) else ('/', '.')
if path == '':
- return '.'
+ return dot
initial_slashes = path.startswith('/')
# POSIX allows one or two initial slashes, but treats three or more
# as single slash.
@@ -326,10 +328,10 @@ def normpath(path):
elif new_comps:
new_comps.pop()
comps = new_comps
- path = '/'.join(comps)
+ path = slash.join(comps)
if initial_slashes:
- path = '/'*initial_slashes + path
- return path or '.'
+ path = slash*initial_slashes + path
+ return path or dot
def abspath(path):
diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py
index a077c78..10bbe3a 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -123,6 +123,11 @@ class TestNtpath(unittest.TestCase):
tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
+ # Issue 5827: Make sure normpath preserves unicode
+ for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
+ self.assertTrue(isinstance(ntpath.normpath(path), unicode),
+ 'normpath() returned str instead of unicode')
+
def test_expandvars(self):
oldenv = os.environ.copy()
try:
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
index b7fbd50..f6cb047 100644
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -385,6 +385,11 @@ class PosixPathTest(unittest.TestCase):
self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
+ # Issue 5827: Make sure normpath preserves unicode
+ for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
+ self.assertTrue(isinstance(posixpath.normpath(path), unicode),
+ 'normpath() returned str instead of unicode')
+
self.assertRaises(TypeError, posixpath.normpath)
def test_abspath(self):