[Python-checkins] bpo-27827: identify a greater range of reserved filename on Windows. (GH-26698) (#27422)
ambv
webhook-mailer at python.org
Wed Jul 28 11:15:56 EDT 2021
https://github.com/python/cpython/commit/debb751f11f5221eafcdf07a14c7e9408350ad9a
commit: debb751f11f5221eafcdf07a14c7e9408350ad9a
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-07-28T17:15:51+02:00
summary:
bpo-27827: identify a greater range of reserved filename on Windows. (GH-26698) (#27422)
`pathlib.PureWindowsPath.is_reserved()` now identifies as reserved
filenames with trailing spaces or colons.
Co-authored-by: Barney Gale <barney.gale at foundry.com>
Co-authored-by: Eryk Sun <eryksun at gmail.com>
(cherry picked from commit 56c1f6d7edad454f382d3ecb8cdcff24ac898a50)
files:
A Misc/NEWS.d/next/Library/2021-06-12-21-25-35.bpo-27827.TMWh1i.rst
M Lib/pathlib.py
M Lib/test/test_pathlib.py
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 60e2855ba1ea3..7aeda14a14120 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -132,16 +132,25 @@ class _WindowsFlavour(_Flavour):
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)}
+ {'CON', 'PRN', 'AUX', 'NUL', 'CONIN$', 'CONOUT$'} |
+ {'COM%s' % c for c in '123456789\xb9\xb2\xb3'} |
+ {'LPT%s' % c for c in '123456789\xb9\xb2\xb3'}
)
# Interesting findings about extended paths:
- # - '\\?\c:\a', '//?/c:\a' and '//?/c:/a' are all supported
- # but '\\?\c:/a' is not
- # - extended paths are always absolute; "relative" extended paths will
- # fail.
+ # * '\\?\c:\a' is an extended path, which bypasses normal Windows API
+ # path processing. Thus relative paths are not resolved and slash is not
+ # translated to backslash. It has the native NT path limit of 32767
+ # characters, but a bit less after resolving device symbolic links,
+ # such as '\??\C:' => '\Device\HarddiskVolume2'.
+ # * '\\?\c:/a' looks for a device named 'C:/a' because slash is a
+ # regular name character in the object namespace.
+ # * '\\?\c:\foo/bar' is invalid because '/' is illegal in NT filesystems.
+ # The only path separator at the filesystem level is backslash.
+ # * '//?/c:\a' and '//?/c:/a' are effectively equivalent to '\\.\c:\a' and
+ # thus limited to MAX_PATH.
+ # * Prior to Windows 8, ANSI API bytes paths are limited to MAX_PATH,
+ # even with the '\\?\' prefix.
def splitroot(self, part, sep=sep):
first = part[0:1]
@@ -231,15 +240,16 @@ def _ext_to_normal(self, s):
def is_reserved(self, parts):
# NOTE: the rules for reserved names seem somewhat complicated
- # (e.g. r"..\NUL" is reserved but not r"foo\NUL").
- # We err on the side of caution and return True for paths which are
- # not considered reserved by Windows.
+ # (e.g. r"..\NUL" is reserved but not r"foo\NUL" if "foo" does not
+ # exist). We err on the side of caution and return True for paths
+ # which are not considered reserved by Windows.
if not parts:
return False
if parts[0].startswith('\\\\'):
# UNC paths are never reserved
return False
- return parts[-1].partition('.')[0].upper() in self.reserved_names
+ name = parts[-1].partition('.')[0].partition(':')[0].rstrip(' ')
+ return name.upper() in self.reserved_names
def make_uri(self, path):
# Under Windows, file URIs use the UTF-8 encoding.
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 3da35710b9de4..0314938e30c14 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1248,19 +1248,35 @@ def test_is_reserved(self):
self.assertIs(False, P('').is_reserved())
self.assertIs(False, P('/').is_reserved())
self.assertIs(False, P('/foo/bar').is_reserved())
+ # UNC paths are never reserved.
+ self.assertIs(False, P('//my/share/nul/con/aux').is_reserved())
+ # Case-insenstive DOS-device names are reserved.
+ self.assertIs(True, P('nul').is_reserved())
+ self.assertIs(True, P('aux').is_reserved())
+ self.assertIs(True, P('prn').is_reserved())
self.assertIs(True, P('con').is_reserved())
- self.assertIs(True, P('NUL').is_reserved())
+ self.assertIs(True, P('conin$').is_reserved())
+ self.assertIs(True, P('conout$').is_reserved())
+ # COM/LPT + 1-9 or + superscript 1-3 are reserved.
+ self.assertIs(True, P('COM1').is_reserved())
+ self.assertIs(True, P('LPT9').is_reserved())
+ self.assertIs(True, P('com\xb9').is_reserved())
+ self.assertIs(True, P('com\xb2').is_reserved())
+ self.assertIs(True, P('lpt\xb3').is_reserved())
+ # DOS-device name mataching ignores characters after a dot or
+ # a colon and also ignores trailing spaces.
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(True, P('PRN ').is_reserved())
+ self.assertIs(True, P('AUX .txt').is_reserved())
+ self.assertIs(True, P('COM1:bar').is_reserved())
+ self.assertIs(True, P('LPT9 :bar').is_reserved())
+ # DOS-device names are only matched at the beginning
+ # of a path component.
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())
- # Only the last component matters.
+ # Only the last path component matters.
+ self.assertIs(True, P('c:/baz/con/NUL').is_reserved())
self.assertIs(False, P('c:/NUL/con/baz').is_reserved())
- # UNC paths are never reserved.
- self.assertIs(False, P('//my/share/nul/con/aux').is_reserved())
class PurePathTest(_BasePurePathTest, unittest.TestCase):
cls = pathlib.PurePath
diff --git a/Misc/NEWS.d/next/Library/2021-06-12-21-25-35.bpo-27827.TMWh1i.rst b/Misc/NEWS.d/next/Library/2021-06-12-21-25-35.bpo-27827.TMWh1i.rst
new file mode 100644
index 0000000000000..1b8cc04533ed8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-06-12-21-25-35.bpo-27827.TMWh1i.rst
@@ -0,0 +1,2 @@
+:meth:`pathlib.PureWindowsPath.is_reserved` now identifies a greater range of
+reserved filenames, including those with trailing spaces or colons.
More information about the Python-checkins
mailing list