]> granicus.if.org Git - python/commitdiff
Merged revisions 77442 via svnmerge from
authorEzio Melotti <ezio.melotti@gmail.com>
Tue, 12 Jan 2010 03:38:53 +0000 (03:38 +0000)
committerEzio Melotti <ezio.melotti@gmail.com>
Tue, 12 Jan 2010 03:38:53 +0000 (03:38 +0000)
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
........

Lib/ntpath.py
Lib/posixpath.py
Lib/test/test_ntpath.py
Lib/test/test_posixpath.py
Misc/NEWS

index 37f32f019d54e18558952645b5ffc8b07bf95e8e..02d8584736dea686276e668f66432c35bfecab87 100644 (file)
@@ -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.
index cdce5afedecf21931d089bd430427dd199e54bf3..35dcffbe88b6d6e68ac2a9b0b70c0f6033e7a9cf 100644 (file)
@@ -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):
index a077c78ff1376a5bd6b184320cee0db5b0e5c23e..10bbe3a3a2634836fb8f5197c76403e91ea1b255 100644 (file)
@@ -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:
index b7fbd50dbb822f15f582712919f4e11eea32f4cb..f6cb047da5e149820c46d7ae0f46c2e759594dc9 100644 (file)
@@ -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):
index 2ca4c0b349e492d604b06ef8a5e73e1ab2f7e587..9386a18142b05a2b46e24df74e026ba034346096 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -48,6 +48,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5827: Make sure that normpath preserves unicode.  Initial patch
+  by Matt Giuca.
+
 - Issue #5372: Drop the reuse of .o files in Distutils' ccompiler (since
   Extension extra options may change the output without changing the .c
   file). Initial patch by Collin Winter.