-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The int
filter throws OverflowError
when the incoming string looks like scientific notation
#1921
Milestone
Comments
webknjaz
added a commit
to aio-libs/multidict
that referenced
this issue
Dec 30, 2023
This is a workaround for Jinja2 misjudging a string as an infinite float and throwing an `OverflowError` instead of returning the default of `None`. Ref: pallets/jinja#1921
davidism
changed the title
[BUG] The
The Dec 30, 2023
int
filter throws OverflowError
when the incoming string looks like scientific notationint
filter throws OverflowError
when the incoming string looks like scientific notation
webknjaz
added a commit
to aio-libs/multidict
that referenced
this issue
Dec 30, 2023
This patch work around the Jinja's `int` filter bug that allows certain commit hash-based filenames trigger an `OverflowError` in the Towncrier template. Ref: pallets/jinja#1921
@davidism do you think this might deserve a |
We don't use those labels. Either it's a bug or a feature, and that's obvious from their description and milestone when assigned. |
Ah, I see. I just saw that some issues are labeled and it confused me: bug . |
I am working on this at the Pycon 2024 sprints |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So I'm using the
int
filter with a default ofNone
to filter out list items that look like numbers. It worked with the test data but broke as I tried to use it for more.Having
{{ '31e1170' | int }}
should be the minimum repro. And adding any non-defaultbase
will probably yield the same behavior.Context: I'm improving a Towncrier template to allow for non-number references (such as commit messages) to be used (and linked) in changelogs. For this, I need to extract parts of the original list of things (that come from filenames — this is a part of the Towncrier logic; it's non-customizable) into separate lists — integers (PR/issue numbers), commits (strings of length 7, 8 or 40 that only have chars from the range
[0-9a-f]
) and the rest.To perform the first filtering step, I use
| map('int', default=None) | select('integer') | map('string')
, basically turning everything non-integer intoNone
and rejecting later in the chain.Then, I reject the found numbers from the original list, and attempt checking the items for length and using the same
int
filter but withbase=16
to get things that look like commits, I put them into their own list and add the rest to yet another one.After that, I can render those separately.
My test filenames looked differently, which is why I didn't initially catch the problem. It was luck that allowed me to stumble on this bug. I was confused at first, but then I realized that
<integer>E<integer>
is a scientific notation. When I checked the source @ https://github.com/pallets/jinja/blob/d594969/src/jinja2/filters.py#L961, it became clear whether the problem is coming from:Oh, and the traceback is:
(https://github.com/aio-libs/multidict/actions/runs/7360938745/job/20037695667#step:8:103)
This is the template change for more context: aio-libs/multidict@946e61a#diff-6b766c7833e9e98b72d0413f3b72569683a45bed7406bd31900466a8704205e5R19
Solution?
I think that it should be either adding
OverflowError
to the exceptions in the inner try-except here https://github.com/pallets/jinja/blob/d594969/src/jinja2/filters.py#L962 or havingif base != 10: return default
right before that inner try-except.Environment:
main
has this bug, anywayThe text was updated successfully, but these errors were encountered: