clean up scientific notation support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
add changelog clean up docs parametrize tests
authored by David Lord · committed by David Lord · parent fd00805 · Browse files · 4 files·+40 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+40 added−21 removed
- tests:+30 added−3 removed
- other:+10 added−18 removed
Provenance
- Commit
483b62e- Tree
178cb43- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
fd00805
4 files changed+40 added−21 removed
modified:docs/templates.rst+6 added-11 removed
394 unmodified lines395 this template, it first locates the parent. The extends tag should be the396 first tag in the template. Everything before it is printed out normally and397 may cause confusion. For details about this behavior and how to take398 advantage of it, see :ref:`null-master-fallback`. Also a block will always be399-filled in regardless of whether the surrounding condition is evaluated to be true 399+filled in regardless of whether the surrounding condition is evaluated to be true400 or false.401 402 The filename of the template depends on the template loader. For example, the403 :class:`FileSystemLoader` allows you to access other templates by giving the773 unmodified lines1177 arguments to function calls and filters, or just to extend or include a1178 template).1179 1180 42:1181- Integers numbers are created by just writing the number down. 1182- If a dot is present, the number will be considered a float, otherwise an1183- integer. Keep in mind that, in Python, ``42`` and ``42.0``1184- are different (``int`` and ``float``, respectively).1181+ Integers are whole numbers without a decimal part.1182 1186-42.23 / 42e2 / 42E2 / 1e0:1187- Floating point numbers are created by just writing the1188- number down. Floating points can be written using the dot as a decimal mark,1189- or they can be written in scientific notation in which1190- case you can use lower case 'e' or upper case 'E' to indicate the exponent1191- part. 1183+42.23 / 42.1e2:1184+ Floating point numbers can be written using a '.' as a decimal mark.1185+ They can also be written in scientific notation with an upper or1186+ lower case 'e' to indicate the exponent part.1187 1188 ['list', 'of', 'objects']:1189 Everything between two brackets is a list. Lists are useful for storing1190 sequential data to be iterated over. For example, you can easily569 unmodified linesmodified:jinja2/lexer.py+2 added-7 removed
13 unmodified lines14 :copyright: (c) 2017 by the Jinja Team.15 :license: BSD, see LICENSE for more details.16 """17 import re18+from ast import literal_eval19 from collections import deque20 from operator import itemgetter21 22 from jinja2._compat import implements_iterator, intern, iteritems, text_type23 from jinja2.exceptions import TemplateSyntaxError24 from jinja2.utils import LRUCache25 25-from ast import literal_eval # to support scientific notation26-26 # cache for the lexers. Exists in order to be able to have multiple27 # environments with the same lexer28 _lexer_cache = LRUCache(50)29 22 unmodified lines52 import jinja253 del jinja2._identifier54 del _identifier55 57-# Note: Float now supports 0 or 1 dots, and must thus be evaluated in the right 58-# order so that pure integers are caught first. Floats can now be written with 59-# scientific notation.60-float_re = re.compile(r'(?<!\.)\d+\.?\d+(?:e-?\d+)?', re.IGNORECASE)56+float_re = re.compile(r'(?<!\.)\d+(?:\.\d+)?(?:e[+\-]?\d+)?', re.IGNORECASE)61-57 newline_re = re.compile(r'(\r\n|\r|\n)')58 59 # internal the tokens and keep references to them60 TOKEN_ADD = intern('add')690 unmodified linesmodified:tests/test_lexnparse.py+30 added-3 removed
334 unmodified lines335 def test_inop(self, env):336 tmpl = env.from_string('{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}')337 assert tmpl.render() == 'True|False'338 339- def test_literals(self, env):340- tmpl = env.from_string('{{ [] }}|{{ {} }}|{{ () }}')341- assert tmpl.render().lower() == '[]|{}|()'339+ @pytest.mark.parametrize("value", ("[]", "{}", "()"))340+ def test_collection_literal(self, env, value):341+ t = env.from_string("{{ %s }}" % value)342+ assert t.render() == value343 344+ @pytest.mark.parametrize("value", ("1", "123"))345+ def test_int_literal(self, env, value):346+ t = env.from_string("{{ %s }}" % value)347+ assert t.render() == value348+349+ @pytest.mark.parametrize(350+ "value",351+ (352+ "1.2",353+ "34.56",354+ ("1e0", "1.0"),355+ ("10e1", "100.0"),356+ ("2.5e100", "2.5e+100"),357+ "2.5e+100",358+ ("25.6e-10", "2.56e-09"),359+ ),360+ )361+ def test_float_literal(self, env, value):362+ if isinstance(value, tuple):363+ value, expect = value364+ else:365+ expect = value366+367+ t = env.from_string("{{ %s }}" % value)368+ assert t.render() == expect369+370 def test_bool(self, env):371 tmpl = env.from_string('{{ true and false }}|{{ false '372 'or true }}|{{ not false }}')373 assert tmpl.render() == 'False|True|True'319 unmodified linesmodified:CHANGES.rst+2 added
19 unmodified lines20 ``lstrip_blocks == False`` (`#748`_)21 - Added a ``default`` parameter for the ``map`` filter. (`#557`_)22 - Exclude environment globals from23 :func:`meta.find_undeclared_variables`. #93124+- Float literals can be written with scientific notation, like25+ ``{{ 2.56e-3 }}``. #912, #92226 27 .. _#557: https://github.com/pallets/jinja/issues/55728 .. _#765: https://github.com/pallets/jinja/issues/76529 .. _#748: https://github.com/pallets/jinja/issues/748613 unmodified lines