diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2013-09-04 18:46:33 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2013-09-04 18:46:33 (GMT) |
commit | e93b63b74b101c65807af7d9311de70012a2e49d (patch) | |
tree | 9add8e90484b309ab34081c03c05faa19624a61b /Modules | |
parent | c9e1dcdd5323b11d1d4e901dbc74287a2672bc04 (diff) | |
download | cpython-e93b63b74b101c65807af7d9311de70012a2e49d.zip cpython-e93b63b74b101c65807af7d9311de70012a2e49d.tar.gz cpython-e93b63b74b101c65807af7d9311de70012a2e49d.tar.bz2 |
Issue #18876: The FileIO.mode attribute now better reflects the actual mode under which the file was opened.
Patch by Erik Bray.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_io/fileio.c | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 2d0239e..f3ce776 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -49,6 +49,7 @@ typedef struct { unsigned int created : 1; unsigned int readable : 1; unsigned int writable : 1; + unsigned int appending : 1; signed int seekable : 2; /* -1 means unknown */ unsigned int closefd : 1; unsigned int deallocating: 1; @@ -156,6 +157,7 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) self->created = 0; self->readable = 0; self->writable = 0; + self->appending = 0; self->seekable = -1; self->closefd = 1; self->weakreflist = NULL; @@ -216,7 +218,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) 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; @@ -309,8 +311,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) 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; @@ -341,11 +343,6 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) flags |= O_BINARY; #endif -#ifdef O_APPEND - if (append) - flags |= O_APPEND; -#endif - if (fd >= 0) { if (check_fd(fd)) goto error; @@ -411,7 +408,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) 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()). */ @@ -1012,7 +1009,13 @@ mode_string(fileio *self) else return "xb"; } - if (self->readable) { + if (self->appending) { + if (self->readable) + return "ab+"; + else + return "ab"; + } + else if (self->readable) { if (self->writable) return "rb+"; else |