Skip to content

Conversation

@duaneg
Copy link
Contributor

@duaneg duaneg commented Jun 26, 2025

The asyncio code is writing binary data to a socket.socket, and advancing through the buffer after a partial write by doing memoryview(data)[n:], where n is the number of bytes written.

This assumes the memoryview is a one dimensional buffer of bytes, which is not the case e.g. for a memoryview of an array.array of non-bytes, or an ndarray with more than one dimension. Partial writes of such memoryviews will corrupt the stream, repeating or omitting some data.

When creating a memoryview from a memoryview, first convert it to a one-dimensional array of bytes before taking the remaining slice, thus avoiding these issues.

Add an assertion that the remaining data is bytes, to guard against possible regressions if additional types of data are allowed in the future.

…rrays

The :mod:`asyncio` code is writing binary data to a :class:`socket.socket`, and
advancing through the buffer after a partial write by doing
``memoryview(data)[n:]``, where ``n`` is the number of bytes written.

This assumes the :class:`memoryview` is a one dimensional buffer of bytes,
which is not the case e.g. for a :class:`memoryview` of an :class:`array.array`
of non-bytes, or an ``ndarray`` with more than one dimension. Partial writes of
such :class:`memoryview`s will corrupt the stream, repeating or omitting some
data.

When creating a :class:`memoryview` from a :class:`memoryview`, first convert
it to a one-dimensional array of bytes before taking the remaining slice, thus
avoiding these issues.

Add an assertion that the remaining data is bytes, to guard against possible
regressions if additional types of data are allowed in the future.
duaneg added 2 commits June 26, 2025 23:26
Hopefully this will fix the windows compile failures...
@duaneg duaneg closed this Jun 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant