]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Fixed int() filter for non-string objects #466
authorSebastian Noack <sebastian.noack@gmail.com>
Tue, 4 Aug 2015 16:28:42 +0000 (18:28 +0200)
committerMarkus Unterwaditzer <markus@unterwaditzer.net>
Tue, 12 Apr 2016 19:43:16 +0000 (21:43 +0200)
jinja2/filters.py
tests/test_filters.py

index e5c7a1ab439f803d76563a4cd77695bc92cfb539..96660a6a42a40a301e8937ef8cbf4dad6d016785 100644 (file)
@@ -518,9 +518,12 @@ def do_int(value, default=0, base=10):
     can also override the default base (10) in the second
     parameter, which handles input with prefixes such as
     0b, 0o and 0x for bases 2, 8 and 16 respectively.
+    The base is ignored for decimal numbers and non-string values.
     """
     try:
-        return int(value, base)
+        if isinstance(value, string_types):
+            return int(value, base)
+        return int(value)
     except (TypeError, ValueError):
         # this quirk is necessary so that "42.23"|int gives 42.
         try:
index 741ef341b1b0f7691e3b01dccac91e3193e3f95f..caefdff360c631e8dcde4a40af09bd99eaa6cc1c 100644 (file)
@@ -133,11 +133,16 @@ class TestFilter():
                        'foo bar foo bar\n  foo bar foo bar')
 
     def test_int(self, env):
+        class IntIsh(object):
+            def __int__(self):
+                return 42
+
         tmpl = env.from_string('{{ "42"|int }}|{{ "ajsghasjgd"|int }}|'
                                '{{ "32.32"|int }}|{{ "0x4d32"|int(0, 16) }}|'
-                               '{{ "011"|int(0, 8)}}|{{ "0x33FU"|int(0, 16) }}')
-        out = tmpl.render()
-        assert out == '42|0|32|19762|9|0'
+                               '{{ "011"|int(0, 8)}}|{{ "0x33FU"|int(0, 16) }}|'
+                               '{{ obj|int }}')
+        out = tmpl.render(obj=IntIsh())
+        assert out == '42|0|32|19762|9|0|42'
 
     def test_join(self, env):
         tmpl = env.from_string('{{ [1, 2, 3]|join("|") }}')