Read data from the stream until ``separator`` is found. On success, the data and separator will be removed from the internal buffer (consumed). Returned data will include the separator at the end. Configured stream limit is used to check result. Limit sets the
(self, separator=b'\n')
| 570 | return line |
| 571 | |
| 572 | async def readuntil(self, separator=b'\n'): |
| 573 | """Read data from the stream until ``separator`` is found. |
| 574 | |
| 575 | On success, the data and separator will be removed from the |
| 576 | internal buffer (consumed). Returned data will include the |
| 577 | separator at the end. |
| 578 | |
| 579 | Configured stream limit is used to check result. Limit sets the |
| 580 | maximal length of data that can be returned, not counting the |
| 581 | separator. |
| 582 | |
| 583 | If an EOF occurs and the complete separator is still not found, |
| 584 | an IncompleteReadError exception will be raised, and the internal |
| 585 | buffer will be reset. The IncompleteReadError.partial attribute |
| 586 | may contain the separator partially. |
| 587 | |
| 588 | If the data cannot be read because of over limit, a |
| 589 | LimitOverrunError exception will be raised, and the data |
| 590 | will be left in the internal buffer, so it can be read again. |
| 591 | |
| 592 | The ``separator`` may also be a tuple of separators. In this |
| 593 | case the return value will be the shortest possible that has any |
| 594 | separator as the suffix. For the purposes of LimitOverrunError, |
| 595 | the shortest possible separator is considered to be the one that |
| 596 | matched. |
| 597 | """ |
| 598 | if isinstance(separator, tuple): |
| 599 | # Makes sure shortest matches wins |
| 600 | separator = sorted(separator, key=len) |
| 601 | else: |
| 602 | separator = [separator] |
| 603 | if not separator: |
| 604 | raise ValueError('Separator should contain at least one element') |
| 605 | min_seplen = len(separator[0]) |
| 606 | max_seplen = len(separator[-1]) |
| 607 | if min_seplen == 0: |
| 608 | raise ValueError('Separator should be at least one-byte string') |
| 609 | |
| 610 | if self._exception is not None: |
| 611 | raise self._exception |
| 612 | |
| 613 | # Consume whole buffer except last bytes, which length is |
| 614 | # one less than max_seplen. Let's check corner cases with |
| 615 | # separator[-1]='SEPARATOR': |
| 616 | # * we have received almost complete separator (without last |
| 617 | # byte). i.e buffer='some textSEPARATO'. In this case we |
| 618 | # can safely consume max_seplen - 1 bytes. |
| 619 | # * last byte of buffer is first byte of separator, i.e. |
| 620 | # buffer='abcdefghijklmnopqrS'. We may safely consume |
| 621 | # everything except that last byte, but this require to |
| 622 | # analyze bytes of buffer that match partial separator. |
| 623 | # This is slow and/or require FSM. For this case our |
| 624 | # implementation is not optimal, since require rescanning |
| 625 | # of data that is known to not belong to separator. In |
| 626 | # real world, separator will not be so long to notice |
| 627 | # performance problems. Even when reading MIME-encoded |
| 628 | # messages :) |
| 629 |