@@ -1944,25 +1944,21 @@ _bufferedwriter_reset_buf(buffered *self)
19441944static void
19451945_bufferedwriter_set_append (buffered * self )
19461946{
1947- PyObject * mode = _PyObject_GetAttrId (self -> raw , & PyId_mode );
1948- if (mode != NULL && PyUnicode_Check (mode )) {
1947+ PyObject * mode = NULL ;
1948+ if (PyObject_GetOptionalAttr (self -> raw , & _Py_ID (mode ), & mode ) < 0 ) {
1949+ /* Raw fileobj has no mode string so as far as we can know it has
1950+ normal write behavior */
1951+ self -> appending = 0 ;
1952+ } else if (mode != NULL && PyUnicode_Check (mode )) {
19491953 if (PyUnicode_FindChar (mode , 'a' , 0 ,
1950- PyUnicode_GET_LENGTH (mode ), 1 ) != -1 ) {
1954+ PyUnicode_GET_LENGTH (mode ), 1 ) >= 0 ) {
19511955 self -> appending = 1 ;
19521956 }
19531957 else {
19541958 self -> appending = 0 ;
19551959 }
19561960 Py_DECREF (mode );
19571961 }
1958- else {
1959- if (PyErr_ExceptionMatches (PyExc_AttributeError )) {
1960- PyErr_Clear ();
1961- }
1962- /* Raw fileobj has no mode string so as far as we can know it has
1963- normal write behavior */
1964- self -> appending = 0 ;
1965- }
19661962}
19671963
19681964/*[clinic input]
@@ -2062,14 +2058,13 @@ _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)
20622058static PyObject *
20632059_bufferedwriter_flush_unlocked (buffered * self )
20642060{
2065- Py_ssize_t written = 0 ;
20662061 Py_off_t n , rewind ;
20672062
20682063 if (!VALID_WRITE_BUFFER (self ) || self -> write_pos == self -> write_end )
20692064 goto end ;
20702065 /* First, rewind unless raw file is in append mode */
20712066 if (!self -> appending ) {
2072- Py_off_t rewind = RAW_OFFSET (self ) + (self -> pos - self -> write_pos );
2067+ rewind = RAW_OFFSET (self ) + (self -> pos - self -> write_pos );
20732068 if (rewind != 0 ) {
20742069 n = _buffered_raw_seek (self , - rewind , 1 );
20752070 if (n < 0 ) {
0 commit comments