Skip to content

Commit 5882866

Browse files
committed
Reject division by zero in MathBlock
1 parent bb353f5 commit 5882866

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Unreleased
22

3-
*Currently none*
3+
- Reject division by zero in `MathBlock`
4+
- Blocks that divide by zero are now rejected instead of throwing an exception into
5+
the interpreter
46

57
# v1.3.1
68

src/ya_tagscript/blocks/math/math_block.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,6 @@ def process(self, ctx: Context) -> str | None:
345345
parsed_payload = ctx.interpret_segment(payload)
346346
try:
347347
return str(self._NSP.eval(parsed_payload))
348-
except (ValueError, OverflowError) as e:
348+
except (ValueError, OverflowError, ZeroDivisionError) as e:
349349
_log.debug(e)
350350
return None

tests/ya_tagscript/blocks/math/test_math_block.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ def test_dec_math_docs_example_three(
113113
assert result == "3.33"
114114

115115

116+
def test_dec_math_division_by_zero_is_rejected(
117+
ts_interpreter: TagScriptInterpreter,
118+
):
119+
script = "{math:10 / 0}"
120+
result = ts_interpreter.process(script).body
121+
assert result == script
122+
123+
116124
# region 001-110: 110 random expressions, tested with the 'math' declaration
117125

118126

0 commit comments

Comments
 (0)