Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 1 | import sys |
Fred Drake | 2ec80fa | 2000-10-23 16:59:35 +0000 | [diff] [blame] | 2 | import os |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3 | import unittest |
Neal Norwitz | 62f5a9d | 2002-04-01 00:09:00 +0000 | [diff] [blame] | 4 | from array import array |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 5 | from weakref import proxy |
Fred Drake | 2ec80fa | 2000-10-23 16:59:35 +0000 | [diff] [blame] | 6 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 7 | import io |
| 8 | import _pyio as pyio |
| 9 | |
Georg Brandl | 1b37e87 | 2010-03-14 10:45:50 +0000 | [diff] [blame] | 10 | from test.support import TESTFN, run_unittest |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 11 | from collections import UserList |
Marc-André Lemburg | fa44d79 | 2000-08-25 22:37:31 +0000 | [diff] [blame] | 12 | |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 13 | class AutoFileTests: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 14 | # file tests for which a test file is automatically set up |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 16 | def setUp(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 17 | self.f = self.open(TESTFN, 'wb') |
Tim Peters | 015dd82 | 2003-05-04 04:16:52 +0000 | [diff] [blame] | 18 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 19 | def tearDown(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | if self.f: |
| 21 | self.f.close() |
| 22 | os.remove(TESTFN) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 23 | |
| 24 | def testWeakRefs(self): |
| 25 | # verify weak references |
| 26 | p = proxy(self.f) |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 27 | p.write(b'teststring') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 28 | self.assertEqual(self.f.tell(), p.tell()) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 29 | self.f.close() |
| 30 | self.f = None |
| 31 | self.assertRaises(ReferenceError, getattr, p, 'tell') |
| 32 | |
| 33 | def testAttributes(self): |
| 34 | # verify expected attributes exist |
| 35 | f = self.f |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 36 | f.name # merely shouldn't blow up |
| 37 | f.mode # ditto |
| 38 | f.closed # ditto |
| 39 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 40 | def testReadinto(self): |
| 41 | # verify readinto |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 42 | self.f.write(b'12') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 43 | self.f.close() |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 44 | a = array('b', b'x'*10) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 45 | self.f = self.open(TESTFN, 'rb') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 46 | n = self.f.readinto(a) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 47 | self.assertEqual(b'12', a.tobytes()[:n]) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 48 | |
| 49 | def testReadinto_text(self): |
| 50 | # verify readinto refuses text files |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 51 | a = array('b', b'x'*10) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 52 | self.f.close() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 53 | self.f = self.open(TESTFN, 'r') |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 54 | if hasattr(self.f, "readinto"): |
| 55 | self.assertRaises(TypeError, self.f.readinto, a) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 56 | |
| 57 | def testWritelinesUserList(self): |
| 58 | # verify writelines with instance sequence |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 59 | l = UserList([b'1', b'2']) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 60 | self.f.writelines(l) |
| 61 | self.f.close() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 62 | self.f = self.open(TESTFN, 'rb') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 63 | buf = self.f.read() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 64 | self.assertEqual(buf, b'12') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 65 | |
| 66 | def testWritelinesIntegers(self): |
| 67 | # verify writelines with integers |
| 68 | self.assertRaises(TypeError, self.f.writelines, [1, 2, 3]) |
| 69 | |
| 70 | def testWritelinesIntegersUserList(self): |
| 71 | # verify writelines with integers in UserList |
| 72 | l = UserList([1,2,3]) |
| 73 | self.assertRaises(TypeError, self.f.writelines, l) |
| 74 | |
| 75 | def testWritelinesNonString(self): |
| 76 | # verify writelines with non-string object |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | class NonString: |
| 78 | pass |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 79 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | self.assertRaises(TypeError, self.f.writelines, |
| 81 | [NonString(), NonString()]) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 82 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 83 | def testErrors(self): |
| 84 | f = self.f |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 85 | self.assertEqual(f.name, TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 86 | self.assertTrue(not f.isatty()) |
| 87 | self.assertTrue(not f.closed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 89 | if hasattr(f, "readinto"): |
| 90 | self.assertRaises((IOError, TypeError), f.readinto, "") |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 91 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 92 | self.assertTrue(f.closed) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 93 | |
| 94 | def testMethods(self): |
Guido van Rossum | 4b5386f | 2007-07-10 09:12:49 +0000 | [diff] [blame] | 95 | methods = [('fileno', ()), |
| 96 | ('flush', ()), |
| 97 | ('isatty', ()), |
| 98 | ('__next__', ()), |
| 99 | ('read', ()), |
| 100 | ('write', (b"",)), |
| 101 | ('readline', ()), |
| 102 | ('readlines', ()), |
| 103 | ('seek', (0,)), |
| 104 | ('tell', ()), |
| 105 | ('write', (b"",)), |
| 106 | ('writelines', ([],)), |
| 107 | ('__iter__', ()), |
| 108 | ] |
Antoine Pitrou | 6103ab1 | 2009-10-24 20:11:21 +0000 | [diff] [blame] | 109 | methods.append(('truncate', ())) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 110 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 111 | # __exit__ should close the file |
| 112 | self.f.__exit__(None, None, None) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 113 | self.assertTrue(self.f.closed) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 4b5386f | 2007-07-10 09:12:49 +0000 | [diff] [blame] | 115 | for methodname, args in methods: |
| 116 | method = getattr(self.f, methodname) |
| 117 | # should raise on closed file |
| 118 | self.assertRaises(ValueError, method, *args) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 119 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 120 | # file is closed, __exit__ shouldn't do anything |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 121 | self.assertEqual(self.f.__exit__(None, None, None), None) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 122 | # it must also return None if an exception was given |
| 123 | try: |
| 124 | 1/0 |
| 125 | except: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 126 | self.assertEqual(self.f.__exit__(*sys.exc_info()), None) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 127 | |
Skip Montanaro | 80072cb | 2008-12-23 03:51:14 +0000 | [diff] [blame] | 128 | def testReadWhenWriting(self): |
| 129 | self.assertRaises(IOError, self.f.read) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 130 | |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 131 | class CAutoFileTests(AutoFileTests, unittest.TestCase): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 132 | open = io.open |
| 133 | |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 134 | class PyAutoFileTests(AutoFileTests, unittest.TestCase): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 135 | open = staticmethod(pyio.open) |
| 136 | |
| 137 | |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 138 | class OtherFileTests: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 139 | |
| 140 | def testModeStrings(self): |
| 141 | # check invalid mode strings |
| 142 | for mode in ("", "aU", "wU+"): |
| 143 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 144 | f = self.open(TESTFN, mode) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 145 | except ValueError: |
| 146 | pass |
| 147 | else: |
| 148 | f.close() |
| 149 | self.fail('%r is an invalid file mode' % mode) |
| 150 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 151 | def testBadModeArgument(self): |
| 152 | # verify that we get a sensible error message for bad mode argument |
| 153 | bad_mode = "qwerty" |
Tim Peters | cffcfed | 2006-02-14 17:41:18 +0000 | [diff] [blame] | 154 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 155 | f = self.open(TESTFN, bad_mode) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 156 | except ValueError as msg: |
Brett Cannon | ca477b2 | 2007-03-21 22:26:20 +0000 | [diff] [blame] | 157 | if msg.args[0] != 0: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 158 | s = str(msg) |
Ezio Melotti | 7fb4da7 | 2010-03-18 12:29:13 +0000 | [diff] [blame] | 159 | if TESTFN in s or bad_mode not in s: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 160 | self.fail("bad error message for invalid mode: %s" % s) |
Georg Brandl | 50da60c | 2008-01-06 21:38:54 +0000 | [diff] [blame] | 161 | # if msg.args[0] == 0, we're probably on Windows where there may be |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 162 | # no obvious way to discover why open() failed. |
| 163 | else: |
| 164 | f.close() |
| 165 | self.fail("no error for invalid mode: %s" % bad_mode) |
| 166 | |
| 167 | def testSetBufferSize(self): |
| 168 | # make sure that explicitly setting the buffer size doesn't cause |
| 169 | # misbehaviour especially with repeated close() calls |
| 170 | for s in (-1, 0, 1, 512): |
| 171 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 172 | f = self.open(TESTFN, 'wb', s) |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 173 | f.write(str(s).encode("ascii")) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 174 | f.close() |
| 175 | f.close() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 176 | f = self.open(TESTFN, 'rb', s) |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 177 | d = int(f.read().decode("ascii")) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 178 | f.close() |
| 179 | f.close() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 180 | except IOError as msg: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 181 | self.fail('error setting buffer size %d: %s' % (s, str(msg))) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 182 | self.assertEqual(d, s) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 183 | |
| 184 | def testTruncateOnWindows(self): |
Guido van Rossum | 79b79ee | 2007-10-25 23:21:03 +0000 | [diff] [blame] | 185 | # SF bug <http://www.python.org/sf/801631> |
| 186 | # "file.truncate fault on windows" |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 79b79ee | 2007-10-25 23:21:03 +0000 | [diff] [blame] | 188 | os.unlink(TESTFN) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 189 | f = self.open(TESTFN, 'wb') |
Guido van Rossum | 79b79ee | 2007-10-25 23:21:03 +0000 | [diff] [blame] | 190 | |
| 191 | try: |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 192 | f.write(b'12345678901') # 11 bytes |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 193 | f.close() |
| 194 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 195 | f = self.open(TESTFN,'rb+') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 196 | data = f.read(5) |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 197 | if data != b'12345': |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 198 | self.fail("Read on file opened for update failed %r" % data) |
| 199 | if f.tell() != 5: |
| 200 | self.fail("File pos after read wrong %d" % f.tell()) |
| 201 | |
| 202 | f.truncate() |
| 203 | if f.tell() != 5: |
| 204 | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 205 | |
| 206 | f.close() |
| 207 | size = os.path.getsize(TESTFN) |
| 208 | if size != 5: |
| 209 | self.fail("File size after ftruncate wrong %d" % size) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 210 | finally: |
Guido van Rossum | 79b79ee | 2007-10-25 23:21:03 +0000 | [diff] [blame] | 211 | f.close() |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 212 | os.unlink(TESTFN) |
| 213 | |
| 214 | def testIteration(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 215 | # Test the complex interaction when mixing file-iteration and the |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 216 | # various read* methods. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 217 | dataoffset = 16384 |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 218 | filler = b"ham\n" |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 219 | assert not dataoffset % len(filler), \ |
| 220 | "dataoffset must be multiple of len(filler)" |
| 221 | nchunks = dataoffset // len(filler) |
| 222 | testlines = [ |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 223 | b"spam, spam and eggs\n", |
| 224 | b"eggs, spam, ham and spam\n", |
| 225 | b"saussages, spam, spam and eggs\n", |
| 226 | b"spam, ham, spam and eggs\n", |
| 227 | b"spam, spam, spam, spam, spam, ham, spam\n", |
| 228 | b"wonderful spaaaaaam.\n" |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 229 | ] |
| 230 | methods = [("readline", ()), ("read", ()), ("readlines", ()), |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 231 | ("readinto", (array("b", b" "*100),))] |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 232 | |
| 233 | try: |
| 234 | # Prepare the testfile |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 235 | bag = self.open(TESTFN, "wb") |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 236 | bag.write(filler * nchunks) |
| 237 | bag.writelines(testlines) |
| 238 | bag.close() |
| 239 | # Test for appropriate errors mixing read* and iteration |
| 240 | for methodname, args in methods: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 241 | f = self.open(TESTFN, 'rb') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 242 | if next(f) != filler: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 243 | self.fail, "Broken testfile" |
| 244 | meth = getattr(f, methodname) |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 245 | meth(*args) # This simply shouldn't fail |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 246 | f.close() |
| 247 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 248 | # Test to see if harmless (by accident) mixing of read* and |
| 249 | # iteration still works. This depends on the size of the internal |
| 250 | # iteration buffer (currently 8192,) but we can test it in a |
| 251 | # flexible manner. Each line in the bag o' ham is 4 bytes |
| 252 | # ("h", "a", "m", "\n"), so 4096 lines of that should get us |
| 253 | # exactly on the buffer boundary for any power-of-2 buffersize |
| 254 | # between 4 and 16384 (inclusive). |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 255 | f = self.open(TESTFN, 'rb') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 256 | for i in range(nchunks): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 257 | next(f) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 258 | testline = testlines.pop(0) |
| 259 | try: |
| 260 | line = f.readline() |
| 261 | except ValueError: |
| 262 | self.fail("readline() after next() with supposedly empty " |
| 263 | "iteration-buffer failed anyway") |
| 264 | if line != testline: |
| 265 | self.fail("readline() after next() with empty buffer " |
| 266 | "failed. Got %r, expected %r" % (line, testline)) |
| 267 | testline = testlines.pop(0) |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 268 | buf = array("b", b"\x00" * len(testline)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 269 | try: |
| 270 | f.readinto(buf) |
| 271 | except ValueError: |
| 272 | self.fail("readinto() after next() with supposedly empty " |
| 273 | "iteration-buffer failed anyway") |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 274 | line = buf.tobytes() |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 275 | if line != testline: |
| 276 | self.fail("readinto() after next() with empty buffer " |
| 277 | "failed. Got %r, expected %r" % (line, testline)) |
| 278 | |
| 279 | testline = testlines.pop(0) |
| 280 | try: |
| 281 | line = f.read(len(testline)) |
| 282 | except ValueError: |
| 283 | self.fail("read() after next() with supposedly empty " |
| 284 | "iteration-buffer failed anyway") |
| 285 | if line != testline: |
| 286 | self.fail("read() after next() with empty buffer " |
| 287 | "failed. Got %r, expected %r" % (line, testline)) |
| 288 | try: |
| 289 | lines = f.readlines() |
| 290 | except ValueError: |
| 291 | self.fail("readlines() after next() with supposedly empty " |
| 292 | "iteration-buffer failed anyway") |
| 293 | if lines != testlines: |
| 294 | self.fail("readlines() after next() with empty buffer " |
| 295 | "failed. Got %r, expected %r" % (line, testline)) |
Antoine Pitrou | a6e9502 | 2010-10-30 14:22:43 +0000 | [diff] [blame] | 296 | f.close() |
| 297 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 298 | # Reading after iteration hit EOF shouldn't hurt either |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 299 | f = self.open(TESTFN, 'rb') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 300 | try: |
| 301 | for line in f: |
| 302 | pass |
| 303 | try: |
| 304 | f.readline() |
| 305 | f.readinto(buf) |
| 306 | f.read() |
| 307 | f.readlines() |
| 308 | except ValueError: |
| 309 | self.fail("read* failed after next() consumed file") |
| 310 | finally: |
| 311 | f.close() |
| 312 | finally: |
| 313 | os.unlink(TESTFN) |
| 314 | |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 315 | class COtherFileTests(OtherFileTests, unittest.TestCase): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 316 | open = io.open |
| 317 | |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 318 | class PyOtherFileTests(OtherFileTests, unittest.TestCase): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 319 | open = staticmethod(pyio.open) |
| 320 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 321 | |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 322 | def tearDownModule(): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 323 | # Historically, these tests have been sloppy about removing TESTFN. |
| 324 | # So get rid of it no matter what. |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 325 | if os.path.exists(TESTFN): |
| 326 | os.unlink(TESTFN) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 327 | |
| 328 | if __name__ == '__main__': |
Ezio Melotti | 3a03d2e | 2013-02-15 19:17:53 +0200 | [diff] [blame] | 329 | unittest.main() |