https://github.com/python/cpython/commit/01a51f949475f1590eb5899f3002304060501ab2
commit: 01a51f949475f1590eb5899f3002304060501ab2
branch: main
author: Bruce Merry <[email protected]>
committer: gvanrossum <[email protected]>
date: 2024-04-11T07:41:55-07:00
summary:

gh-117722: Fix Stream.readuntil with non-bytes buffer objects (#117723)

gh-16429 introduced support for an iterable of separators in
Stream.readuntil. Since bytes-like types are themselves iterable, this
can introduce ambiguities in deciding whether the argument is an
iterator of separators or a singleton separator. In gh-16429, only 'bytes'
was considered a singleton, but this will break code that passes other
buffer object types.

Fix it by only supporting tuples rather than arbitrary iterables.

Closes gh-117722.

files:
A Misc/NEWS.d/next/Library/2024-04-10-20-59-10.gh-issue-117722.oxIUEI.rst
M Doc/library/asyncio-stream.rst
M Doc/whatsnew/3.13.rst
M Lib/asyncio/streams.py
M Lib/test/test_asyncio/test_streams.py

diff --git a/Doc/library/asyncio-stream.rst b/Doc/library/asyncio-stream.rst
index 6231b49b1e2431..3fdc79b3c6896c 100644
--- a/Doc/library/asyncio-stream.rst
+++ b/Doc/library/asyncio-stream.rst
@@ -260,7 +260,7 @@ StreamReader
       buffer is reset.  The :attr:`IncompleteReadError.partial` attribute
       may contain a portion of the separator.
 
-      The *separator* may also be an :term:`iterable` of separators. In this
+      The *separator* may also be a tuple of separators. In this
       case the return value will be the shortest possible that has any
       separator as the suffix. For the purposes of :exc:`LimitOverrunError`,
       the shortest possible separator is considered to be the one that
@@ -270,7 +270,7 @@ StreamReader
 
       .. versionchanged:: 3.13
 
-         The *separator* parameter may now be an :term:`iterable` of
+         The *separator* parameter may now be a :class:`tuple` of
          separators.
 
    .. method:: at_eof()
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index d394fbe3b0c357..65985ddc65a86f 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -324,6 +324,10 @@ asyncio
   :exc:`asyncio.QueueShutDown`) for queue termination.
   (Contributed by Laurie Opperman and Yves Duprat in :gh:`104228`.)
 
+* Accept a tuple of separators in :meth:`asyncio.StreamReader.readuntil`,
+  stopping when one of them is encountered.
+  (Contributed by Bruce Merry in :gh:`81322`.)
+
 base64
 ------
 
diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py
index 4517ca22d74637..64aac4cc50d15a 100644
--- a/Lib/asyncio/streams.py
+++ b/Lib/asyncio/streams.py
@@ -591,17 +591,17 @@ async def readuntil(self, separator=b'\n'):
         LimitOverrunError exception  will be raised, and the data
         will be left in the internal buffer, so it can be read again.
 
