Commit b3d98e8
committed
remove unnecessary and buggy type checks in asyncio
Within asyncio, several `write`, `send` and `sendto` methods
contained a check whether the `data` parameter is one of the
*right* types, complaining that the data should be bytes-like.
This error message is very misleading if one hands over a byte-like
object that happens not to be one of the *right* types.
Usually, these kinds of tests are not necessary at all, as the
code the data is handed to will complain itself if the type is wrong.
But in the case of the mentioned methods, the data may be put into
a buffer if the data cannot be sent immediately. Any type errors
will only be raised much later, when the buffer is finally sent.
Interestingly, the test is incorrect as well: any memoryview is
considered *right*, although it may be passed to functions that
cannot deal with non-contiguous memoryviews, in which case the all
the problems that the test should protect from reappear.
There are several options one can go forward:
* one could update the documentation to reflect the fact that not
all bytes-like objects can be passed, but only special ones. This
would be unfortunate as the underlying code actually can deal with
all bytes-like data.
* actually test whether the data is bytes-like. This is unfortunately
not easy. The correct test would be to check whether the data can
be parsed as by PyArg_Parse with a y* format. I am not aware of
a simple test like this.
* Remove the test. In this case one should assure that only bytes-like
data will be put into the buffers if the data cannot be sent
immediately.
For simplicity I opted for the last option. One should note another
problem here: if we accept objects like memoryview or bytearray
(which we currently do), the user may modify the data after-the-fact,
which will lead to weird, unexpected behavior. This could be mitigated
by always copying the data. This is done in some of the modified
methods, but not all, most likely for performance reasons. I think
it would be beneficial to deal with this problem in a systematic way,
but this is beyond the scope of this patch.1 parent b9c50b4 commit b3d98e8
File tree
4 files changed
+10
-23
lines changed- Lib
- asyncio
- test/test_asyncio
4 files changed
+10
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
336 | 336 | | |
337 | 337 | | |
338 | 338 | | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | 339 | | |
344 | 340 | | |
345 | 341 | | |
| |||
485 | 481 | | |
486 | 482 | | |
487 | 483 | | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | 484 | | |
493 | 485 | | |
494 | 486 | | |
| |||
500 | 492 | | |
501 | 493 | | |
502 | 494 | | |
503 | | - | |
| 495 | + | |
| 496 | + | |
504 | 497 | | |
505 | 498 | | |
506 | 499 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1049 | 1049 | | |
1050 | 1050 | | |
1051 | 1051 | | |
1052 | | - | |
1053 | | - | |
1054 | | - | |
1055 | 1052 | | |
1056 | 1053 | | |
1057 | 1054 | | |
| |||
1071 | 1068 | | |
1072 | 1069 | | |
1073 | 1070 | | |
1074 | | - | |
| 1071 | + | |
1075 | 1072 | | |
1076 | 1073 | | |
1077 | 1074 | | |
| |||
1084 | 1081 | | |
1085 | 1082 | | |
1086 | 1083 | | |
1087 | | - | |
| 1084 | + | |
1088 | 1085 | | |
1089 | 1086 | | |
1090 | 1087 | | |
| |||
1255 | 1252 | | |
1256 | 1253 | | |
1257 | 1254 | | |
1258 | | - | |
1259 | | - | |
1260 | | - | |
1261 | | - | |
1262 | 1255 | | |
1263 | 1256 | | |
1264 | 1257 | | |
| |||
1284 | 1277 | | |
1285 | 1278 | | |
1286 | 1279 | | |
1287 | | - | |
| 1280 | + | |
1288 | 1281 | | |
1289 | 1282 | | |
1290 | 1283 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | 217 | | |
221 | 218 | | |
222 | | - | |
| 219 | + | |
| 220 | + | |
223 | 221 | | |
224 | 222 | | |
225 | 223 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1499 | 1499 | | |
1500 | 1500 | | |
1501 | 1501 | | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
1502 | 1505 | | |
1503 | 1506 | | |
1504 | 1507 | | |
| |||
0 commit comments