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
an assertion failure in io.TextIOWrapper.write #75454
Comments
currently, the following causes an assertion in Modules/_io/textio.c in class BadEncoder():
def encode(self, dummy):
return 42
def _get_bad_encoder(dummy):
return BadEncoder()
quopri = codecs.lookup("quopri")
quopri._is_text_encoding = True
quopri.incrementalencoder = _get_bad_encoder
t = io.TextIOWrapper(io.BytesIO(b'foo'), encoding="quopri")
t.write('bar') this is because _io_TextIOWrapper_write_impl() doesn't check whether the value (I would open a PR to fix that soon.) |
I can't reproduce this on 3.6.0. Is this on 3.7 only? |
Just checked on current 3.6 on my Windows 10. |
As Serhiy pointed out on github, the assertion failure can be easily reproduced import codecs
import io
rot13 = codecs.lookup("rot13")
rot13._is_text_encoding = True
t = io.TextIOWrapper(io.BytesIO(b'foo'), encoding="rot13")
t.write('bar') |
Nick can be interested in this. |
The proposed fix looks good to me, but it did make me wonder if we might have a missing check in the other direction as well. However, it looks like that case is already fine:
|
May be make an error message more symmetric to the one in the decoder case? |
all three versions do 'self->pending_bytes_count += PyBytes_GET_SIZE(b);', in 3.6 and 3.7, the implementation of PyBytes_GET_SIZE() includes and so, in 2.7 there isn't an assertion failure. Moreover, do you still think we should backport to 2.7? |
This is not trivial question. On one side, using PyString_GET_SIZE() with non-bytes object definitely is a bug. It is better to catch it earlier rather than hope on failing in the following code. On other side, adding a check for bytes can break existing user code that is passed now by accident. _PyBytes_Join() in 2.7 supports unicode objects. I think that the fix should be backported, and the proper fix should allow bytes and unicode objects. But I left the decision on Benjamin. |
Oren, do you mind to create a backport to 2.7? miss-islington can not handle it. |
sure |
I am not sure, but ISTM that it isn't possible for the encoder to return a |
You are right. But in any case the test should be fixed for 2.7. |
Serhiy: "You are right. But in any case the test should be fixed for 2.7." Done: I merged Oren's PR 3951. It seems like the bug was fixed in all (maintained) branches, so I close the issue. Thank you again Oren Milman for *reporting* and *fixing* the bug! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: