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/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index b74cec2..e7955cc 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -305,7 +305,7 @@
finally:
os.unlink(TESTFN)
- def testModeStrings(self):
+ def testInvalidModeStrings(self):
# check invalid mode strings
for mode in ("", "aU", "wU+", "rw", "rt"):
try:
@@ -316,6 +316,21 @@
f.close()
self.fail('%r is an invalid file mode' % mode)
+ def testModeStrings(self):
+ # test that the mode attribute is correct for various mode strings
+ # given as init args
+ try:
+ for modes in [('w', 'wb'), ('wb', 'wb'), ('wb+', 'rb+'),
+ ('w+b', 'rb+'), ('a', 'ab'), ('ab', 'ab'),
+ ('ab+', 'ab+'), ('a+b', 'ab+'), ('r', 'rb'),
+ ('rb', 'rb'), ('rb+', 'rb+'), ('r+b', 'rb+')]:
+ # read modes are last so that TESTFN will exist first
+ with _FileIO(TESTFN, modes[0]) as f:
+ self.assertEqual(f.mode, modes[1])
+ finally:
+ if os.path.exists(TESTFN):
+ os.unlink(TESTFN)
+
def testUnicodeOpen(self):
# verify repr works for unicode too
f = _FileIO(str(TESTFN), "w")
diff --git a/Misc/ACKS b/Misc/ACKS
index 8977c02..8d29290 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -124,6 +124,7 @@
Georg Brandl
Christopher Brannon
Terrence Brannon
+Erik Bray
Brian Brazil
Dave Brennan
Tom Bridgman
diff --git a/Misc/NEWS b/Misc/NEWS
index afb7464..7ef400e 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,9 @@
Library
-------
+- Issue #18876: The FileIO.mode attribute now better reflects the actual mode
+ under which the file was opened. Patch by Erik Bray.
+
- Issue #18851: Avoid a double close of subprocess pipes when the child
process fails starting.
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