-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
asyncio tcp transport on Windows reads bytearray instead of bytes #99941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is it really only happening on Windows? I'm not sure what you mean by Fixing this might require copying the data which would adversely affect performance, effectively copying all data received an extra time. |
I've not tested it on linux, the docs clearly says https://docs.python.org/3/library/asyncio-protocol.html#asyncio.Protocol.data_received non empty |
Hmm, actually it says "bytes object", not " That said, the doc for |
I agree that the phrase "bytes object" in the docs refers to The cause of the regression (if it is that) is presumably GH-21442, which switches to Now, I am reluctant to switch back to |
Fair as long as the |
+1 for updating the docs to something like |
Hm, It is indeed the case that the I think it's fine if the docs state bytes-like. |
Totally agree, should be possible (ofc requiring testing) to wrap a memoryview, slice that which is a noop and bytes it instead with almost no performance drop |
So instead of But since the current behavior is already out in 3.10 and 3.11 maybe it's not worth fixing and we should just document that it's "bytes-like". Thoughts? |
In my opinion I'd keep bytes, cause passing mutable objects in the stream stack means that any protocol in between could mess up and break reading stuff (same with write in fact there was a discussion to disallow passing bytearray in writes, I think they settled in copying it with bytes on function call). Also all the reads (file like objects and everything) documentations must be changed or al least checked if we change this protocol. I'm not in the dev team for python tho, so I might not be the best person to ask this |
Do you feel comfortable submitting a PR? I think there are only two or three places that need to be changed, plus a unittest for each. |
Sure no problem, I'd see how to test performance with the example, I've also found that a similar thing is done in |
Another PR, #21446, efficiently implementing |
I'll give a look at the PR, it seems more broad then the actual problem. Now there are test cases for this so if it passes should be fine |
Bug report
asyncio.Protocol.data_received
prototype not respected: when usingcreate_connection
to create a tcp transportdata_received
is being called with abytearray
object instead ofbytes
.If this is expected behaviour libraries like httpx and such should be warned or the prototype modified, although I doubt it's intended because it would suppose the generator doesn't hold reference to it otherwise data could change while stored in buffered stuff.
Correct output:
@@@@@@@@@@ b'HTTP/1.0 ...WHATEVER THE SERVER ANSWERS...'
Faulty output:
@@@@@@@@@@ bytearray(b'HTTP/1.0 ...WHATEVER THE SERVER ANSWERS...')
Your environment
On Windows 11 x64
Tested on:
Working as expected in:
Related problems
Link to the thread where I found a conflicting thing (it's being solved there as a workaround)
encode/httpx#2305 (comment)
EDIT: simplified the minimal example
Linked PRs
The text was updated successfully, but these errors were encountered: