From: Armin Ronacher Date: Wed, 5 Oct 2011 07:48:05 +0000 (+0200) Subject: Fixed filesizeformat X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=b1b7b0893ca2750c871b402ea556e31558dc09dc;p=jinja2.git Fixed filesizeformat --- diff --git a/CHANGES b/CHANGES index 8e76e2b..84da684 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,7 @@ Version 2.7 - Choice and prefix loaders now dispatch source and template lookup separately in order to work in combination with module loaders as advertised. +- Fixed filesizeformat. Version 2.6 ----------- diff --git a/jinja2/filters.py b/jinja2/filters.py index 1ef47f9..5c9c341 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -15,7 +15,7 @@ from operator import itemgetter from itertools import imap, groupby from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode from jinja2.runtime import Undefined -from jinja2.exceptions import FilterArgumentError, SecurityError +from jinja2.exceptions import FilterArgumentError _word_re = re.compile(r'\w+(?u)') @@ -346,25 +346,25 @@ def do_filesizeformat(value, binary=False): bytes = float(value) base = binary and 1024 or 1000 prefixes = [ - (binary and "KiB" or "kB"), - (binary and "MiB" or "MB"), - (binary and "GiB" or "GB"), - (binary and "TiB" or "TB"), - (binary and "PiB" or "PB"), - (binary and "EiB" or "EB"), - (binary and "ZiB" or "ZB"), - (binary and "YiB" or "YB") + (binary and 'KiB' or 'kB'), + (binary and 'MiB' or 'MB'), + (binary and 'GiB' or 'GB'), + (binary and 'TiB' or 'TB'), + (binary and 'PiB' or 'PB'), + (binary and 'EiB' or 'EB'), + (binary and 'ZiB' or 'ZB'), + (binary and 'YiB' or 'YB') ] if bytes == 1: - return "1 Byte" + return '1 Byte' elif bytes < base: - return "%d Bytes" % bytes + return '%d Bytes' % bytes else: for i, prefix in enumerate(prefixes): - unit = base * base ** (i + 1) - if bytes < unit: - return "%.1f %s" % ((bytes / unit), prefix) - return "%.1f %s" % ((bytes / unit), prefix) + unit = base ** (i + 1) + if bytes <= unit: + return '%.1f %s' % ((bytes / unit), prefix) + return '%.1f %s' % ((bytes / unit), prefix) def do_pprint(value, verbose=False): diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py index aefe768..2009352 100644 --- a/jinja2/testsuite/filters.py +++ b/jinja2/testsuite/filters.py @@ -84,10 +84,10 @@ class FilterTestCase(JinjaTestCase): '{{ 1000000000000|filesizeformat(true) }}' ) out = tmpl.render() - assert out == ( - '100 Bytes|0.0 kB|0.0 MB|0.0 GB|0.0 TB|100 Bytes|' - '1000 Bytes|1.0 KiB|0.9 MiB|0.9 GiB' - ) + self.assert_equal(out, ( + '100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|' + '1000 Bytes|1.0 MiB|0.9 GiB|0.9 TiB' + )) def test_first(self): tmpl = env.from_string('{{ foo|first }}')