summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2023-03-10 17:29:04 (GMT)
committerGitHub <noreply@github.com>2023-03-10 17:29:04 (GMT)
commit90f1d777177e28b6c7b8d9ba751550e373d61b0a (patch)
tree62f3a97cbe233f44e6f839b4ced6c17cdabb2abf
parent12226bec2588f925f4698e1130ce78e118343934 (diff)
downloadcpython-90f1d777177e28b6c7b8d9ba751550e373d61b0a.zip
cpython-90f1d777177e28b6c7b8d9ba751550e373d61b0a.tar.gz
cpython-90f1d777177e28b6c7b8d9ba751550e373d61b0a.tar.bz2
GH-80486: Fix handling of NTFS alternate data streams in pathlib (GH-102454)
Co-authored-by: Maor Kleinberger <kmaork@gmail.com>
-rw-r--r--Lib/pathlib.py8
-rw-r--r--Lib/test/test_pathlib.py28
-rw-r--r--Misc/NEWS.d/next/Library/2019-03-15-22-50-27.bpo-36305.Pbkv6u.rst2
3 files changed, 34 insertions, 4 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index d375529..55c44f1 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -320,8 +320,9 @@ class PurePath(object):
def _format_parsed_parts(cls, drv, root, parts):
if drv or root:
return drv + root + cls._flavour.sep.join(parts[1:])
- else:
- return cls._flavour.sep.join(parts)
+ elif parts and cls._flavour.splitdrive(parts[0])[0]:
+ parts = ['.'] + parts
+ return cls._flavour.sep.join(parts)
def __str__(self):
"""Return the string representation of the path, suitable for
@@ -1188,7 +1189,8 @@ class Path(PurePath):
homedir = self._flavour.expanduser(self._parts[0])
if homedir[:1] == "~":
raise RuntimeError("Could not determine home directory.")
- return self._from_parts([homedir] + self._parts[1:])
+ drv, root, parts = self._parse_parts((homedir,))
+ return self._from_parsed_parts(drv, root, parts + self._parts[1:])
return self
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index f8e2f44..f05dead 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -122,6 +122,13 @@ class NTFlavourTest(_BaseFlavourTest, unittest.TestCase):
# the second path is relative.
check(['c:/a/b', 'c:x/y'], ('c:', '\\', ['c:\\', 'a', 'b', 'x', 'y']))
check(['c:/a/b', 'c:/x/y'], ('c:', '\\', ['c:\\', 'x', 'y']))
+ # Paths to files with NTFS alternate data streams
+ check(['./c:s'], ('', '', ['c:s']))
+ check(['cc:s'], ('', '', ['cc:s']))
+ check(['C:c:s'], ('C:', '', ['C:', 'c:s']))
+ check(['C:/c:s'], ('C:', '\\', ['C:\\', 'c:s']))
+ check(['D:a', './c:b'], ('D:', '', ['D:', 'a', 'c:b']))
+ check(['D:/a', './c:b'], ('D:', '\\', ['D:\\', 'a', 'c:b']))
#
@@ -165,6 +172,7 @@ class _BasePurePathTest(object):
self.assertEqual(P(P('a'), 'b'), P('a/b'))
self.assertEqual(P(P('a'), P('b')), P('a/b'))
self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c")))
+ self.assertEqual(P(P('./a:b')), P('./a:b'))
def test_bytes(self):
P = self.cls
@@ -814,7 +822,8 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
equivalences = _BasePurePathTest.equivalences.copy()
equivalences.update({
- 'c:a': [ ('c:', 'a'), ('c:', 'a/'), ('/', 'c:', 'a') ],
+ './a:b': [ ('./a:b',) ],
+ 'c:a': [ ('c:', 'a'), ('c:', 'a/'), ('.', 'c:', 'a') ],
'c:/a': [
('c:/', 'a'), ('c:', '/', 'a'), ('c:', '/a'),
('/z', 'c:/', 'a'), ('//x/y', 'c:/', 'a'),
@@ -838,6 +847,7 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self.assertEqual(str(p), '\\\\a\\b\\c\\d')
def test_str_subclass(self):
+ self._check_str_subclass('.\\a:b')
self._check_str_subclass('c:')
self._check_str_subclass('c:a')
self._check_str_subclass('c:a\\b.txt')
@@ -1005,6 +1015,7 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self.assertEqual(P('//a/b').drive, '\\\\a\\b')
self.assertEqual(P('//a/b/').drive, '\\\\a\\b')
self.assertEqual(P('//a/b/c/d').drive, '\\\\a\\b')
+ self.assertEqual(P('./c:a').drive, '')
def test_root(self):
P = self.cls
@@ -1341,6 +1352,14 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self.assertEqual(pp, P('C:/a/b/x/y'))
pp = p.joinpath('c:/x/y')
self.assertEqual(pp, P('C:/x/y'))
+ # Joining with files with NTFS data streams => the filename should
+ # not be parsed as a drive letter
+ pp = p.joinpath(P('./d:s'))
+ self.assertEqual(pp, P('C:/a/b/d:s'))
+ pp = p.joinpath(P('./dd:s'))
+ self.assertEqual(pp, P('C:/a/b/dd:s'))
+ pp = p.joinpath(P('E:d:s'))
+ self.assertEqual(pp, P('E:d:s'))
def test_div(self):
# Basically the same as joinpath().
@@ -1361,6 +1380,11 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
# the second path is relative.
self.assertEqual(p / 'c:x/y', P('C:/a/b/x/y'))
self.assertEqual(p / 'c:/x/y', P('C:/x/y'))
+ # Joining with files with NTFS data streams => the filename should
+ # not be parsed as a drive letter
+ self.assertEqual(p / P('./d:s'), P('C:/a/b/d:s'))
+ self.assertEqual(p / P('./dd:s'), P('C:/a/b/dd:s'))
+ self.assertEqual(p / P('E:d:s'), P('E:d:s'))
def test_is_reserved(self):
P = self.cls
@@ -1626,6 +1650,8 @@ class _BasePathTest(object):
self.assertEqual(p.expanduser(), p)
p = P(P('').absolute().anchor) / '~'
self.assertEqual(p.expanduser(), p)
+ p = P('~/a:b')
+ self.assertEqual(p.expanduser(), P(os.path.expanduser('~'), './a:b'))
def test_exists(self):
P = self.cls
diff --git a/Misc/NEWS.d/next/Library/2019-03-15-22-50-27.bpo-36305.Pbkv6u.rst b/Misc/NEWS.d/next/Library/2019-03-15-22-50-27.bpo-36305.Pbkv6u.rst
new file mode 100644
index 0000000..d936049
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-03-15-22-50-27.bpo-36305.Pbkv6u.rst
@@ -0,0 +1,2 @@
+Fix handling of Windows filenames that resemble drives, such as ``./a:b``,
+in :mod:`pathlib`.