]> granicus.if.org Git - python/commitdiff
#9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
authorEzio Melotti <ezio.melotti@gmail.com>
Fri, 25 Jun 2010 10:56:11 +0000 (10:56 +0000)
committerEzio Melotti <ezio.melotti@gmail.com>
Fri, 25 Jun 2010 10:56:11 +0000 (10:56 +0000)
Doc/library/os.path.rst
Lib/macpath.py
Lib/ntpath.py
Lib/os2emxpath.py
Lib/posixpath.py
Lib/test/test_genericpath.py
Misc/NEWS

index 8c720bf60b71ea04f8a07a99f7207c17230925a2..c838983d0e2b282fc2d5f12e532fcd9b890262b7 100644 (file)
@@ -201,6 +201,7 @@ applications should use string objects to access all files.
    Normalize the case of a pathname.  On Unix and Mac OS X, this returns the
    path unchanged; on case-insensitive filesystems, it converts the path to
    lowercase.  On Windows, it also converts forward slashes to backward slashes.
+   Raise a TypeError if the type of *path* is not ``str`` or ``bytes``.
 
 
 .. function:: normpath(path)
index 3b3e4ff071694ef7869379823d9dd4944a2effa1..4ab7c09793c1129a8bd5054e20a4f5f3967f2926 100644 (file)
@@ -32,6 +32,9 @@ def _get_colon(path):
 # Normalize the case of a pathname.  Dummy in Posix, but <s>.lower() here.
 
 def normcase(path):
+    if not isinstance(path, (bytes, str)):
+        raise TypeError("normcase() argument must be str or bytes, "
+                        "not '{}'".format(path.__class__.__name__))
     return path.lower()
 
 
index 8d502942fa4614059b9fec72e568d6b870e83acc..7972aa92c0c3f6f5fa97d53a817d487ed9e21738 100644 (file)
@@ -78,6 +78,9 @@ def normcase(s):
     """Normalize case of pathname.
 
     Makes all characters lowercase and all slashes into backslashes."""
+    if not isinstance(s, (bytes, str)):
+        raise TypeError("normcase() argument must be str or bytes, "
+                        "not '{}'".format(s.__class__.__name__))
     return s.replace(_get_altsep(s), _get_sep(s)).lower()
 
 
index 184c9b169a8d9ed01060540dac00fbc42e7e1ff8..0ccbf8ae7b886a0462b1cfdc6891f7867216030a 100644 (file)
@@ -36,6 +36,9 @@ def normcase(s):
     """Normalize case of pathname.
 
     Makes all characters lowercase and all altseps into seps."""
+    if not isinstance(s, (bytes, str)):
+        raise TypeError("normcase() argument must be str or bytes, "
+                        "not '{}'".format(s.__class__.__name__))
     return s.replace('\\', '/').lower()
 
 
index 5783975e4ac9a5dddb6dbbf2f401c9a0b85a12e3..667f5c52de2e1b47743962eb018b06b06dd9291e 100644 (file)
@@ -49,6 +49,9 @@ def _get_sep(path):
 def normcase(s):
     """Normalize case of pathname.  Has no effect under Posix"""
     # TODO: on Mac OS X, this should really return s.lower().
+    if not isinstance(s, (bytes, str)):
+        raise TypeError("normcase() argument must be str or bytes, "
+                        "not '{}'".format(s.__class__.__name__))
     return s
 
 
index 9a0f2267f3d8d1b3a428f53628e8830fc871b7bd..2955f494693c1d8f3cf8f8c1e0c9dcf17fb544f9 100644 (file)
@@ -194,14 +194,18 @@ class CommonTest(GenericTest):
     ]
 
     def test_normcase(self):
-        # Check that normcase() is idempotent
-        p = "FoO/./BaR"
-        p = self.pathmodule.normcase(p)
-        self.assertEqual(p, self.pathmodule.normcase(p))
-
-        p = b"FoO/./BaR"
-        p = self.pathmodule.normcase(p)
-        self.assertEqual(p, self.pathmodule.normcase(p))
+        normcase = self.pathmodule.normcase
+        # check that normcase() is idempotent
+        for p in ["FoO/./BaR", b"FoO/./BaR"]:
+            p = normcase(p)
+            self.assertEqual(p, normcase(p))
+
+        self.assertEqual(normcase(''), '')
+        self.assertEqual(normcase(b''), b'')
+
+        # check that normcase raises a TypeError for invalid types
+        for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
+            self.assertRaises(TypeError, normcase, path)
 
     def test_splitdrive(self):
         # splitdrive for non-NT paths
index b099046cee0facf705718f126cb59afbb7861077..c57ca2c3bb002cafe23bfa9bd95210f6b826f0f4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -454,6 +454,9 @@ C-API
 Library
 -------
 
+- Issue #9018: os.path.normcase() now raises a TypeError if the argument is
+  not ``str`` or ``bytes``.
+
 - Issue #9075: In the ssl module, remove the setting of a ``debug`` flag
   on an OpenSSL structure.