@@ -49,6 +49,7 @@ typedef struct {
4949 unsigned int created : 1 ;
5050 unsigned int readable : 1 ;
5151 unsigned int writable : 1 ;
52+ unsigned int appending : 1 ;
5253 signed int seekable : 2 ; /* -1 means unknown */
5354 unsigned int closefd : 1 ;
5455 unsigned int deallocating : 1 ;
@@ -156,6 +157,7 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
156157 self -> created = 0 ;
157158 self -> readable = 0 ;
158159 self -> writable = 0 ;
160+ self -> appending = 0 ;
159161 self -> seekable = -1 ;
160162 self -> closefd = 1 ;
161163 self -> weakreflist = NULL ;
@@ -216,7 +218,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
216218 Py_UNICODE * widename = NULL ;
217219#endif
218220 int ret = 0 ;
219- int rwa = 0 , plus = 0 , append = 0 ;
221+ int rwa = 0 , plus = 0 ;
220222 int flags = 0 ;
221223 int fd = -1 ;
222224 int closefd = 1 ;
@@ -309,8 +311,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
309311 goto bad_mode ;
310312 rwa = 1 ;
311313 self -> writable = 1 ;
312- flags |= O_CREAT ;
313- append = 1 ;
314+ self -> appending = 1 ;
315+ flags |= O_APPEND | O_CREAT ;
314316 break ;
315317 case 'b' :
316318 break ;
@@ -341,11 +343,6 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
341343 flags |= O_BINARY ;
342344#endif
343345
344- #ifdef O_APPEND
345- if (append )
346- flags |= O_APPEND ;
347- #endif
348-
349346 if (fd >= 0 ) {
350347 if (check_fd (fd ))
351348 goto error ;
@@ -411,7 +408,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
411408 if (PyObject_SetAttrString ((PyObject * )self , "name" , nameobj ) < 0 )
412409 goto error ;
413410
414- if (append ) {
411+ if (self -> appending ) {
415412 /* For consistent behaviour, we explicitly seek to the
416413 end of file (otherwise, it might be done only on the
417414 first write()). */
@@ -1012,7 +1009,13 @@ mode_string(fileio *self)
10121009 else
10131010 return "xb" ;
10141011 }
1015- if (self -> readable ) {
1012+ if (self -> appending ) {
1013+ if (self -> readable )
1014+ return "ab+" ;
1015+ else
1016+ return "ab" ;
1017+ }
1018+ else if (self -> readable ) {
10161019 if (self -> writable )
10171020 return "rb+" ;
10181021 else
0 commit comments