Skip to content

Correctly indicate the input type limitations of asyncio.write and asyncio.sendto #138703

@tecki

Description

@tecki

Bug report

Bug description:

When using any of the write or sendto methods in the asyncio library with anything that is not an instance of bytes, bytearray or memoryview, one gets the error message "data argument must be a bytes-like object". This is even true if one does pass a bytes-like object.

The reason is that the code checks for exactly those three types. But there are many other types which are bytes-like. Until now it is only a documentation bug: one could just fix the error message.

However, it is also a real bug, because if you hand in a non-contiguous memory view to those methods, they will not complain, however they will fail, and that in a not-so-nice way, as the methods will happily put the data into a buffer to be sent out later, and at this later time it will fail.

Unfortunately I am not aware of a good test whether something is bytes-like. The test should be: does it pass well with the PyArg_Parse y* format type, as this is, according to the documentation, the recommended way to accept binary data, and also what the underlying functions in asyncio do.

CPython versions tested on:

3.11

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirtopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions