Open webknjaz opened 8 months ago
@davidism do you think this might deserve a bug
label?
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: https://github.com/pallets/jinja/labels/bug.
I am working on this at the Pycon 2024 sprints
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 sameint
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: https://github.com/aio-libs/multidict/commit/946e61a5410524fe2572ffa94cc22369cdf84a58#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, anyway