Issue #18876: The FileIO.mode attribute now better reflects the actual mode under which the file was opened.
Patch by Erik Bray.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 6cd7d81..5946e6a 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -47,6 +47,7 @@
int fd;
unsigned int readable : 1;
unsigned int writable : 1;
+ unsigned int appending : 1;
signed int seekable : 2; /* -1 means unknown */
unsigned int closefd : 1;
PyObject *weakreflist;
@@ -124,6 +125,7 @@
self->fd = -1;
self->readable = 0;
self->writable = 0;
+ self->appending = 0;
self->seekable = -1;
self->closefd = 1;
self->weakreflist = NULL;
@@ -184,7 +186,7 @@
Py_UNICODE *widename = NULL;
#endif
int ret = 0;
- int rwa = 0, plus = 0, append = 0;
+ int rwa = 0, plus = 0;
int flags = 0;
int fd = -1;
int closefd = 1;
@@ -279,8 +281,8 @@
goto bad_mode;
rwa = 1;
self->writable = 1;
- flags |= O_CREAT;
- append = 1;
+ self->appending = 1;
+ flags |= O_APPEND | O_CREAT;
break;
case 'b':
break;
@@ -311,11 +313,6 @@
flags |= O_BINARY;
#endif
-#ifdef O_APPEND
- if (append)
- flags |= O_APPEND;
-#endif
-
if (fd >= 0) {
if (check_fd(fd))
goto error;
@@ -356,7 +353,7 @@
if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
goto error;
- if (append) {
+ if (self->appending) {
/* For consistent behaviour, we explicitly seek to the
end of file (otherwise, it might be done only on the
first write()). */
@@ -898,7 +895,13 @@
static char *
mode_string(fileio *self)
{
- if (self->readable) {
+ if (self->appending) {
+ if (self->readable)
+ return "ab+";
+ else
+ return "ab";
+ }
+ else if (self->readable) {
if (self->writable)
return "rb+";
else