[3.13] gh-134151 Fix TypeError in email.utils.decode_params when sorting RFC 2231 continuations (GH-134687) (#135248)

gh-134151 Fix `TypeError` in `email.utils.decode_params` when sorting RFC 2231 continuations (GH-134687)

- Fix sorting logic in `email.utils.decode_params` to handle None values.
- Update tests for RFC 2231 continuation sorting.
(cherry picked from commit bcb6b45cb8)

Co-authored-by: Jiucheng(Oliver) <git.jiucheng@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-06-08 09:38:00 +02:00 committed by GitHub
parent 7620805480
commit e5d1771c6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 2 deletions

View file

@ -417,8 +417,14 @@ def decode_params(params):
for name, continuations in rfc2231_params.items():
value = []
extended = False
# Sort by number
continuations.sort()
# Sort by number, treating None as 0 if there is no 0,
# and ignore it if there is already a 0.
has_zero = any(x[0] == 0 for x in continuations)
if has_zero:
continuations = [x for x in continuations if x[0] is not None]
else:
continuations = [(x[0] or 0, x[1], x[2]) for x in continuations]
continuations.sort(key=lambda x: x[0])
# And now append all values in numerical order, converting
# %-encodings for the encoded segments. If any of the
# continuation names ends in a *, then the entire string, after