Skip to content

Commit

Permalink
pythongh-102519: Add os.listdrives, os.listvolumes and os.listmounts …
Browse files Browse the repository at this point in the history
…on Windows
  • Loading branch information
zooba committed Mar 8, 2023
1 parent cbb0aa7 commit cafd311
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ os
method to check if the entry is a junction.
(Contributed by Charles Machalow in :gh:`99547`.)

* Add :func:`os.listdrives`, :func:`os.listvolumes` and :func:`os.listmounts`
functions on Windows for enumerating drives, volumes and mount points.
(Contributed by Steve Dower in :gh:`102519`.)

os.path
-------

Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(value)
STRUCT_FOR_ID(values)
STRUCT_FOR_ID(version)
STRUCT_FOR_ID(volume)
STRUCT_FOR_ID(warnings)
STRUCT_FOR_ID(warnoptions)
STRUCT_FOR_ID(wbits)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2648,6 +2648,49 @@ def test_listdir_extended_path(self):
[os.fsencode(path) for path in self.created_paths])


@unittest.skipUnless(os.name == "nt", "NT specific tests")
class Win32ListdriveTests(unittest.TestCase):
"""Test listdrive, listmounts and listvolume on Windows."""

def setUp(self):
# Get drives and volumes from fsutil
out = subprocess.check_output(
["fsutil.exe", "volume", "list"],
cwd=os.path.join(os.getenv("SystemRoot", "\\Windows"), "System32"),
encoding="mbcs",
errors="ignore",
)
lines = out.splitlines()
self.known_volumes = {l for l in lines if l.startswith('\\\\?\\')}
self.known_drives = {l for l in lines if l[1:] == ':\\'}
self.known_mounts = {l for l in lines if l[1:3] == ':\\'}

def test_listdrives(self):
drives = os.listdrives()
self.assertIsInstance(drives, list)
self.assertSetEqual(
self.known_drives,
self.known_drives & set(drives),
)

def test_listvolumes(self):
volumes = os.listvolumes()
self.assertIsInstance(volumes, list)
self.assertSetEqual(
self.known_volumes,
self.known_volumes & set(volumes),
)

def test_listmounts(self):
for volume in os.listvolumes():
mounts = os.listmounts(volume)
self.assertIsInstance(mounts, list)
self.assertSetEqual(
set(mounts),
self.known_mounts & set(mounts),
)


@unittest.skipUnless(hasattr(os, 'readlink'), 'needs os.readlink()')
class ReadlinkTests(unittest.TestCase):
filelink = 'readlinktest'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`os.listdrives`, :func:`os.listvolumes` and :func:`os.listmounts`
functions on Windows for enumerating drives, volumes and mount points
128 changes: 127 additions & 1 deletion Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cafd311

Please sign in to comment.