From 44309e6b3768d6d559491ddf6ca56bfad2adb9b5 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 22 Nov 2008 00:41:45 +0000 Subject: [PATCH] make FileIO.mode always include 'b' #4386 Reviewed by Amaury --- Doc/library/stdtypes.rst | 2 ++ Lib/socket.py | 4 +++- Lib/test/test_fileio.py | 4 ++-- Lib/test/test_gzip.py | 2 +- Lib/test/test_io.py | 14 +++++++------- Lib/test/test_socket.py | 4 ++-- Misc/NEWS | 2 ++ Modules/_fileio.c | 8 +++++--- 8 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index f0d51e26f29..5668623ed34 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -1875,6 +1875,8 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: view objects. +.. _dict-views: + Dictionary view objects ----------------------- diff --git a/Lib/socket.py b/Lib/socket.py index 5578b13b1a3..045987c0860 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -198,10 +198,12 @@ class SocketIO(io.RawIOBase): # XXX More docs def __init__(self, sock, mode): - if mode not in ("r", "w", "rw"): + if mode not in ("r", "w", "rw", "rb", "wb", "rwb"): raise ValueError("invalid mode: %r" % mode) io.RawIOBase.__init__(self) self._sock = sock + if "b" not in mode: + mode += "b" self._mode = mode self._reading = "r" in mode self._writing = "w" in mode diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index a11d3ba1173..80de93ddb9f 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -49,7 +49,7 @@ class AutoFileTests(unittest.TestCase): # verify expected attributes exist f = self.f - self.assertEquals(f.mode, "w") + self.assertEquals(f.mode, "wb") self.assertEquals(f.closed, False) # verify the attributes are readonly @@ -159,7 +159,7 @@ class OtherFileTests(unittest.TestCase): def testModeStrings(self): # check invalid mode strings - for mode in ("", "aU", "wU+", "rb", "rt"): + for mode in ("", "aU", "wU+", "rw", "rt"): try: f = _fileio._FileIO(TESTFN, mode) except ValueError: diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 3493b8dfba6..d28c024d07f 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -150,7 +150,7 @@ class TestGzip(unittest.TestCase): def test_mode(self): self.test_write() f = gzip.GzipFile(self.filename, 'r') - self.assertTrue(f.myfileobj.mode.startswith('r')) + self.assertEqual(f.myfileobj.mode, 'rb') f.close() def test_1647484(self): diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 79cfd8a686e..58203ed2ff0 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1266,7 +1266,7 @@ class MiscIOTest(unittest.TestCase): def test_attributes(self): f = io.open(support.TESTFN, "wb", buffering=0) - self.assertEquals(f.mode, "w") + self.assertEquals(f.mode, "wb") f.close() f = io.open(support.TESTFN, "U") @@ -1274,18 +1274,18 @@ class MiscIOTest(unittest.TestCase): self.assertEquals(f.buffer.name, support.TESTFN) self.assertEquals(f.buffer.raw.name, support.TESTFN) self.assertEquals(f.mode, "U") - self.assertEquals(f.buffer.mode, "r") - self.assertEquals(f.buffer.raw.mode, "r") + self.assertEquals(f.buffer.mode, "rb") + self.assertEquals(f.buffer.raw.mode, "rb") f.close() f = io.open(support.TESTFN, "w+") self.assertEquals(f.mode, "w+") - self.assertEquals(f.buffer.mode, "r+") # Does it really matter? - self.assertEquals(f.buffer.raw.mode, "r+") + self.assertEquals(f.buffer.mode, "rb+") # Does it really matter? + self.assertEquals(f.buffer.raw.mode, "rb+") g = io.open(f.fileno(), "wb", closefd=False) - self.assertEquals(g.mode, "w") - self.assertEquals(g.raw.mode, "w") + self.assertEquals(g.mode, "wb") + self.assertEquals(g.raw.mode, "wb") self.assertEquals(g.name, f.fileno()) self.assertEquals(g.raw.name, f.fileno()) f.close() diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index b2323fe7fd2..51f41a9f2ef 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -849,11 +849,11 @@ class FileObjectClassTestCase(SocketConnectedTest): self.assert_(not self.cli_file.closed) def testAttributes(self): - self.assertEqual(self.serv_file.mode, 'r') + self.assertEqual(self.serv_file.mode, 'rb') self.assertEqual(self.serv_file.name, self.cli_conn.fileno()) def _testAttributes(self): - self.assertEqual(self.cli_file.mode, 'w') + self.assertEqual(self.cli_file.mode, 'wb') self.assertEqual(self.cli_file.name, self.serv_conn.fileno()) class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): diff --git a/Misc/NEWS b/Misc/NEWS index 1e61e549dae..4e797b211d6 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -48,6 +48,8 @@ Core and Builtins Library ------- +- FileIO's mode attribute now always includes ``"b"``. + - Issue #3799: Fix dbm.dumb to accept strings as well as bytes for keys. String keys are now written out in UTF-8. diff --git a/Modules/_fileio.c b/Modules/_fileio.c index 2cec2132235..0a34eb3be5f 100644 --- a/Modules/_fileio.c +++ b/Modules/_fileio.c @@ -208,6 +208,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) flags |= O_CREAT; append = 1; break; + case 'b': + break; case '+': if (plus) goto bad_mode; @@ -682,12 +684,12 @@ mode_string(PyFileIOObject *self) { if (self->readable) { if (self->writable) - return "r+"; + return "rb+"; else - return "r"; + return "rb"; } else - return "w"; + return "wb"; } static PyObject *