Skip to content
Browse files

Fixed filesizeformat

  • Loading branch information...
1 parent 1898c24 commit b1b7b0893ca2750c871b402ea556e31558dc09dc @mitsuhiko mitsuhiko committed
Showing with 20 additions and 19 deletions.
  1. +1 −0 CHANGES
  2. +15 −15 jinja2/filters.py
  3. +4 −4 jinja2/testsuite/filters.py
View
1 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
-----------
View
30 jinja2/filters.py
@@ -15,7 +15,7 @@
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):
View
8 jinja2/testsuite/filters.py
@@ -84,10 +84,10 @@ def test_filesizeformat(self):
'{{ 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 }}')

0 comments on commit b1b7b08

Please sign in to comment.
Something went wrong with that request. Please try again.