Skip to content

Commit

Permalink
fix #1051: make disk_usage() on python 3 able to deal with bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed May 4, 2017
1 parent 8393e9c commit ec42f77
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Process.as_dict(): "attrs" and "ad_value". With this you can iterate over all
processes in one shot without needing to catch NoSuchProcess and do list/dict
comprehensions.
- 1051_: disk_usage() on Python 3 is now able to accept bytes.

**Bug fixes**

Expand Down
10 changes: 3 additions & 7 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,9 @@ def swap_memory():

def disk_usage(path):
"""Return disk usage associated with path."""
try:
total, free = cext.disk_usage(path)
except WindowsError:
if not os.path.exists(path):
msg = "No such file or directory: '%s'" % path
raise OSError(errno.ENOENT, msg)
raise
if PY3 and isinstance(path, bytes):
path = path.decode(FS_ENCODING)
total, free = cext.disk_usage(path)
used = total - free
percent = usage_percent(used, total, _round=1)
return _common.sdiskusage(total, used, free, percent)
Expand Down
8 changes: 3 additions & 5 deletions psutil/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,9 @@ def test_disk_usage_unicode(self):
if ASCII_FS:
with self.assertRaises(UnicodeEncodeError):
psutil.disk_usage(TESTFN_UNICODE)
else:
safe_rmpath(TESTFN_UNICODE)
self.addCleanup(safe_rmpath, TESTFN_UNICODE)
os.mkdir(TESTFN_UNICODE)
psutil.disk_usage(TESTFN_UNICODE)

def test_disk_usage_bytes(self):
psutil.disk_usage(b'.')

def test_disk_partitions(self):
# all = False
Expand Down

0 comments on commit ec42f77

Please sign in to comment.