diff options
author | Steve Dower <steve.dower@python.org> | 2023-05-29 09:05:32 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-29 09:05:32 (GMT) |
commit | bfd20d257e4ad16a25f4bac0ea4dbb719cdf6bc7 (patch) | |
tree | 325056d3c4bbd55dd1b81be6d7ab150b5c0749b1 /Lib | |
parent | e92ac0a741b125f1cffe8c07b054d1dea7b0a05a (diff) | |
download | cpython-bfd20d257e4ad16a25f4bac0ea4dbb719cdf6bc7.zip cpython-bfd20d257e4ad16a25f4bac0ea4dbb719cdf6bc7.tar.gz cpython-bfd20d257e4ad16a25f4bac0ea4dbb719cdf6bc7.tar.bz2 |
gh-104803: Implement ntpath.isdevdrive for checking whether a path is on a Windows Dev Drive (GH-104805)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ntpath.py | 16 | ||||
-rw-r--r-- | Lib/test/test_ntpath.py | 20 |
2 files changed, 36 insertions, 0 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 0f3674f..dadcdc0 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -867,3 +867,19 @@ try: except ImportError: # Use genericpath.* as imported above pass + + +try: + from nt import _path_isdevdrive +except ImportError: + def isdevdrive(path): + """Determines whether the specified path is on a Windows Dev Drive.""" + # Never a Dev Drive + return False +else: + def isdevdrive(path): + """Determines whether the specified path is on a Windows Dev Drive.""" + try: + return _path_isdevdrive(abspath(path)) + except OSError: + return False diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 0e57c16..538d758 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -992,6 +992,26 @@ class TestNtpath(NtpathTestCase): self.assertTrue(os.path.exists is nt._path_exists) self.assertFalse(inspect.isfunction(os.path.exists)) + @unittest.skipIf(os.name != 'nt', "Dev Drives only exist on Win32") + def test_isdevdrive(self): + # Result may be True or False, but shouldn't raise + self.assertIn(ntpath.isdevdrive(os_helper.TESTFN), (True, False)) + # ntpath.isdevdrive can handle relative paths + self.assertIn(ntpath.isdevdrive("."), (True, False)) + self.assertIn(ntpath.isdevdrive(b"."), (True, False)) + # Volume syntax is supported + self.assertIn(ntpath.isdevdrive(os.listvolumes()[0]), (True, False)) + # Invalid volume returns False from os.path method + self.assertFalse(ntpath.isdevdrive(r"\\?\Volume{00000000-0000-0000-0000-000000000000}\\")) + # Invalid volume raises from underlying helper + with self.assertRaises(OSError): + nt._path_isdevdrive(r"\\?\Volume{00000000-0000-0000-0000-000000000000}\\") + + @unittest.skipIf(os.name == 'nt', "isdevdrive fallback only used off Win32") + def test_isdevdrive_fallback(self): + # Fallback always returns False + self.assertFalse(ntpath.isdevdrive(os_helper.TESTFN)) + class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase): pathmodule = ntpath |