Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.1k
gh-134706: Return bytes written from codecs.Stream(Reader)Writer.write()#134708
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I consider this a bug fix and propose to backport this change.
Misc/NEWS.d/next/Library/2025-05-26-12-44-40.gh-issue-134706.bcTycR.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Just a small suggestion for the NEWS
Actually, there is alsohttps://docs.python.org/3/library/asyncio-stream.html#asyncio.StreamWriter which doesn't return the number of bytes as well. |
I think this is a different case, as that seems to write asynchronously, so the number of bytes written is not known at the time |
cmaloney left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks good to me.
--
There are a number of cases in test mocks which do similarly I have in my backlog to make small patches for (Have a patch locally which makes a warning ifTextIOWrapper
gets something other than a number of bytes which finds a lot).
btw, can we add / modify a test for this? |
I was originally looking into this, but noticed that there were no tests for codecs at all. I can start a test suite, of course. |
I am not a fan of adding big amount of test code in the same trivial PR. So, please, let's create a new one with codecs tests 🙏 |
picnixz commentedMay 27, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
The codecs tests are in test_capi IIRC and/or in another place that I forgot |
srittau commentedMay 27, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There are actually two tests: One for the capi and one for the Python wrapper. They are in the obvious place, so I have no idea, how I could overlook them ... Will add tests.
Well, I was only going to add a test for this specific feature anyway, not a whole suite of tests. |
Could this be merged so the backport gets into the next 3.13 release which is scheduled for 2025-06-03? |
cc@malemburg as codecs expert and original author of this code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Should this change.writelines
as well?
srittau commentedMay 30, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Looking at thedocumentation for IOBase.writelines and the existing stub annotations, it seems unusual for |
It returns the number of bytes written, but |
I don't want to be a pest, but 3.13.4 is expected tomorrow, and I would like to update typeshed after that release while this is still fresh in my mind. |
I'd prefer to wait for a review from MAL, better to delay than rush a merge. |
I'm not sure whether it's a good idea to return a value from The Overall, I'm not convinced that returning a value provides much benefit. You can use In any case: This is a new feature, so I don't think it should go into a patch level release. |
Well, the current behavior has the downside that it may break code in unexpected ways, as it breaks the |
picnixz commentedJun 2, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
For now, I'll remove the backport labels; we'll add them back if needed. The fact that the contract is broken is a convincing argument though. |
I'm not sure where you get this idea from that there is some We could start to use the same approach as is done for the |
The contract to return the number of items written (characters or bytes) for synchronous I/O is found throughout the standard library, both in docs in implementation. For example, all types returned by
This is exactly what this PR does, although the documentation was removed when it was determined that this should be a bug fix. |
If returning the number of characters written is the part of the contract, then returning the number of bytes written breaks the contract. The Of course, been more compatible with TextIOWriter would be good. But we should not introduce a new subtle discrepancy. |
BTW, returning the number of bytes written is essential for unbuffered bytes stream, but I do not see a use case for text streams. |
That's actually a good point. The writers behave like a "character writer", not a "bytes writer" to the user. The fact that they write bytes behind the scenes does not matter to the user. So instead of returning the number of bytes written, we should actually return the number of characters written (consistently, without looking at the actual codec). This would also be consistent with other text writers: >>>withopen("foo","w")asf:...print(f.write("Föo"))...3 |
For character writers and non-blocking what number of characters should be returned if only part of a multi-byte character is written? (Non-blocking I/O is used with various pieces and usually doesn't guarantee whole "characters" are written. Going from None -> int returned is a safeish API change, int -> slightly different meaning int a lot less safe). |
Non-blocking I/O generally returns |
cmaloney commentedJun 2, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Also note: the implementation and docs differ on some cases although I'm actively working on getting the docs and implementation to match. |
Uh oh!
There was an error while loading.Please reload this page.
📚 Documentation preview 📚:https://cpython-previews--134708.org.readthedocs.build/