https://github.com/python/cpython/commit/e5d1771c6b3b99d1b112b4c7e555c7e35e38657b
commit: e5d1771c6b3b99d1b112b4c7e555c7e35e38657b
branch: 3.13
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: picnixz <10796600+picn...@users.noreply.github.com>
date: 2025-06-08T07:38:00Z
summary:

[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 bcb6b45cb86a2f9f65b6c41f27c36059ba86a50b)

Co-authored-by: Jiucheng(Oliver) <git.jiuch...@gmail.com>

files:
A Misc/NEWS.d/next/Library/2025-05-25-23-23-05.gh-issue-134151.13Wwsb.rst
M Lib/email/utils.py
M Lib/test/test_email/test_email.py

diff --git a/Lib/email/utils.py b/Lib/email/utils.py
index e42674fa4f3b75..e4d35f06abcc09 100644
--- a/Lib/email/utils.py
+++ b/Lib/email/utils.py
@@ -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
diff --git a/Lib/test/test_email/test_email.py 
b/Lib/test/test_email/test_email.py
index bbb0102c5018a9..5d50eef31a74bd 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -389,6 +389,24 @@ def test_bad_param(self):
         msg = email.message_from_string("Content-Type: blarg; baz; boo\n")
         self.assertEqual(msg.get_param('baz'), '')
 
+    def test_continuation_sorting_part_order(self):
+        msg = email.message_from_string(
+            "Content-Disposition: attachment; "
+            "filename*=\"ignored\"; "
+            "filename*0*=\"utf-8''foo%20\"; "
+            "filename*1*=\"bar.txt\"\n"
+        )
+        filename = msg.get_filename()
+        self.assertEqual(filename, 'foo bar.txt')
+
+    def test_sorting_no_continuations(self):
+        msg = email.message_from_string(
+            "Content-Disposition: attachment; "
+            "filename*=\"bar.txt\"; "
+        )
+        filename = msg.get_filename()
+        self.assertEqual(filename, 'bar.txt')
+
     def test_missing_filename(self):
         msg = email.message_from_string("From: foo\n")
         self.assertEqual(msg.get_filename(), None)
diff --git 
a/Misc/NEWS.d/next/Library/2025-05-25-23-23-05.gh-issue-134151.13Wwsb.rst 
b/Misc/NEWS.d/next/Library/2025-05-25-23-23-05.gh-issue-134151.13Wwsb.rst
new file mode 100644
index 00000000000000..ecdde240b4aadc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-05-25-23-23-05.gh-issue-134151.13Wwsb.rst
@@ -0,0 +1,2 @@
+:mod:`email`: Fix :exc:`TypeError` in :func:`email.utils.decode_params`
+when sorting :rfc:`2231` continuations that contain an unnumbered section.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to