[Python-checkins] r77442 - in python/trunk: Lib/ntpath.py Lib/posixpath.py Lib/test/test_ntpath.py Lib/test/test_posixpath.py Misc/NEWS

ezio.melotti python-checkins at python.org
Tue Jan 12 04:32:05 CET 2010


Author: ezio.melotti
Date: Tue Jan 12 04:32:05 2010
New Revision: 77442

Log:
#5827: make sure that normpath preserves unicode

Modified:
   python/trunk/Lib/ntpath.py
   python/trunk/Lib/posixpath.py
   python/trunk/Lib/test/test_ntpath.py
   python/trunk/Lib/test/test_posixpath.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/ntpath.py
==============================================================================
--- python/trunk/Lib/ntpath.py	(original)
+++ python/trunk/Lib/ntpath.py	Tue Jan 12 04:32:05 2010
@@ -397,6 +397,8 @@
 
 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 @@
     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 @@
             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.

Modified: python/trunk/Lib/posixpath.py
==============================================================================
--- python/trunk/Lib/posixpath.py	(original)
+++ python/trunk/Lib/posixpath.py	Tue Jan 12 04:32:05 2010
@@ -307,8 +307,10 @@
 
 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 @@
         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):

Modified: python/trunk/Lib/test/test_ntpath.py
==============================================================================
--- python/trunk/Lib/test/test_ntpath.py	(original)
+++ python/trunk/Lib/test/test_ntpath.py	Tue Jan 12 04:32:05 2010
@@ -123,6 +123,11 @@
         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):
         with test_support.EnvironmentVarGuard() as env:
             env.clear()

Modified: python/trunk/Lib/test/test_posixpath.py
==============================================================================
--- python/trunk/Lib/test/test_posixpath.py	(original)
+++ python/trunk/Lib/test/test_posixpath.py	Tue Jan 12 04:32:05 2010
@@ -381,6 +381,11 @@
         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):

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Jan 12 04:32:05 2010
@@ -23,6 +23,9 @@
 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.


More information about the Python-checkins mailing list