Skip to content

asyncio.StreamReader.readuntil is not general enough #76576

Closed

Description

@BruceMerry
BPO 32395
Nosy @asvetlov, @socketpair, @1st1, @tirkarthi
Superseder
  • bpo-37141: Allow multiple separators in Stream.readuntil
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-09-12.09:39:28.389>
    created_at = <Date 2017-12-21.05:52:54.042>
    labels = ['type-feature', '3.7', 'expert-asyncio']
    title = 'asyncio.StreamReader.readuntil is not general enough'
    updated_at = <Date 2019-09-12.09:39:28.388>
    user = 'https://bugs.python.org/BruceMerry'

    bugs.python.org fields:

    activity = <Date 2019-09-12.09:39:28.388>
    actor = 'asvetlov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-09-12.09:39:28.389>
    closer = 'asvetlov'
    components = ['asyncio']
    creation = <Date 2017-12-21.05:52:54.042>
    creator = 'Bruce Merry'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32395
    keywords = []
    message_count = 4.0
    messages = ['308852', '308853', '352078', '352080']
    nosy_count = 5.0
    nosy_names = ['asvetlov', 'socketpair', 'yselivanov', 'Bruce Merry', 'xtreak']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '37141'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32395'
    versions = ['Python 3.7']

    Activity

    1. BruceMerry commented on Dec 21, 2017

      BruceMerrymannequin
      MannequinAuthor

      I'd proposed one specific solution in bpo-32052 which asvetlov didn't like, so as requested I'm filing a bug about the problem rather than the solution.

      The specific case I have is reading a protocol in which either \r or \n can be used to terminate lines. With StreamReader.readuntil, it's only possible to specify one separator, so it can't easily be used (*).

      Some nice-to-have features, from specific to general:

      1. Specify multiple alternate separators.
      2. Specify a regex for a separator.
      3. Specify a regex for the line.
      4. Specify a callback that takes a string and returns the position of the end of the line, if any.

      Of course, some of these risk quadratic-time behaviour if they have to check the whole buffer every time the buffer is extended, so that would need to be considered in the design. In the last case, the callback could take care of it itself by maintaining internal state.

      (*) I actually have a solution for this case (https://github.com/ska-sa/aiokatcp/blob/bd8263cefe213003a218fac0dd8c5207cc76aeef/aiokatcp/connection.py#L44-L52), but it only works because \r and \n are semantically equivalent in the particular protocol I'm parsing.

    2. added
      3.7 (EOL)
      type-feature
      on Dec 21, 2017
    3. asvetlov commented on Dec 21, 2017

      @asvetlov
      Contributor

      Support multiple separators looks easy, I don't expect any performance impact.
      Like we already have it for strings: s.startswith(('\n', '\r'))

      Regexps are more expensive thing, callbacks are kind of evil.

      Let's add a patch for multiple separators first, maybe it covers 99.9% use cases.

    4. tirkarthi commented on Sep 12, 2019

      @tirkarthi
      Member

      I think this is a duplicate of bpo-37141 where multiple separators are requested for readuntil. I guess we can close one of them as duplicates.

    5. asvetlov commented on Sep 12, 2019

      @asvetlov
      Contributor

      Agree

    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)topic-asynciotype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions