@@ -753,10 +753,8 @@ buffer_access_safe(textio *self)
753753 return NULL ;
754754 }
755755
756- /* Returning a borrowed reference is safe since TextIOWrapper methods are
757- protected by critical sections. */
758756 _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (self );
759- return self -> buffer ;
757+ return Py_NewRef ( self -> buffer ) ;
760758}
761759
762760static PyObject *
@@ -767,7 +765,9 @@ buffer_getattr(textio *self, PyObject *attr_name)
767765 return NULL ;
768766 }
769767
770- return PyObject_GetAttr (buffer , attr_name );
768+ PyObject * res = PyObject_GetAttr (buffer , attr_name );
769+ Py_DECREF (buffer );
770+ return res ;
771771}
772772
773773static PyObject *
@@ -778,7 +778,9 @@ buffer_callmethod_noargs(textio *self, PyObject *name)
778778 return NULL ;
779779 }
780780
781- return PyObject_CallMethodNoArgs (buffer , name );
781+ PyObject * res = PyObject_CallMethodNoArgs (buffer , name );
782+ Py_DECREF (buffer );
783+ return res ;
782784}
783785
784786static PyObject *
@@ -789,7 +791,9 @@ buffer_callmethod_onearg(textio *self, PyObject *name, PyObject *arg)
789791 return NULL ;
790792 }
791793
792- return PyObject_CallMethodOneArg (buffer , name , arg );
794+ PyObject * res = PyObject_CallMethodOneArg (buffer , name , arg );
795+ Py_DECREF (buffer );
796+ return res ;
793797}
794798
795799static void
@@ -1639,7 +1643,7 @@ _io_TextIOWrapper_detach_impl(textio *self)
16391643 if (buffer == NULL ) {
16401644 return NULL ;
16411645 }
1642- self -> buffer = NULL ;
1646+ Py_CLEAR ( self -> buffer ) ;
16431647 self -> detached = 1 ;
16441648 return buffer ;
16451649}
@@ -1860,7 +1864,12 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
18601864
18611865 if (needflush ) {
18621866 PyObject * buffer = buffer_access_safe (self );
1863- if (buffer == NULL || _PyFile_Flush (buffer ) < 0 ) {
1867+ if (buffer == NULL ) {
1868+ return NULL ;
1869+ }
1870+ int res = _PyFile_Flush (buffer );
1871+ Py_DECREF (buffer );
1872+ if (res < 0 ) {
18641873 return NULL ;
18651874 }
18661875 }
@@ -2679,6 +2688,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
26792688 goto fail ;
26802689 }
26812690 res = _PyObject_CallMethod (buf , & _Py_ID (seek ), "ii" , 0 , 2 );
2691+ Py_DECREF (buf );
26822692 Py_CLEAR (cookieObj );
26832693 if (res == NULL )
26842694 goto fail ;
@@ -3436,7 +3446,7 @@ static PyObject *
34363446_io_TextIOWrapper_buffer_get_impl (textio * self )
34373447/*[clinic end generated code: output=d265a34555aa5d4b input=5951cfa148f7350a]*/
34383448{
3439- return Py_XNewRef ( buffer_access_safe (self ) );
3449+ return buffer_access_safe (self );
34403450}
34413451
34423452static PyMethodDef incrementalnewlinedecoder_methods [] = {
0 commit comments