Skip to content

Commit

Permalink
Do not consider paths with a single leading slash as absolute on Windows
Browse files Browse the repository at this point in the history
This is consistent with the new "os.path.isabs" behavior since Python 3.13
  • Loading branch information
phw committed Dec 29, 2024
1 parent 63e1b90 commit fb2c5ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 9 additions & 1 deletion picard/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,15 @@ def is_absolute_path(path):
"""Similar to os.path.isabs, but properly detects Windows shares as absolute paths
See https://bugs.python.org/issue22302
"""
return os.path.isabs(path) or (IS_WIN and os.path.normpath(path).startswith("\\\\"))
if IS_WIN:
# Two backslashes indicate a UNC path.
if path.startswith("\\\\"):
return True
# Consider a single slash at the start not relative. This is the default
# for `os.path.isabs` since Python 3.13.
elif path.startswith("\\") or path.startswith("/"):
return False
return os.path.isabs(path)


def samepath(path1, path2):
Expand Down
7 changes: 6 additions & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ def test_remove_windows_drive(self):

class IsAbsolutePathTest(PicardTestCase):

@unittest.skipIf(IS_WIN, "POSIX test")
def test_is_absolute(self):
self.assertTrue(is_absolute_path('/foo/bar'))
self.assertFalse(is_absolute_path('foo/bar'))
Expand All @@ -410,7 +411,11 @@ def test_is_absolute(self):
def test_is_absolute_windows(self):
self.assertTrue(is_absolute_path('D:/foo/bar'))
self.assertTrue(is_absolute_path('D:\\foo\\bar'))
self.assertTrue(is_absolute_path('\\foo\\bar'))
self.assertFalse(is_absolute_path('\\foo\\bar'))
self.assertFalse(is_absolute_path('/foo/bar'))
self.assertFalse(is_absolute_path('foo/bar'))
self.assertFalse(is_absolute_path('./foo/bar'))
self.assertFalse(is_absolute_path('../foo/bar'))
# Paths to Windows shares
self.assertTrue(is_absolute_path('\\\\foo\\bar'))
self.assertTrue(is_absolute_path('\\\\foo\\bar\\'))
Expand Down

0 comments on commit fb2c5ec

Please sign in to comment.