@@ -758,10 +758,8 @@ buffer_access_safe(textio *self)
758758 return NULL ;
759759 }
760760
761- /* Returning a borrowed reference is safe since TextIOWrapper methods are
762- protected by critical sections. */
763761 _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (self );
764- return self -> buffer ;
762+ return Py_NewRef ( self -> buffer ) ;
765763}
766764
767765static PyObject *
@@ -772,7 +770,9 @@ buffer_getattr(textio *self, PyObject *attr_name)
772770 return NULL ;
773771 }
774772
775- return PyObject_GetAttr (buffer , attr_name );
773+ PyObject * res = PyObject_GetAttr (buffer , attr_name );
774+ Py_DECREF (buffer );
775+ return res ;
776776}
777777
778778static PyObject *
@@ -783,7 +783,9 @@ buffer_callmethod_noargs(textio *self, PyObject *name)
783783 return NULL ;
784784 }
785785
786- return PyObject_CallMethodNoArgs (buffer , name );
786+ PyObject * res = PyObject_CallMethodNoArgs (buffer , name );
787+ Py_DECREF (buffer );
788+ return res ;
787789}
788790
789791static PyObject *
@@ -794,7 +796,9 @@ buffer_callmethod_onearg(textio *self, PyObject *name, PyObject *arg)
794796 return NULL ;
795797 }
796798
797- return PyObject_CallMethodOneArg (buffer , name , arg );
799+ PyObject * res = PyObject_CallMethodOneArg (buffer , name , arg );
800+ Py_DECREF (buffer );
801+ return res ;
798802}
799803
800804static void
@@ -1644,7 +1648,7 @@ _io_TextIOWrapper_detach_impl(textio *self)
16441648 if (buffer == NULL ) {
16451649 return NULL ;
16461650 }
1647- self -> buffer = NULL ;
1651+ Py_CLEAR ( self -> buffer ) ;
16481652 self -> detached = 1 ;
16491653 return buffer ;
16501654}
@@ -1868,7 +1872,12 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
18681872
18691873 if (needflush ) {
18701874 PyObject * buffer = buffer_access_safe (self );
1871- if (buffer == NULL || _PyFile_Flush (buffer ) < 0 ) {
1875+ if (buffer == NULL ) {
1876+ return NULL ;
1877+ }
1878+ int res = _PyFile_Flush (buffer );
1879+ Py_DECREF (buffer );
1880+ if (res < 0 ) {
18721881 return NULL ;
18731882 }
18741883 }
@@ -2687,6 +2696,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
26872696 goto fail ;
26882697 }
26892698 res = _PyObject_CallMethod (buf , & _Py_ID (seek ), "ii" , 0 , 2 );
2699+ Py_DECREF (buf );
26902700 Py_CLEAR (cookieObj );
26912701 if (res == NULL )
26922702 goto fail ;
@@ -3441,7 +3451,7 @@ static PyObject *
34413451_io_TextIOWrapper_buffer_get_impl (textio * self )
34423452/*[clinic end generated code: output=d265a34555aa5d4b input=5951cfa148f7350a]*/
34433453{
3444- return Py_XNewRef ( buffer_access_safe (self ) );
3454+ return buffer_access_safe (self );
34453455}
34463456
34473457static PyMethodDef incrementalnewlinedecoder_methods [] = {
0 commit comments