]> granicus.if.org Git - python/commitdiff
bpo-30427: eliminate redundant type checks in os.path.normcase() (GH-1712)
authorWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Thu, 28 Mar 2019 21:47:18 +0000 (22:47 +0100)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 28 Mar 2019 21:47:18 +0000 (14:47 -0700)
https://bugs.python.org/issue30427

Lib/ntpath.py
Lib/posixpath.py
Misc/NEWS.d/next/Library/2019-03-28-21-17-08.bpo-30427.lxzvbw.rst [new file with mode: 0644]

index b5e1d121fc574e9d0be33b5764e6087000caf540..f3cfabf0238ef574fc0c14bf36ecf28a4ca0c447 100644 (file)
@@ -46,16 +46,10 @@ def normcase(s):
 
     Makes all characters lowercase and all slashes into backslashes."""
     s = os.fspath(s)
-    try:
-        if isinstance(s, bytes):
-            return s.replace(b'/', b'\\').lower()
-        else:
-            return s.replace('/', '\\').lower()
-    except (TypeError, AttributeError):
-        if not isinstance(s, (bytes, str)):
-            raise TypeError("normcase() argument must be str or bytes, "
-                            "not %r" % s.__class__.__name__) from None
-        raise
+    if isinstance(s, bytes):
+        return s.replace(b'/', b'\\').lower()
+    else:
+        return s.replace('/', '\\').lower()
 
 
 # Return whether a path is absolute.
index 12ab2eadbf9b199085490633bfc4c0dcdf2c3dd5..21ce72fd79cdef561ab77d1b573447e19dafc47f 100644 (file)
@@ -51,11 +51,7 @@ def _get_sep(path):
 
 def normcase(s):
     """Normalize case of pathname.  Has no effect under Posix"""
-    s = os.fspath(s)
-    if not isinstance(s, (bytes, str)):
-        raise TypeError("normcase() argument must be str or bytes, "
-                        "not '{}'".format(s.__class__.__name__))
-    return s
+    return os.fspath(s)
 
 
 # Return whether a path is absolute.
diff --git a/Misc/NEWS.d/next/Library/2019-03-28-21-17-08.bpo-30427.lxzvbw.rst b/Misc/NEWS.d/next/Library/2019-03-28-21-17-08.bpo-30427.lxzvbw.rst
new file mode 100644 (file)
index 0000000..80e7c4a
--- /dev/null
@@ -0,0 +1,2 @@
+``os.path.normcase()`` relies on ``os.fspath()`` to check the type of its argument. Redundant checks have been removed from its ``posixpath.normcase()`` and ``ntpath.normcase()`` implementations.
+Patch by Wolfgang Maier.