Fix ZeroDivisionError in slice, batch, and divisibleby with 0 args
This commit is contained in:
parent
5ef70112a1
commit
e8321f30e1
@ -1074,6 +1074,11 @@ def sync_do_slice(
|
||||
If you pass it a second argument it's used to fill missing
|
||||
values on the last iteration.
|
||||
"""
|
||||
if slices <= 0:
|
||||
raise FilterArgumentError(
|
||||
f"slice: 'slices' must be greater than 0, got {slices}"
|
||||
)
|
||||
|
||||
seq = list(value)
|
||||
length = len(seq)
|
||||
items_per_slice = length // slices
|
||||
@ -1125,6 +1130,11 @@ def do_batch(
|
||||
{%- endfor %}
|
||||
</table>
|
||||
"""
|
||||
if linecount <= 0:
|
||||
raise FilterArgumentError(
|
||||
f"batch: 'linecount' must be greater than 0, got {linecount}"
|
||||
)
|
||||
|
||||
tmp: list[V] = []
|
||||
|
||||
for item in value:
|
||||
|
||||
@ -24,6 +24,8 @@ def test_even(value: int) -> bool:
|
||||
|
||||
def test_divisibleby(value: int, num: int) -> bool:
|
||||
"""Check if a variable is divisible by a number."""
|
||||
if num == 0:
|
||||
return False
|
||||
return value % num == 0
|
||||
|
||||
|
||||
|
||||
@ -70,6 +70,13 @@ class TestFilter:
|
||||
"[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]"
|
||||
)
|
||||
|
||||
def test_batch_zero(self, env):
|
||||
from jinja2.exceptions import FilterArgumentError
|
||||
|
||||
tmpl = env.from_string("{{ foo|batch(0)|list }}")
|
||||
with pytest.raises(FilterArgumentError):
|
||||
tmpl.render(foo=list(range(10)))
|
||||
|
||||
def test_slice(self, env):
|
||||
tmpl = env.from_string("{{ foo|slice(3)|list }}|{{ foo|slice(3, 'X')|list }}")
|
||||
out = tmpl.render(foo=list(range(10)))
|
||||
@ -78,6 +85,13 @@ class TestFilter:
|
||||
"[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]"
|
||||
)
|
||||
|
||||
def test_slice_zero(self, env):
|
||||
from jinja2.exceptions import FilterArgumentError
|
||||
|
||||
tmpl = env.from_string("{{ foo|slice(0)|list }}")
|
||||
with pytest.raises(FilterArgumentError):
|
||||
tmpl.render(foo=list(range(10)))
|
||||
|
||||
def test_escape(self, env):
|
||||
tmpl = env.from_string("""{{ '<">&'|escape }}""")
|
||||
out = tmpl.render()
|
||||
|
||||
@ -27,6 +27,10 @@ class TestTestsCase:
|
||||
tmpl = env.from_string("""{{ "foo" is lower }}|{{ "FOO" is lower }}""")
|
||||
assert tmpl.render() == "True|False"
|
||||
|
||||
def test_divisibleby_zero(self, env):
|
||||
tmpl = env.from_string("{{ 5 is divisibleby(0) }}")
|
||||
assert tmpl.render() == "False"
|
||||
|
||||
# Test type checks
|
||||
@pytest.mark.parametrize(
|
||||
"op,expect",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user