From 2eb4542cbab101910d220229a8f09c94f76c19d1 Mon Sep 17 00:00:00 2001 From: Felipe Moreno Date: Mon, 20 May 2024 11:02:20 -0400 Subject: [PATCH] int filter handles OverflowError to handle scientific notation --- CHANGES.rst | 2 ++ src/jinja2/filters.py | 2 +- tests/test_filters.py | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index e4bffbfb..6c998e62 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -18,6 +18,8 @@ Unreleased when calling block references. :issue:`1701` - Make ``|unique`` async-aware, allowing it to be used after another async-aware filter. :issue:`1781` +- ``|int`` filter handles ``OverflowError`` from scientific notation. + :issue:`1921` Version 3.1.4 diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index af9f6bc0..a92832a3 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -999,7 +999,7 @@ def do_int(value: t.Any, default: int = 0, base: int = 10) -> int: # this quirk is necessary so that "42.23"|int gives 42. try: return int(float(value)) - except (TypeError, ValueError): + except (TypeError, ValueError, OverflowError): return default diff --git a/tests/test_filters.py b/tests/test_filters.py index d8e9114d..2cb53ac9 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -196,6 +196,7 @@ class TestFilter: ("abc", "0"), ("32.32", "32"), ("12345678901234567890", "12345678901234567890"), + ("1e10000", "0"), ), ) def test_int(self, env, value, expect): -- 2.47.2