Commit 55ca778
committed
pythongh-135862: fix asyncio socket partial writes of non-1d-binary arrays
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.1 parent 6227662 commit 55ca778
3 files changed
Lines changed: 46 additions & 2 deletions
File tree
- Lib
- asyncio
- test/test_asyncio
- Misc/NEWS.d/next/Library
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1077 | 1077 | | |
1078 | 1078 | | |
1079 | 1079 | | |
1080 | | - | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
1081 | 1085 | | |
1082 | 1086 | | |
1083 | 1087 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
| |||
768 | 774 | | |
769 | 775 | | |
770 | 776 | | |
771 | | - | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
772 | 810 | | |
773 | 811 | | |
774 | 812 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
0 commit comments