Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Commit

Permalink
bytes2human() is unable to print negative values (giampaolo#2268)
Browse files Browse the repository at this point in the history
Signed-off-by: Giampaolo Rodola <[email protected]>

The bug:

```
>>> from psutil._common import bytes2human
>>> bytes2human(212312454)
202.5M
>>> bytes2human(-212312454)
-212312454
```
  • Loading branch information
giampaolo authored Jun 9, 2023
1 parent bca6232 commit 962cb9a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ XXXX-XX-XX

- 2241_, [NetBSD]: can't compile On NetBSD 10.99.3/amd64. (patch by Thomas
Klausner)
- 2266_: if `Process`_ class is passed a very high PID, raise `NoSuchProcess`_
- 2266_: if `Process`_ class is passed a very high PID, raise `NoSuchProcess`_
instead of OverflowError. (patch by Xuehai Pan)
- 2268_: ``bytes2human()`` utility function was unable to properly represent
negative values.

5.9.5
=====
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ Bytes conversion
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
if abs(n) >= prefix[s]:
value = float(n) / prefix[s]
return '%.1f%s' % (value, s)
return "%sB" % n
Expand Down
2 changes: 1 addition & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ def bytes2human(n, format="%(value).1f%(symbol)s"):
for i, s in enumerate(symbols[1:]):
prefix[s] = 1 << (i + 1) * 10
for symbol in reversed(symbols[1:]):
if n >= prefix[symbol]:
if abs(n) >= prefix[symbol]:
value = float(n) / prefix[symbol]
return format % locals()
return format % dict(symbol=symbols[0], value=n)
Expand Down

0 comments on commit 962cb9a

Please sign in to comment.