From: Christopher Grebs Date: Fri, 7 Oct 2011 20:50:13 +0000 (+0200) Subject: Fixed do_filesizeformat to actually calculate correctly, fixes #59 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=7d268bef0e8f3f12c0acb90f30d67726a3e3f261;p=jinja2.git Fixed do_filesizeformat to actually calculate correctly, fixes #59 --- diff --git a/jinja2/filters.py b/jinja2/filters.py index 5c9c341..352b166 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -361,10 +361,10 @@ def do_filesizeformat(value, binary=False): return '%d Bytes' % bytes else: for i, prefix in enumerate(prefixes): - unit = base ** (i + 1) - if bytes <= unit: - return '%.1f %s' % ((bytes / unit), prefix) - return '%.1f %s' % ((bytes / unit), prefix) + unit = base ** (i + 2) + if bytes < unit: + return '%.1f %s' % ((base * bytes / unit), prefix) + return '%.1f %s' % ((base * bytes / unit), prefix) def do_pprint(value, verbose=False): diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py index 2009352..770bdf2 100644 --- a/jinja2/testsuite/filters.py +++ b/jinja2/testsuite/filters.py @@ -86,9 +86,27 @@ class FilterTestCase(JinjaTestCase): out = tmpl.render() 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' + '1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB' )) + def test_filesizeformat_issue59(self): + tmpl = env.from_string( + '{{ 300|filesizeformat }}|' + '{{ 3000|filesizeformat }}|' + '{{ 3000000|filesizeformat }}|' + '{{ 3000000000|filesizeformat }}|' + '{{ 3000000000000|filesizeformat }}|' + '{{ 300|filesizeformat(true) }}|' + '{{ 3000|filesizeformat(true) }}|' + '{{ 3000000|filesizeformat(true) }}' + ) + out = tmpl.render() + self.assert_equal(out, ( + '300 Bytes|3.0 kB|3.0 MB|3.0 GB|3.0 TB|300 Bytes|' + '2.9 KiB|2.9 MiB' + )) + + def test_first(self): tmpl = env.from_string('{{ foo|first }}') out = tmpl.render(foo=range(10))