-        The ``separator`` may also be an iterable of separators. In this
+        The ``separator`` may also be a tuple of separators. In this
         case the return value will be the shortest possible that has any
         separator as the suffix. For the purposes of LimitOverrunError,
         the shortest possible separator is considered to be the one that
         matched.
         """
-        if isinstance(separator, bytes):
-            separator = [separator]
-        else:
-            # Makes sure shortest matches wins, and supports arbitrary 
iterables
+        if isinstance(separator, tuple):
+            # Makes sure shortest matches wins
             separator = sorted(separator, key=len)
+        else:
+            separator = [separator]
         if not separator:
             raise ValueError('Separator should contain at least one element')
         min_seplen = len(separator[0])
diff --git a/Lib/test/test_asyncio/test_streams.py 
b/Lib/test/test_asyncio/test_streams.py
index 792e88761acdc2..ae943f39869815 100644
--- a/Lib/test/test_asyncio/test_streams.py
+++ b/Lib/test/test_asyncio/test_streams.py
@@ -384,9 +384,9 @@ def test_readuntil_separator(self):
         with self.assertRaisesRegex(ValueError, 'Separator should be'):
             self.loop.run_until_complete(stream.readuntil(separator=b''))
         with self.assertRaisesRegex(ValueError, 'Separator should be'):
-            self.loop.run_until_complete(stream.readuntil(separator=[b'']))
+            self.loop.run_until_complete(stream.readuntil(separator=(b'',)))
         with self.assertRaisesRegex(ValueError, 'Separator should contain'):
-            self.loop.run_until_complete(stream.readuntil(separator=[]))
+            self.loop.run_until_complete(stream.readuntil(separator=()))
 
     def test_readuntil_multi_chunks(self):
         stream = asyncio.StreamReader(loop=self.loop)
@@ -475,15 +475,15 @@ def test_readuntil_multi_separator(self):
 
         # Simple case
         stream.feed_data(b'line 1\nline 2\r')
-        data = self.loop.run_until_complete(stream.readuntil([b'\r', b'\n']))
+        data = self.loop.run_until_complete(stream.readuntil((b'\r', b'\n')))
         self.assertEqual(b'line 1\n', data)
-        data = self.loop.run_until_complete(stream.readuntil([b'\r', b'\n']))
+        data = self.loop.run_until_complete(stream.readuntil((b'\r', b'\n')))
         self.assertEqual(b'line 2\r', data)
         self.assertEqual(b'', stream._buffer)
 
         # First end position matches, even if that's a longer match
         stream.feed_data(b'ABCDEFG')
-        data = self.loop.run_until_complete(stream.readuntil([b'DEF', 
b'BCDE']))
+        data = self.loop.run_until_complete(stream.readuntil((b'DEF', 
b'BCDE')))
         self.assertEqual(b'ABCDE', data)
         self.assertEqual(b'FG', stream._buffer)
 
@@ -493,7 +493,7 @@ def test_readuntil_multi_separator_limit(self):
 
         with self.assertRaisesRegex(asyncio.LimitOverrunError,
                                     'is found') as cm:
-            self.loop.run_until_complete(stream.readuntil([b'A', b'ome 
dataA']))
+            self.loop.run_until_complete(stream.readuntil((b'A', b'ome 
dataA')))
 
         self.assertEqual(b'some dataA', stream._buffer)
 
@@ -504,7 +504,7 @@ def test_readuntil_multi_separator_negative_offset(self):
         stream = asyncio.StreamReader(loop=self.loop)
         stream.feed_data(b'data')
 
-        readuntil_task = self.loop.create_task(stream.readuntil([b'A', b'long 
sep']))
+        readuntil_task = self.loop.create_task(stream.readuntil((b'A', b'long 
sep')))
         self.loop.call_soon(stream.feed_data, b'Z')
         self.loop.call_soon(stream.feed_data, b'Aaaa')
 
@@ -512,6 +512,13 @@ def test_readuntil_multi_separator_negative_offset(self):
         self.assertEqual(b'dataZA', data)
         self.assertEqual(b'aaa', stream._buffer)
 
+    def test_readuntil_bytearray(self):
+        stream = asyncio.StreamReader(loop=self.loop)
+        stream.feed_data(b'some data\r\n')
+        data = 
self.loop.run_until_complete(stream.readuntil(bytearray(b'\r\n')))
+        self.assertEqual(b'some data\r\n', data)
+        self.assertEqual(b'', stream._buffer)
+
     def test_readexactly_zero_or_less(self):
         # Read exact number of bytes (zero or less).
         stream = asyncio.StreamReader(loop=self.loop)
diff --git 
a/Misc/NEWS.d/next/Library/2024-04-10-20-59-10.gh-issue-117722.oxIUEI.rst 
b/Misc/NEWS.d/next/Library/2024-04-10-20-59-10.gh-issue-117722.oxIUEI.rst
new file mode 100644
index 00000000000000..de999883658898
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-04-10-20-59-10.gh-issue-117722.oxIUEI.rst
@@ -0,0 +1,2 @@
+Change the new multi-separator support in :meth:`asyncio.Stream.readuntil`
+to only accept tuples of separators rather than arbitrary iterables.

_______________________________________________
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]

Reply via email to