Commits

Anonymous committed bf2c6de

Add PurePath.normcase()

  • Participants
  • Parent commits 13cae0f
  • Branches pathlib

Comments (0)

Files changed (2)

File Lib/pathlib.py

             return False
         return not self._flavour.has_drv or bool(self._drv)
 
+    def normcase(self):
+        """Return this path, possibly lowercased if the path flavour has
+        case-insensitive path semantics.
+        Calling this method is not needed before comparing Path instances."""
+        fix = self._flavour.casefold_parts
+        drv, = fix((self._drv,))
+        root = self._root
+        parts = fix(self._parts)
+        return self._from_parsed_parts(drv, root, parts)
+
 
 class PurePosixPath(PurePath):
     _flavour = _posix_flavour

File Lib/test/test_pathlib.py

         self.assertTrue(P('/a').is_absolute())
         self.assertTrue(P('/a/b/').is_absolute())
 
+    def test_normcase(self):
+        P = self.cls
+        p = P('/Aa/Bb/Cc').normcase()
+        self.assertEqual(P('/Aa/Bb/Cc'), p)
+        self.assertEqual('/Aa/Bb/Cc', str(p))
+
 
 class PureNTPathTest(_BasePurePathTest):
     cls = pathlib.PureNTPath
         self.assertTrue(P('//a/b/c').is_absolute())
         self.assertTrue(P('//a/b/c/d').is_absolute())
 
+    def test_normcase(self):
+        P = self.cls
+        p = P('D:/Aa/Bb/Cc').normcase()
+        self.assertEqual(P('d:/aa/bb/cc'), p)
+        self.assertEqual(r'd:\aa\bb\cc', str(p))
+
 
 class PurePathTest(_BasePurePathTest):
     cls = pathlib.PurePath