Issue #22982: Improve BOM handling when seeking to multiple positions of a writable text file.

This commit is contained in:
Antoine Pitrou 2015-04-13 20:02:33 +02:00
commit 56452eea39
4 changed files with 52 additions and 15 deletions

View file

@ -2048,11 +2048,10 @@ _textiowrapper_decoder_setstate(textio *self, cookie_type *cookie)
}
static int
_textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
_textiowrapper_encoder_reset(textio *self, int start_of_stream)
{
PyObject *res;
/* Same as _textiowrapper_decoder_setstate() above. */
if (cookie->start_pos == 0 && cookie->dec_flags == 0) {
if (start_of_stream) {
res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_reset, NULL);
self->encoding_start_of_stream = 1;
}
@ -2067,6 +2066,14 @@ _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
return 0;
}
static int
_textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
{
/* Same as _textiowrapper_decoder_setstate() above. */
return _textiowrapper_encoder_reset(
self, cookie->start_pos == 0 && cookie->dec_flags == 0);
}
static PyObject *
textiowrapper_seek(textio *self, PyObject *args)
{
@ -2134,7 +2141,17 @@ textiowrapper_seek(textio *self, PyObject *args)
}
res = _PyObject_CallMethodId(self->buffer, &PyId_seek, "ii", 0, 2);
Py_XDECREF(cookieObj);
Py_CLEAR(cookieObj);
if (res == NULL)
goto fail;
if (self->encoder) {
/* If seek() == 0, we are at the start of stream, otherwise not */
cmp = PyObject_RichCompareBool(res, _PyIO_zero, Py_EQ);
if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) {
Py_DECREF(res);
goto fail;
}
}
return res;
}
else if (whence != 0) {