gh-153521: Preserve bytes message sets in imaplib (GH-153907)

serhiy-storchaka <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/0d87a0b635475fd311d350c20f8cf70893681a83
commit: 0d87a0b635475fd311d350c20f8cf70893681a83
branch: main
author: cui fliter <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-08-14T14:02:53+03:00
summary:

gh-153521: Preserve bytes message sets in imaplib (GH-153907)

Signed-off-by: cuishuang <[email protected]>

files:
M Lib/imaplib.py
M Lib/test/test_imaplib.py

diff --git a/Lib/imaplib.py b/Lib/imaplib.py
index 139da1d3bb6fb8..24d3a27f21d2d1 100644
--- a/Lib/imaplib.py
+++ b/Lib/imaplib.py
@@ -168,6 +168,8 @@ def _seq_range(item):
 
 
 def _format_sequence_set(arg):
+    if isinstance(arg, (bytes, bytearray)):
+        return str(arg, 'ascii')
     if isinstance(arg, (int, str)):
         return str(arg)
     # A sequence of message numbers and ranges.
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index 100791a9c3eb2c..d97da398803681 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -292,6 +292,8 @@ def test_sequence_set(self):
         # A scalar is passed through as a string.
         self.assertEqual(m._sequence_set(5), '5')
         self.assertEqual(m._sequence_set('1:3,7'), '1:3,7')
+        self.assertEqual(m._sequence_set(b'1:3,7'), '1:3,7')
+        self.assertEqual(m._sequence_set(bytearray(b'1:3,7')), '1:3,7')
         # A sequence of numbers and ranges is formatted as a sequence set.
         self.assertEqual(m._sequence_set([1, 2, 5]), '1,2,5')
         self.assertEqual(m._sequence_set([1, (3, 5), (8, '*')]), '1,3:5,8:*')
@@ -333,6 +335,7 @@ def test_substitute(self):
                          r'(\Seen \Answered)')
         # '?s' formats a message sequence set.
         self.assertEqual(sub('?s', [[1, (3, 5), (8, '*')]]), '1,3:5,8:*')
+        self.assertEqual(sub('?s', [b'1:3,7']), '1:3,7')
         # '??' is a literal '?'.
         self.assertEqual(sub('a?? b', []), 'a? b')
 
@@ -1574,6 +1577,16 @@ def cmd_FETCH(self, tag, args):
         ])
         self.assertEqual(server.args, ['2:4', '(FLAGS)'])
 
+        # A preformatted message set may be passed as bytes.
+        typ, data = client.fetch(b'2:4', '(FLAGS)')
+        self.assertEqual(typ, 'OK')
+        self.assertEqual(data, [
+            br'2 (FLAGS (\Seen))',
+            br'3 (FLAGS (\Seen))',
+            br'4 (FLAGS (\Seen))',
+        ])
+        self.assertEqual(server.args, ['2:4', '(FLAGS)'])
+
         # A comma-separated set with an open range up to '*'.
         typ, data = client.fetch('1,3:*', '(FLAGS)')
         self.assertEqual(typ, 'OK')

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.