Skip to content

Commit cb35882

Browse files
authored
pythongh-102519: Add os.listdrives, os.listvolumes and os.listmounts on Windows (pythonGH-102544)
1 parent 2999e02 commit cb35882

9 files changed

+375
-1
lines changed

Doc/whatsnew/3.12.rst

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ os
294294
method to check if the entry is a junction.
295295
(Contributed by Charles Machalow in :gh:`99547`.)
296296

297+
* Add :func:`os.listdrives`, :func:`os.listvolumes` and :func:`os.listmounts`
298+
functions on Windows for enumerating drives, volumes and mount points.
299+
(Contributed by Steve Dower in :gh:`102519`.)
300+
297301
os.path
298302
-------
299303

Include/internal/pycore_global_objects_fini_generated.h

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

+1
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ struct _Py_global_strings {
702702
STRUCT_FOR_ID(value)
703703
STRUCT_FOR_ID(values)
704704
STRUCT_FOR_ID(version)
705+
STRUCT_FOR_ID(volume)
705706
STRUCT_FOR_ID(warnings)
706707
STRUCT_FOR_ID(warnoptions)
707708
STRUCT_FOR_ID(wbits)

Include/internal/pycore_runtime_init_generated.h

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_os.py

+43
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,49 @@ def test_listdir_extended_path(self):
26482648
[os.fsencode(path) for path in self.created_paths])
26492649

26502650

2651+
@unittest.skipUnless(os.name == "nt", "NT specific tests")
2652+
class Win32ListdriveTests(unittest.TestCase):
2653+
"""Test listdrive, listmounts and listvolume on Windows."""
2654+
2655+
def setUp(self):
2656+
# Get drives and volumes from fsutil
2657+
out = subprocess.check_output(
2658+
["fsutil.exe", "volume", "list"],
2659+
cwd=os.path.join(os.getenv("SystemRoot", "\\Windows"), "System32"),
2660+
encoding="mbcs",
2661+
errors="ignore",
2662+
)
2663+
lines = out.splitlines()
2664+
self.known_volumes = {l for l in lines if l.startswith('\\\\?\\')}
2665+
self.known_drives = {l for l in lines if l[1:] == ':\\'}
2666+
self.known_mounts = {l for l in lines if l[1:3] == ':\\'}
2667+
2668+
def test_listdrives(self):
2669+
drives = os.listdrives()
2670+
self.assertIsInstance(drives, list)
2671+
self.assertSetEqual(
2672+
self.known_drives,
2673+
self.known_drives & set(drives),
2674+
)
2675+
2676+
def test_listvolumes(self):
2677+
volumes = os.listvolumes()
2678+
self.assertIsInstance(volumes, list)
2679+
self.assertSetEqual(
2680+
self.known_volumes,
2681+
self.known_volumes & set(volumes),
2682+
)
2683+
2684+
def test_listmounts(self):
2685+
for volume in os.listvolumes():
2686+
mounts = os.listmounts(volume)
2687+
self.assertIsInstance(mounts, list)
2688+
self.assertSetEqual(
2689+
set(mounts),
2690+
self.known_mounts & set(mounts),
2691+
)
2692+
2693+
26512694
@unittest.skipUnless(hasattr(os, 'readlink'), 'needs os.readlink()')
26522695
class ReadlinkTests(unittest.TestCase):
26532696
filelink = 'readlinktest'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :func:`os.listdrives`, :func:`os.listvolumes` and :func:`os.listmounts`
2+
functions on Windows for enumerating drives, volumes and mount points

Modules/clinic/posixmodule.c.h

+127-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)