Skip to content

Commit 5a3ef5b

Browse files
committed
#9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
1 parent 6186bfb commit 5a3ef5b

7 files changed

Lines changed: 28 additions & 8 deletions

File tree

Doc/library/os.path.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ applications should use string objects to access all files.
201201
Normalize the case of a pathname. On Unix and Mac OS X, this returns the
202202
path unchanged; on case-insensitive filesystems, it converts the path to
203203
lowercase. On Windows, it also converts forward slashes to backward slashes.
204+
Raise a TypeError if the type of *path* is not ``str`` or ``bytes``.
204205

205206

206207
.. function:: normpath(path)

Lib/macpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def _get_colon(path):
3232
# Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
3333

3434
def normcase(path):
35+
if not isinstance(path, (bytes, str)):
36+
raise TypeError("normcase() argument must be str or bytes, "
37+
"not '{}'".format(path.__class__.__name__))
3538
return path.lower()
3639

3740

Lib/ntpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ def normcase(s):
7878
"""Normalize case of pathname.
7979
8080
Makes all characters lowercase and all slashes into backslashes."""
81+
if not isinstance(s, (bytes, str)):
82+
raise TypeError("normcase() argument must be str or bytes, "
83+
"not '{}'".format(s.__class__.__name__))
8184
return s.replace(_get_altsep(s), _get_sep(s)).lower()
8285

8386

Lib/os2emxpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def normcase(s):
3636
"""Normalize case of pathname.
3737
3838
Makes all characters lowercase and all altseps into seps."""
39+
if not isinstance(s, (bytes, str)):
40+
raise TypeError("normcase() argument must be str or bytes, "
41+
"not '{}'".format(s.__class__.__name__))
3942
return s.replace('\\', '/').lower()
4043

4144

Lib/posixpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def _get_sep(path):
4949
def normcase(s):
5050
"""Normalize case of pathname. Has no effect under Posix"""
5151
# TODO: on Mac OS X, this should really return s.lower().
52+
if not isinstance(s, (bytes, str)):
53+
raise TypeError("normcase() argument must be str or bytes, "
54+
"not '{}'".format(s.__class__.__name__))
5255
return s
5356

5457

Lib/test/test_genericpath.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,18 @@ class CommonTest(GenericTest):
194194
]
195195

196196
def test_normcase(self):
197-
# Check that normcase() is idempotent
198-
p = "FoO/./BaR"
199-
p = self.pathmodule.normcase(p)
200-
self.assertEqual(p, self.pathmodule.normcase(p))
201-
202-
p = b"FoO/./BaR"
203-
p = self.pathmodule.normcase(p)
204-
self.assertEqual(p, self.pathmodule.normcase(p))
197+
normcase = self.pathmodule.normcase
198+
# check that normcase() is idempotent
199+
for p in ["FoO/./BaR", b"FoO/./BaR"]:
200+
p = normcase(p)
201+
self.assertEqual(p, normcase(p))
202+
203+
self.assertEqual(normcase(''), '')
204+
self.assertEqual(normcase(b''), b'')
205+
206+
# check that normcase raises a TypeError for invalid types
207+
for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
208+
self.assertRaises(TypeError, normcase, path)
205209

206210
def test_splitdrive(self):
207211
# splitdrive for non-NT paths

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ C-API
454454
Library
455455
-------
456456

457+
- Issue #9018: os.path.normcase() now raises a TypeError if the argument is
458+
not ``str`` or ``bytes``.
459+
457460
- Issue #9075: In the ssl module, remove the setting of a ``debug`` flag
458461
on an OpenSSL structure.
459462

0 commit comments

Comments
 (0)