From 5a3ef5b22af607666111c76764db0efffbef82be Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Fri, 25 Jun 2010 10:56:11 +0000 Subject: [PATCH] #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes. --- Doc/library/os.path.rst | 1 + Lib/macpath.py | 3 +++ Lib/ntpath.py | 3 +++ Lib/os2emxpath.py | 3 +++ Lib/posixpath.py | 3 +++ Lib/test/test_genericpath.py | 20 ++++++++++++-------- Misc/NEWS | 3 +++ 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Doc/library/os.path.rst b/Doc/library/os.path.rst index 8c720bf60b..c838983d0e 100644 --- a/Doc/library/os.path.rst +++ b/Doc/library/os.path.rst @@ -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) diff --git a/Lib/macpath.py b/Lib/macpath.py index 3b3e4ff071..4ab7c09793 100644 --- a/Lib/macpath.py +++ b/Lib/macpath.py @@ -32,6 +32,9 @@ def _get_colon(path): # Normalize the case of a pathname. Dummy in Posix, but .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() diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 8d502942fa..7972aa92c0 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -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() diff --git a/Lib/os2emxpath.py b/Lib/os2emxpath.py index 184c9b169a..0ccbf8ae7b 100644 --- a/Lib/os2emxpath.py +++ b/Lib/os2emxpath.py @@ -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() diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 5783975e4a..667f5c52de 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -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 diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py index 9a0f2267f3..2955f49469 100644 --- a/Lib/test/test_genericpath.py +++ b/Lib/test/test_genericpath.py @@ -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 diff --git a/Misc/NEWS b/Misc/NEWS index b099046cee..c57ca2c3bb 100644 --- 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. -- 2.40.0