@@ -2048,11 +2048,10 @@ _textiowrapper_decoder_setstate(textio *self, cookie_type *cookie)
20482048}
20492049
20502050static int
2051- _textiowrapper_encoder_setstate (textio * self , cookie_type * cookie )
2051+ _textiowrapper_encoder_reset (textio * self , int start_of_stream )
20522052{
20532053 PyObject * res ;
2054- /* Same as _textiowrapper_decoder_setstate() above. */
2055- if (cookie -> start_pos == 0 && cookie -> dec_flags == 0 ) {
2054+ if (start_of_stream ) {
20562055 res = PyObject_CallMethodObjArgs (self -> encoder , _PyIO_str_reset , NULL );
20572056 self -> encoding_start_of_stream = 1 ;
20582057 }
@@ -2067,6 +2066,14 @@ _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
20672066 return 0 ;
20682067}
20692068
2069+ static int
2070+ _textiowrapper_encoder_setstate (textio * self , cookie_type * cookie )
2071+ {
2072+ /* Same as _textiowrapper_decoder_setstate() above. */
2073+ return _textiowrapper_encoder_reset (
2074+ self , cookie -> start_pos == 0 && cookie -> dec_flags == 0 );
2075+ }
2076+
20702077static PyObject *
20712078textiowrapper_seek (textio * self , PyObject * args )
20722079{
@@ -2134,7 +2141,17 @@ textiowrapper_seek(textio *self, PyObject *args)
21342141 }
21352142
21362143 res = _PyObject_CallMethodId (self -> buffer , & PyId_seek , "ii" , 0 , 2 );
2137- Py_XDECREF (cookieObj );
2144+ Py_CLEAR (cookieObj );
2145+ if (res == NULL )
2146+ goto fail ;
2147+ if (self -> encoder ) {
2148+ /* If seek() == 0, we are at the start of stream, otherwise not */
2149+ cmp = PyObject_RichCompareBool (res , _PyIO_zero , Py_EQ );
2150+ if (cmp < 0 || _textiowrapper_encoder_reset (self , cmp )) {
2151+ Py_DECREF (res );
2152+ goto fail ;
2153+ }
2154+ }
21382155 return res ;
21392156 }
21402157 else if (whence != 0 ) {
0 commit comments