Commits

Anonymous committed 0c826e2

Add is_reserved().

  • Participants
  • Parent commits 8f0f460
  • Branches pathlib

Comments (0)

Files changed (2)

     )
     ext_namespace_prefix = '\\\\?\\'
 
+    reserved_names = (
+        {'CON', 'PRN', 'AUX', 'NUL'} |
+        {'COM%d' % i for i in range(1, 10)} |
+        {'LPT%d' % i for i in range(1, 10)}
+        )
+
     def splitroot(self, part, sep=sep):
         first = part[0:1]
         second = part[1:2]
                 s = '\\' + s[3:]
         return s
 
+    def is_reserved(self, parts):
+        reserved_names = self.reserved_names
+        for p in parts:
+            if p.partition('.')[0].upper() in reserved_names:
+                return True
+        return False
+
 
 _NO_FD = getattr(os, "AT_FDCWD", -100)
 
             close(resolved_fd)
         return resolved or sep
 
+    def is_reserved(self, parts):
+        return False
+
 
 _nt_flavour = _NTFlavour()
 _posix_flavour = _PosixFlavour()
         parts = fix(self._parts)
         return self._from_parsed_parts(drv, root, parts)
 
+    def is_reserved(self):
+        """Return True if the path contains one of the special names reserved
+        by the system, if any."""
+        return self._flavour.is_reserved(self._parts)
+
 
 class PurePosixPath(PurePath):
     _flavour = _posix_flavour

Lib/test/test_pathlib.py

         self.assertEqual(P('/Aa/Bb/Cc'), p)
         self.assertEqual('/Aa/Bb/Cc', str(p))
 
+    def test_is_reserved(self):
+        P = self.cls
+        self.assertIs(False, P('').is_reserved())
+        self.assertIs(False, P('/').is_reserved())
+        self.assertIs(False, P('/foo/bar').is_reserved())
+        self.assertIs(False, P('/dev/con/PRN/NUL').is_reserved())
+
 
 class PureNTPathTest(_BasePurePathTest):
     cls = pathlib.PureNTPath
         self.assertEqual(P('d:/aa/bb/cc'), p)
         self.assertEqual(r'd:\aa\bb\cc', str(p))
 
+    def test_is_reserved(self):
+        P = self.cls
+        self.assertIs(False, P('').is_reserved())
+        self.assertIs(False, P('/').is_reserved())
+        self.assertIs(False, P('/foo/bar').is_reserved())
+        self.assertIs(True, P('con').is_reserved())
+        self.assertIs(True, P('NUL').is_reserved())
+        self.assertIs(True, P('NUL.txt').is_reserved())
+        self.assertIs(True, P('com1').is_reserved())
+        self.assertIs(True, P('com9.bar').is_reserved())
+        self.assertIs(False, P('bar.com9').is_reserved())
+        self.assertIs(True, P('lpt1').is_reserved())
+        self.assertIs(True, P('lpt9.bar').is_reserved())
+        self.assertIs(False, P('bar.lpt9').is_reserved())
+        self.assertIs(True, P('c:\foo\con.bar\baz').is_reserved())
+        self.assertIs(True, P('c:\foo\lpt9.bar\baz').is_reserved())
+        self.assertIs(True, P('\\my\share\lpt9.bar\baz').is_reserved())
+
 
 class PurePathTest(_BasePurePathTest):
     cls = pathlib.PurePath