[tor-commits] [stem/master] The str_tools functions didn't handle negative values

atagar at torproject.org atagar at torproject.org
Sun Dec 16 00:25:08 UTC 2012


commit fc4bdf7ab686b4bbd12e3aefc83d78d0a171df60
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Dec 15 16:22:30 2012 -0800

    The str_tools functions didn't handle negative values
    
    Functions like str_tools.get_size_label() provided labels using the smallest
    unit type if the count was negative. For instance, '-2000000' would be
    translated into '-2000000 B' rather than '-1 MB'.
    
    An inline comment seems to indicate that this was on purpose, but I haven't a
    clue why. Fixing it.
---
 stem/util/str_tools.py      |    8 +++++---
 test/unit/util/str_tools.py |    1 +
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/stem/util/str_tools.py b/stem/util/str_tools.py
index 6c36cd0..776ed48 100644
--- a/stem/util/str_tools.py
+++ b/stem/util/str_tools.py
@@ -280,14 +280,16 @@ def _get_label(units, count, decimal, is_long):
     (count_per_unit, short_label, long_label)
   :param int count: number of base units being converted
   :param int decimal: decimal precision of label
-  :param bool is_long: uses the long label if **True&&, short label otherwise
+  :param bool is_long: uses the long label if **True**, short label otherwise
   """
   
   # formatted string for the requested number of digits
   label_format = "%%.%if" % decimal
   
-  # for zero or negative values use the smallest units
-  if count < 1:
+  if count < 0:
+    label_format = "-" + label_format
+    count = abs(count)
+  elif count == 0:
     units_label = units[-1][2] + "s" if is_long else units[-1][1]
     return "%s%s" % (label_format % count, units_label)
   
diff --git a/test/unit/util/str_tools.py b/test/unit/util/str_tools.py
index e91e324..84fd850 100644
--- a/test/unit/util/str_tools.py
+++ b/test/unit/util/str_tools.py
@@ -39,6 +39,7 @@ class TestStrTools(unittest.TestCase):
     self.assertEquals('0.00 B', str_tools.get_size_label(0, 2))
     self.assertEquals('-10 B', str_tools.get_size_label(-10))
     self.assertEquals('80 b', str_tools.get_size_label(10, is_bytes = False))
+    self.assertEquals('-1 MB', str_tools.get_size_label(-2000000))
     
     # checking that we round down
     self.assertEquals('23.43 Kb', str_tools.get_size_label(3000, 2, is_bytes = False))



More information about the tor-commits mailing list