Guido van Rossum | de3bc7c | 2007-03-08 01:02:00 +0000 | [diff] [blame] | 1 | # Adapted from test_file.py by Daniel Stutzbach |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 2 | |
| 3 | import sys |
| 4 | import os |
Antoine Pitrou | 0ae29cf | 2009-03-13 22:33:17 +0000 | [diff] [blame] | 5 | import errno |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | from array import array |
| 8 | from weakref import proxy |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 9 | from functools import wraps |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 10 | |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 11 | from test.support import (TESTFN, findfile, check_warnings, run_unittest, |
| 12 | make_bad_fd) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 13 | from collections import UserList |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 14 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 15 | from _io import FileIO as _FileIO |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 16 | |
| 17 | class AutoFileTests(unittest.TestCase): |
| 18 | # file tests for which a test file is automatically set up |
| 19 | |
| 20 | def setUp(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 21 | self.f = _FileIO(TESTFN, 'w') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 22 | |
| 23 | def tearDown(self): |
| 24 | if self.f: |
| 25 | self.f.close() |
| 26 | os.remove(TESTFN) |
| 27 | |
| 28 | def testWeakRefs(self): |
| 29 | # verify weak references |
| 30 | p = proxy(self.f) |
| 31 | p.write(bytes(range(10))) |
| 32 | self.assertEquals(self.f.tell(), p.tell()) |
| 33 | self.f.close() |
| 34 | self.f = None |
| 35 | self.assertRaises(ReferenceError, getattr, p, 'tell') |
| 36 | |
| 37 | def testSeekTell(self): |
| 38 | self.f.write(bytes(range(20))) |
| 39 | self.assertEquals(self.f.tell(), 20) |
| 40 | self.f.seek(0) |
| 41 | self.assertEquals(self.f.tell(), 0) |
| 42 | self.f.seek(10) |
| 43 | self.assertEquals(self.f.tell(), 10) |
| 44 | self.f.seek(5, 1) |
| 45 | self.assertEquals(self.f.tell(), 15) |
| 46 | self.f.seek(-5, 1) |
| 47 | self.assertEquals(self.f.tell(), 10) |
| 48 | self.f.seek(-5, 2) |
| 49 | self.assertEquals(self.f.tell(), 15) |
| 50 | |
| 51 | def testAttributes(self): |
| 52 | # verify expected attributes exist |
| 53 | f = self.f |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 54 | |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 55 | self.assertEquals(f.mode, "wb") |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 56 | self.assertEquals(f.closed, False) |
| 57 | |
| 58 | # verify the attributes are readonly |
| 59 | for attr in 'mode', 'closed': |
| 60 | self.assertRaises((AttributeError, TypeError), |
| 61 | setattr, f, attr, 'oops') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 62 | |
| 63 | def testReadinto(self): |
| 64 | # verify readinto |
| 65 | self.f.write(bytes([1, 2])) |
| 66 | self.f.close() |
Guido van Rossum | a45ea58 | 2007-05-15 21:25:12 +0000 | [diff] [blame] | 67 | a = array('b', b'x'*10) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 68 | self.f = _FileIO(TESTFN, 'r') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 69 | n = self.f.readinto(a) |
| 70 | self.assertEquals(array('b', [1, 2]), a[:n]) |
| 71 | |
| 72 | def testRepr(self): |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 73 | self.assertEquals(repr(self.f), "<_io.FileIO name=%r mode=%r>" |
| 74 | % (self.f.name, self.f.mode)) |
| 75 | del self.f.name |
| 76 | self.assertEquals(repr(self.f), "<_io.FileIO fd=%r mode=%r>" |
| 77 | % (self.f.fileno(), self.f.mode)) |
| 78 | self.f.close() |
| 79 | self.assertEquals(repr(self.f), "<_io.FileIO [closed]>") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 80 | |
| 81 | def testErrors(self): |
| 82 | f = self.f |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 83 | self.assertTrue(not f.isatty()) |
| 84 | self.assertTrue(not f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 85 | #self.assertEquals(f.name, TESTFN) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 86 | self.assertRaises(ValueError, f.read, 10) # Open for reading |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 87 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 88 | self.assertTrue(f.closed) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 89 | f = _FileIO(TESTFN, 'r') |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 90 | self.assertRaises(TypeError, f.readinto, "") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 91 | self.assertTrue(not f.closed) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 92 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 93 | self.assertTrue(f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 94 | |
| 95 | def testMethods(self): |
| 96 | methods = ['fileno', 'isatty', 'read', 'readinto', |
| 97 | 'seek', 'tell', 'truncate', 'write', 'seekable', |
| 98 | 'readable', 'writable'] |
| 99 | if sys.platform.startswith('atheos'): |
| 100 | methods.remove('truncate') |
| 101 | |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 102 | self.f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 103 | self.assertTrue(self.f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 104 | |
| 105 | for methodname in methods: |
| 106 | method = getattr(self.f, methodname) |
| 107 | # should raise on closed file |
| 108 | self.assertRaises(ValueError, method) |
| 109 | |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 110 | def testOpendir(self): |
| 111 | # Issue 3703: opening a directory should fill the errno |
| 112 | # Windows always returns "[Errno 13]: Permission denied |
| 113 | # Unix calls dircheck() and returns "[Errno 21]: Is a directory" |
| 114 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 115 | _FileIO('.', 'r') |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 116 | except IOError as e: |
| 117 | self.assertNotEqual(e.errno, 0) |
Benjamin Peterson | 1efc23c | 2008-12-29 18:02:28 +0000 | [diff] [blame] | 118 | self.assertEqual(e.filename, ".") |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 119 | else: |
| 120 | self.fail("Should have raised IOError") |
| 121 | |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 122 | #A set of functions testing that we get expected behaviour if someone has |
| 123 | #manually closed the internal file descriptor. First, a decorator: |
| 124 | def ClosedFD(func): |
| 125 | @wraps(func) |
| 126 | def wrapper(self): |
| 127 | #forcibly close the fd before invoking the problem function |
| 128 | f = self.f |
| 129 | os.close(f.fileno()) |
| 130 | try: |
| 131 | func(self, f) |
| 132 | finally: |
| 133 | try: |
| 134 | self.f.close() |
| 135 | except IOError: |
| 136 | pass |
| 137 | return wrapper |
Antoine Pitrou | 0ae29cf | 2009-03-13 22:33:17 +0000 | [diff] [blame] | 138 | |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 139 | def ClosedFDRaises(func): |
| 140 | @wraps(func) |
| 141 | def wrapper(self): |
| 142 | #forcibly close the fd before invoking the problem function |
| 143 | f = self.f |
| 144 | os.close(f.fileno()) |
| 145 | try: |
| 146 | func(self, f) |
| 147 | except IOError as e: |
| 148 | self.assertEqual(e.errno, errno.EBADF) |
| 149 | else: |
| 150 | self.fail("Should have raised IOError") |
| 151 | finally: |
| 152 | try: |
| 153 | self.f.close() |
| 154 | except IOError: |
| 155 | pass |
| 156 | return wrapper |
| 157 | |
| 158 | @ClosedFDRaises |
| 159 | def testErrnoOnClose(self, f): |
| 160 | f.close() |
| 161 | |
| 162 | @ClosedFDRaises |
| 163 | def testErrnoOnClosedWrite(self, f): |
| 164 | f.write('a') |
| 165 | |
| 166 | @ClosedFDRaises |
| 167 | def testErrnoOnClosedSeek(self, f): |
| 168 | f.seek(0) |
| 169 | |
| 170 | @ClosedFDRaises |
| 171 | def testErrnoOnClosedTell(self, f): |
| 172 | f.tell() |
| 173 | |
| 174 | @ClosedFDRaises |
| 175 | def testErrnoOnClosedTruncate(self, f): |
| 176 | f.truncate(0) |
| 177 | |
| 178 | @ClosedFD |
| 179 | def testErrnoOnClosedSeekable(self, f): |
| 180 | f.seekable() |
| 181 | |
| 182 | @ClosedFD |
| 183 | def testErrnoOnClosedReadable(self, f): |
| 184 | f.readable() |
| 185 | |
| 186 | @ClosedFD |
| 187 | def testErrnoOnClosedWritable(self, f): |
| 188 | f.writable() |
| 189 | |
| 190 | @ClosedFD |
| 191 | def testErrnoOnClosedFileno(self, f): |
| 192 | f.fileno() |
| 193 | |
| 194 | @ClosedFD |
| 195 | def testErrnoOnClosedIsatty(self, f): |
| 196 | self.assertEqual(f.isatty(), False) |
| 197 | |
| 198 | def ReopenForRead(self): |
| 199 | try: |
| 200 | self.f.close() |
| 201 | except IOError: |
| 202 | pass |
| 203 | self.f = _FileIO(TESTFN, 'r') |
| 204 | os.close(self.f.fileno()) |
| 205 | return self.f |
| 206 | |
| 207 | @ClosedFDRaises |
| 208 | def testErrnoOnClosedRead(self, f): |
| 209 | f = self.ReopenForRead() |
| 210 | f.read(1) |
| 211 | |
| 212 | @ClosedFDRaises |
| 213 | def testErrnoOnClosedReadall(self, f): |
| 214 | f = self.ReopenForRead() |
| 215 | f.readall() |
| 216 | |
| 217 | @ClosedFDRaises |
| 218 | def testErrnoOnClosedReadinto(self, f): |
| 219 | f = self.ReopenForRead() |
| 220 | a = array('b', b'x'*10) |
| 221 | f.readinto(a) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 222 | |
| 223 | class OtherFileTests(unittest.TestCase): |
| 224 | |
| 225 | def testAbles(self): |
| 226 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 227 | f = _FileIO(TESTFN, "w") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 228 | self.assertEquals(f.readable(), False) |
| 229 | self.assertEquals(f.writable(), True) |
| 230 | self.assertEquals(f.seekable(), True) |
| 231 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 232 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 233 | f = _FileIO(TESTFN, "r") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 234 | self.assertEquals(f.readable(), True) |
| 235 | self.assertEquals(f.writable(), False) |
| 236 | self.assertEquals(f.seekable(), True) |
| 237 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 238 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 239 | f = _FileIO(TESTFN, "a+") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 240 | self.assertEquals(f.readable(), True) |
| 241 | self.assertEquals(f.writable(), True) |
| 242 | self.assertEquals(f.seekable(), True) |
| 243 | self.assertEquals(f.isatty(), False) |
| 244 | f.close() |
Guido van Rossum | 682faf8 | 2007-04-12 14:56:58 +0000 | [diff] [blame] | 245 | |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 246 | if sys.platform != "win32": |
| 247 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 248 | f = _FileIO("/dev/tty", "a") |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 249 | except EnvironmentError: |
| 250 | # When run in a cron job there just aren't any |
| 251 | # ttys, so skip the test. This also handles other |
| 252 | # OS'es that don't support /dev/tty. |
| 253 | pass |
| 254 | else: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 255 | f = _FileIO("/dev/tty", "a") |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 256 | self.assertEquals(f.readable(), False) |
| 257 | self.assertEquals(f.writable(), True) |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 258 | if sys.platform != "darwin" and \ |
Antoine Pitrou | d7b3046 | 2009-05-23 16:34:50 +0000 | [diff] [blame] | 259 | 'bsd' not in sys.platform and \ |
Benjamin Peterson | 94fe10f | 2008-07-17 23:27:26 +0000 | [diff] [blame] | 260 | not sys.platform.startswith('sunos'): |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 261 | # Somehow /dev/tty appears seekable on some BSDs |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 262 | self.assertEquals(f.seekable(), False) |
| 263 | self.assertEquals(f.isatty(), True) |
| 264 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 265 | finally: |
| 266 | os.unlink(TESTFN) |
| 267 | |
| 268 | def testModeStrings(self): |
| 269 | # check invalid mode strings |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 270 | for mode in ("", "aU", "wU+", "rw", "rt"): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 271 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 272 | f = _FileIO(TESTFN, mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 273 | except ValueError: |
| 274 | pass |
| 275 | else: |
| 276 | f.close() |
| 277 | self.fail('%r is an invalid file mode' % mode) |
| 278 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 279 | def testUnicodeOpen(self): |
| 280 | # verify repr works for unicode too |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 281 | f = _FileIO(str(TESTFN), "w") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 282 | f.close() |
| 283 | os.unlink(TESTFN) |
| 284 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 285 | def testBytesOpen(self): |
| 286 | # Opening a bytes filename |
| 287 | try: |
| 288 | fn = TESTFN.encode("ascii") |
| 289 | except UnicodeEncodeError: |
| 290 | # Skip test |
| 291 | return |
| 292 | f = _FileIO(fn, "w") |
| 293 | try: |
| 294 | f.write(b"abc") |
| 295 | f.close() |
| 296 | with open(TESTFN, "rb") as f: |
| 297 | self.assertEquals(f.read(), b"abc") |
| 298 | finally: |
| 299 | os.unlink(TESTFN) |
| 300 | |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 301 | def testInvalidFd(self): |
Antoine Pitrou | 7fb111b | 2009-03-04 11:14:01 +0000 | [diff] [blame] | 302 | self.assertRaises(ValueError, _FileIO, -10) |
| 303 | self.assertRaises(OSError, _FileIO, make_bad_fd()) |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 305 | def testBadModeArgument(self): |
| 306 | # verify that we get a sensible error message for bad mode argument |
| 307 | bad_mode = "qwerty" |
| 308 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 309 | f = _FileIO(TESTFN, bad_mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 310 | except ValueError as msg: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 311 | if msg.args[0] != 0: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 312 | s = str(msg) |
| 313 | if s.find(TESTFN) != -1 or s.find(bad_mode) == -1: |
| 314 | self.fail("bad error message for invalid mode: %s" % s) |
Georg Brandl | 50da60c | 2008-01-06 21:38:54 +0000 | [diff] [blame] | 315 | # if msg.args[0] == 0, we're probably on Windows where there may be |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 316 | # no obvious way to discover why open() failed. |
| 317 | else: |
| 318 | f.close() |
| 319 | self.fail("no error for invalid mode: %s" % bad_mode) |
| 320 | |
| 321 | def testTruncateOnWindows(self): |
| 322 | def bug801631(): |
| 323 | # SF bug <http://www.python.org/sf/801631> |
| 324 | # "file.truncate fault on windows" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 325 | f = _FileIO(TESTFN, 'w') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 326 | f.write(bytes(range(11))) |
| 327 | f.close() |
| 328 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 329 | f = _FileIO(TESTFN,'r+') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 330 | data = f.read(5) |
| 331 | if data != bytes(range(5)): |
| 332 | self.fail("Read on file opened for update failed %r" % data) |
| 333 | if f.tell() != 5: |
| 334 | self.fail("File pos after read wrong %d" % f.tell()) |
| 335 | |
| 336 | f.truncate() |
| 337 | if f.tell() != 5: |
| 338 | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 339 | |
| 340 | f.close() |
| 341 | size = os.path.getsize(TESTFN) |
| 342 | if size != 5: |
| 343 | self.fail("File size after ftruncate wrong %d" % size) |
| 344 | |
| 345 | try: |
| 346 | bug801631() |
| 347 | finally: |
| 348 | os.unlink(TESTFN) |
| 349 | |
Walter Dörwald | 3a77c7a | 2007-06-06 16:31:14 +0000 | [diff] [blame] | 350 | def testAppend(self): |
| 351 | try: |
| 352 | f = open(TESTFN, 'wb') |
| 353 | f.write(b'spam') |
| 354 | f.close() |
| 355 | f = open(TESTFN, 'ab') |
| 356 | f.write(b'eggs') |
| 357 | f.close() |
| 358 | f = open(TESTFN, 'rb') |
| 359 | d = f.read() |
| 360 | f.close() |
| 361 | self.assertEqual(d, b'spameggs') |
| 362 | finally: |
| 363 | try: |
| 364 | os.unlink(TESTFN) |
| 365 | except: |
| 366 | pass |
| 367 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 368 | def testInvalidInit(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 369 | self.assertRaises(TypeError, _FileIO, "1", 0, 0) |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 370 | |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 371 | def testWarnings(self): |
| 372 | with check_warnings() as w: |
| 373 | self.assertEqual(w.warnings, []) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 374 | self.assertRaises(TypeError, _FileIO, []) |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 375 | self.assertEqual(w.warnings, []) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 376 | self.assertRaises(ValueError, _FileIO, "/some/invalid/name", "rt") |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 377 | self.assertEqual(w.warnings, []) |
| 378 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 380 | def test_main(): |
| 381 | # Historically, these tests have been sloppy about removing TESTFN. |
| 382 | # So get rid of it no matter what. |
| 383 | try: |
| 384 | run_unittest(AutoFileTests, OtherFileTests) |
| 385 | finally: |
| 386 | if os.path.exists(TESTFN): |
| 387 | os.unlink(TESTFN) |
| 388 | |
| 389 | if __name__ == '__main__': |
| 390 | test_main() |