summaryrefslogtreecommitdiffstats
path: root/Modules/_io
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2013-09-04 18:52:14 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2013-09-04 18:52:14 (GMT)
commite619427f7eb17c370f31c6d4f7625eda8a0e9dce (patch)
tree7c14183bbff4e2935a6173f287ae210080b337d5 /Modules/_io
parent73821c47dcf56b02a30ab9ab524f0c0fc2875c22 (diff)
parente93b63b74b101c65807af7d9311de70012a2e49d (diff)
downloadcpython-e619427f7eb17c370f31c6d4f7625eda8a0e9dce.zip
cpython-e619427f7eb17c370f31c6d4f7625eda8a0e9dce.tar.gz
cpython-e619427f7eb17c370f31c6d4f7625eda8a0e9dce.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/_io')
-rw-r--r--Modules/_io/fileio.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index e757c82..27995e5 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;
char finalizing;
@@ -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;
@@ -219,7 +221,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;
@@ -317,8 +319,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;
@@ -349,10 +351,6 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
flags |= O_BINARY;
#endif
-#ifdef O_APPEND
- if (append)
- flags |= O_APPEND;
-#endif
#ifdef MS_WINDOWS
flags |= O_NOINHERIT;
#elif defined(O_CLOEXEC)
@@ -432,7 +430,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()). */
@@ -1019,7 +1017,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