| 1 |
# Adapted from test_file.py by Daniel Stutzbach |
| 2 |
|
| 3 |
import sys |
| 4 |
import os |
| 5 |
import io |
| 6 |
import errno |
| 7 |
import unittest |
| 8 |
from array import array |
| 9 |
from weakref import proxy |
| 10 |
from functools import wraps |
| 11 |
|
| 12 |
from test.support import ( |
| 13 |
cpython_only, swap_attr, gc_collect, is_emscripten, is_wasi, |
| 14 |
infinite_recursion, |
| 15 |
) |
| 16 |
from test.support.os_helper import ( |
| 17 |
TESTFN, TESTFN_ASCII, TESTFN_UNICODE, make_bad_fd, |
| 18 |
) |
| 19 |
from test.support.warnings_helper import check_warnings |
| 20 |
from test.support.import_helper import import_module |
| 21 |
from collections import UserList |
| 22 |
|
| 23 |
import _io # C implementation of io |
| 24 |
import _pyio # Python implementation of io |
| 25 |
|
| 26 |
|
| 27 |
class AutoFileTests: |
| 28 |
# file tests for which a test file is automatically set up |
| 29 |
|
| 30 |
def setUp(self): |
| 31 |
self.f = self.FileIO(TESTFN, 'w') |
| 32 |
|
| 33 |
def tearDown(self): |
| 34 |
if self.f: |
| 35 |
self.f.close() |
| 36 |
os.remove(TESTFN) |
| 37 |
|
| 38 |
def testWeakRefs(self): |
| 39 |
# verify weak references |
| 40 |
p = proxy(self.f) |
| 41 |
p.write(bytes(range(10))) |
| 42 |
self.assertEqual(self.f.tell(), p.tell()) |
| 43 |
self.f.close() |
| 44 |
self.f = None |
| 45 |
gc_collect() # For PyPy or other GCs. |
| 46 |
self.assertRaises(ReferenceError, getattr, p, 'tell') |
| 47 |
|
| 48 |
def testSeekTell(self): |
| 49 |
self.f.write(bytes(range(20))) |
| 50 |
self.assertEqual(self.f.tell(), 20) |
| 51 |
self.f.seek(0) |
| 52 |
self.assertEqual(self.f.tell(), 0) |
| 53 |
self.f.seek(10) |
| 54 |
self.assertEqual(self.f.tell(), 10) |
| 55 |
self.f.seek(5, 1) |
| 56 |
self.assertEqual(self.f.tell(), 15) |
| 57 |
self.f.seek(-5, 1) |
| 58 |
self.assertEqual(self.f.tell(), 10) |
| 59 |
self.f.seek(-5, 2) |
| 60 |
self.assertEqual(self.f.tell(), 15) |
| 61 |
|
| 62 |
def testAttributes(self): |
| 63 |
# verify expected attributes exist |
| 64 |
f = self.f |
| 65 |
|
| 66 |
self.assertEqual(f.mode, "wb") |
| 67 |
self.assertEqual(f.closed, False) |
| 68 |
|
| 69 |
# verify the attributes are readonly |
| 70 |
for attr in 'mode', 'closed': |
| 71 |
self.assertRaises((AttributeError, TypeError), |
| 72 |
setattr, f, attr, 'oops') |
| 73 |
|
| 74 |
@unittest.skipIf(is_wasi, "WASI does not expose st_blksize.") |
| 75 |
def testBlksize(self): |
| 76 |
# test private _blksize attribute |
| 77 |
blksize = io.DEFAULT_BUFFER_SIZE |
| 78 |
# try to get preferred blksize from stat.st_blksize, if available |
| 79 |
if hasattr(os, 'fstat'): |
| 80 |
fst = os.fstat(self.f.fileno()) |
| 81 |
blksize = getattr(fst, 'st_blksize', blksize) |
| 82 |
self.assertEqual(self.f._blksize, blksize) |
| 83 |
|
| 84 |
# verify readinto |
| 85 |
def testReadintoByteArray(self): |
| 86 |
self.f.write(bytes([1, 2, 0, 255])) |
| 87 |
self.f.close() |
| 88 |
|
| 89 |
ba = bytearray(b'abcdefgh') |
| 90 |
with self.FileIO(TESTFN, 'r') as f: |
| 91 |
n = f.readinto(ba) |
| 92 |
self.assertEqual(ba, b'\x01\x02\x00\xffefgh') |
| 93 |
self.assertEqual(n, 4) |
| 94 |
|
| 95 |
def _testReadintoMemoryview(self): |
| 96 |
self.f.write(bytes([1, 2, 0, 255])) |
| 97 |
self.f.close() |
| 98 |
|
| 99 |
m = memoryview(bytearray(b'abcdefgh')) |
| 100 |
with self.FileIO(TESTFN, 'r') as f: |
| 101 |
n = f.readinto(m) |
| 102 |
self.assertEqual(m, b'\x01\x02\x00\xffefgh') |
| 103 |
self.assertEqual(n, 4) |
| 104 |
|
| 105 |
m = memoryview(bytearray(b'abcdefgh')).cast('H', shape=[2, 2]) |
| 106 |
with self.FileIO(TESTFN, 'r') as f: |
| 107 |
n = f.readinto(m) |
| 108 |
self.assertEqual(bytes(m), b'\x01\x02\x00\xffefgh') |
| 109 |
self.assertEqual(n, 4) |
| 110 |
|
| 111 |
def _testReadintoArray(self): |
| 112 |
self.f.write(bytes([1, 2, 0, 255])) |
| 113 |
self.f.close() |
| 114 |
|
| 115 |
a = array('B', b'abcdefgh') |
| 116 |
with self.FileIO(TESTFN, 'r') as f: |
| 117 |
n = f.readinto(a) |
| 118 |
self.assertEqual(a, array('B', [1, 2, 0, 255, 101, 102, 103, 104])) |
| 119 |
self.assertEqual(n, 4) |
| 120 |
|
| 121 |
a = array('b', b'abcdefgh') |
| 122 |
with self.FileIO(TESTFN, 'r') as f: |
| 123 |
n = f.readinto(a) |
| 124 |
self.assertEqual(a, array('b', [1, 2, 0, -1, 101, 102, 103, 104])) |
| 125 |
self.assertEqual(n, 4) |
| 126 |
|
| 127 |
a = array('I', b'abcdefgh') |
| 128 |
with self.FileIO(TESTFN, 'r') as f: |
| 129 |
n = f.readinto(a) |
| 130 |
self.assertEqual(a, array('I', b'\x01\x02\x00\xffefgh')) |
| 131 |
self.assertEqual(n, 4) |
| 132 |
|
| 133 |
def testWritelinesList(self): |
| 134 |
l = [b'123', b'456'] |
| 135 |
self.f.writelines(l) |
| 136 |
self.f.close() |
| 137 |
self.f = self.FileIO(TESTFN, 'rb') |
| 138 |
buf = self.f.read() |
| 139 |
self.assertEqual(buf, b'123456') |
| 140 |
|
| 141 |
def testWritelinesUserList(self): |
| 142 |
l = UserList([b'123', b'456']) |
| 143 |
self.f.writelines(l) |
| 144 |
self.f.close() |
| 145 |
self.f = self.FileIO(TESTFN, 'rb') |
| 146 |
buf = self.f.read() |
| 147 |
self.assertEqual(buf, b'123456') |
| 148 |
|
| 149 |
def testWritelinesError(self): |
| 150 |
self.assertRaises(TypeError, self.f.writelines, [1, 2, 3]) |
| 151 |
self.assertRaises(TypeError, self.f.writelines, None) |
| 152 |
self.assertRaises(TypeError, self.f.writelines, "abc") |
| 153 |
|
| 154 |
def test_none_args(self): |
| 155 |
self.f.write(b"hi\nbye\nabc") |
| 156 |
self.f.close() |
| 157 |
self.f = self.FileIO(TESTFN, 'r') |
| 158 |
self.assertEqual(self.f.read(None), b"hi\nbye\nabc") |
| 159 |
self.f.seek(0) |
| 160 |
self.assertEqual(self.f.readline(None), b"hi\n") |
| 161 |
self.assertEqual(self.f.readlines(None), [b"bye\n", b"abc"]) |
| 162 |
|
| 163 |
def test_reject(self): |
| 164 |
self.assertRaises(TypeError, self.f.write, "Hello!") |
| 165 |
|
| 166 |
def testRepr(self): |
| 167 |
self.assertEqual(repr(self.f), |
| 168 |
"<%s.FileIO name=%r mode=%r closefd=True>" % |
| 169 |
(self.modulename, self.f.name, self.f.mode)) |
| 170 |
del self.f.name |
| 171 |
self.assertEqual(repr(self.f), |
| 172 |
"<%s.FileIO fd=%r mode=%r closefd=True>" % |
| 173 |
(self.modulename, self.f.fileno(), self.f.mode)) |
| 174 |
self.f.close() |
| 175 |
self.assertEqual(repr(self.f), |
| 176 |
"<%s.FileIO [closed]>" % (self.modulename,)) |
| 177 |
|
| 178 |
def test_subclass_repr(self): |
| 179 |
class TestSubclass(self.FileIO): |
| 180 |
pass |
| 181 |
|
| 182 |
f = TestSubclass(TESTFN) |
| 183 |
with f: |
| 184 |
self.assertIn(TestSubclass.__name__, repr(f)) |
| 185 |
|
| 186 |
self.assertIn(TestSubclass.__name__, repr(f)) |
| 187 |
|
| 188 |
def testReprNoCloseFD(self): |
| 189 |
fd = os.open(TESTFN, os.O_RDONLY) |
| 190 |
try: |
| 191 |
with self.FileIO(fd, 'r', closefd=False) as f: |
| 192 |
self.assertEqual(repr(f), |
| 193 |
"<%s.FileIO name=%r mode=%r closefd=False>" % |
| 194 |
(self.modulename, f.name, f.mode)) |
| 195 |
finally: |
| 196 |
os.close(fd) |
| 197 |
|
| 198 |
@infinite_recursion(25) |
| 199 |
def testRecursiveRepr(self): |
| 200 |
# Issue #25455 |
| 201 |
with swap_attr(self.f, 'name', self.f): |
| 202 |
with self.assertRaises(RuntimeError): |
| 203 |
repr(self.f) # Should not crash |
| 204 |
|
| 205 |
def testErrors(self): |
| 206 |
f = self.f |
| 207 |
self.assertFalse(f.isatty()) |
| 208 |
self.assertFalse(f.closed) |
| 209 |
#self.assertEqual(f.name, TESTFN) |
| 210 |
self.assertRaises(ValueError, f.read, 10) # Open for reading |
| 211 |
f.close() |
| 212 |
self.assertTrue(f.closed) |
| 213 |
f = self.FileIO(TESTFN, 'r') |
| 214 |
self.assertRaises(TypeError, f.readinto, "") |
| 215 |
self.assertFalse(f.closed) |
| 216 |
f.close() |
| 217 |
self.assertTrue(f.closed) |
| 218 |
|
| 219 |
def testMethods(self): |
| 220 |
methods = ['fileno', 'isatty', 'seekable', 'readable', 'writable', |
| 221 |
'read', 'readall', 'readline', 'readlines', |
| 222 |
'tell', 'truncate', 'flush'] |
| 223 |
|
| 224 |
self.f.close() |
| 225 |
self.assertTrue(self.f.closed) |
| 226 |
|
| 227 |
for methodname in methods: |
| 228 |
method = getattr(self.f, methodname) |
| 229 |
# should raise on closed file |
| 230 |
self.assertRaises(ValueError, method) |
| 231 |
|
| 232 |
self.assertRaises(TypeError, self.f.readinto) |
| 233 |
self.assertRaises(ValueError, self.f.readinto, bytearray(1)) |
| 234 |
self.assertRaises(TypeError, self.f.seek) |
| 235 |
self.assertRaises(ValueError, self.f.seek, 0) |
| 236 |
self.assertRaises(TypeError, self.f.write) |
| 237 |
self.assertRaises(ValueError, self.f.write, b'') |
| 238 |
self.assertRaises(TypeError, self.f.writelines) |
| 239 |
self.assertRaises(ValueError, self.f.writelines, b'') |
| 240 |
|
| 241 |
def testOpendir(self): |
| 242 |
# Issue 3703: opening a directory should fill the errno |
| 243 |
# Windows always returns "[Errno 13]: Permission denied |
| 244 |
# Unix uses fstat and returns "[Errno 21]: Is a directory" |
| 245 |
try: |
| 246 |
self.FileIO('.', 'r') |
| 247 |
except OSError as e: |
| 248 |
self.assertNotEqual(e.errno, 0) |
| 249 |
self.assertEqual(e.filename, ".") |
| 250 |
else: |
| 251 |
self.fail("Should have raised OSError") |
| 252 |
|
| 253 |
@unittest.skipIf(os.name == 'nt', "test only works on a POSIX-like system") |
| 254 |
def testOpenDirFD(self): |
| 255 |
fd = os.open('.', os.O_RDONLY) |
| 256 |
with self.assertRaises(OSError) as cm: |
| 257 |
self.FileIO(fd, 'r') |
| 258 |
os.close(fd) |
| 259 |
self.assertEqual(cm.exception.errno, errno.EISDIR) |
| 260 |
|
| 261 |
#A set of functions testing that we get expected behaviour if someone has |
| 262 |
#manually closed the internal file descriptor. First, a decorator: |
| 263 |
def ClosedFD(func): |
| 264 |
@wraps(func) |
| 265 |
def wrapper(self): |
| 266 |
#forcibly close the fd before invoking the problem function |
| 267 |
f = self.f |
| 268 |
os.close(f.fileno()) |
| 269 |
try: |
| 270 |
func(self, f) |
| 271 |
finally: |
| 272 |
try: |
| 273 |
self.f.close() |
| 274 |
except OSError: |
| 275 |
pass |
| 276 |
return wrapper |
| 277 |
|
| 278 |
def ClosedFDRaises(func): |
| 279 |
@wraps(func) |
| 280 |
def wrapper(self): |
| 281 |
#forcibly close the fd before invoking the problem function |
| 282 |
f = self.f |
| 283 |
os.close(f.fileno()) |
| 284 |
try: |
| 285 |
func(self, f) |
| 286 |
except OSError as e: |
| 287 |
self.assertEqual(e.errno, errno.EBADF) |
| 288 |
else: |
| 289 |
self.fail("Should have raised OSError") |
| 290 |
finally: |
| 291 |
try: |
| 292 |
self.f.close() |
| 293 |
except OSError: |
| 294 |
pass |
| 295 |
return wrapper |
| 296 |
|
| 297 |
@ClosedFDRaises |
| 298 |
def testErrnoOnClose(self, f): |
| 299 |
f.close() |
| 300 |
|
| 301 |
@ClosedFDRaises |
| 302 |
def testErrnoOnClosedWrite(self, f): |
| 303 |
f.write(b'a') |
| 304 |
|
| 305 |
@ClosedFDRaises |
| 306 |
def testErrnoOnClosedSeek(self, f): |
| 307 |
f.seek(0) |
| 308 |
|
| 309 |
@ClosedFDRaises |
| 310 |
def testErrnoOnClosedTell(self, f): |
| 311 |
f.tell() |
| 312 |
|
| 313 |
@ClosedFDRaises |
| 314 |
def testErrnoOnClosedTruncate(self, f): |
| 315 |
f.truncate(0) |
| 316 |
|
| 317 |
@ClosedFD |
| 318 |
def testErrnoOnClosedSeekable(self, f): |
| 319 |
f.seekable() |
| 320 |
|
| 321 |
@ClosedFD |
| 322 |
def testErrnoOnClosedReadable(self, f): |
| 323 |
f.readable() |
| 324 |
|
| 325 |
@ClosedFD |
| 326 |
def testErrnoOnClosedWritable(self, f): |
| 327 |
f.writable() |
| 328 |
|
| 329 |
@ClosedFD |
| 330 |
def testErrnoOnClosedFileno(self, f): |
| 331 |
f.fileno() |
| 332 |
|
| 333 |
@ClosedFD |
| 334 |
def testErrnoOnClosedIsatty(self, f): |
| 335 |
self.assertEqual(f.isatty(), False) |
| 336 |
|
| 337 |
def ReopenForRead(self): |
| 338 |
try: |
| 339 |
self.f.close() |
| 340 |
except OSError: |
| 341 |
pass |
| 342 |
self.f = self.FileIO(TESTFN, 'r') |
| 343 |
os.close(self.f.fileno()) |
| 344 |
return self.f |
| 345 |
|
| 346 |
@ClosedFDRaises |
| 347 |
def testErrnoOnClosedRead(self, f): |
| 348 |
f = self.ReopenForRead() |
| 349 |
f.read(1) |
| 350 |
|
| 351 |
@ClosedFDRaises |
| 352 |
def testErrnoOnClosedReadall(self, f): |
| 353 |
f = self.ReopenForRead() |
| 354 |
f.readall() |
| 355 |
|
| 356 |
@ClosedFDRaises |
| 357 |
def testErrnoOnClosedReadinto(self, f): |
| 358 |
f = self.ReopenForRead() |
| 359 |
a = array('b', b'x'*10) |
| 360 |
f.readinto(a) |
| 361 |
|
| 362 |
class CAutoFileTests(AutoFileTests, unittest.TestCase): |
| 363 |
FileIO = _io.FileIO |
| 364 |
modulename = '_io' |
| 365 |
|
| 366 |
class PyAutoFileTests(AutoFileTests, unittest.TestCase): |
| 367 |
FileIO = _pyio.FileIO |
| 368 |
modulename = '_pyio' |
| 369 |
|
| 370 |
|
| 371 |
class OtherFileTests: |
| 372 |
|
| 373 |
def testAbles(self): |
| 374 |
try: |
| 375 |
f = self.FileIO(TESTFN, "w") |
| 376 |
self.assertEqual(f.readable(), False) |
| 377 |
self.assertEqual(f.writable(), True) |
| 378 |
self.assertEqual(f.seekable(), True) |
| 379 |
f.close() |
| 380 |
|
| 381 |
f = self.FileIO(TESTFN, "r") |
| 382 |
self.assertEqual(f.readable(), True) |
| 383 |
self.assertEqual(f.writable(), False) |
| 384 |
self.assertEqual(f.seekable(), True) |
| 385 |
f.close() |
| 386 |
|
| 387 |
f = self.FileIO(TESTFN, "a+") |
| 388 |
self.assertEqual(f.readable(), True) |
| 389 |
self.assertEqual(f.writable(), True) |
| 390 |
self.assertEqual(f.seekable(), True) |
| 391 |
self.assertEqual(f.isatty(), False) |
| 392 |
f.close() |
| 393 |
|
| 394 |
if sys.platform != "win32" and not is_emscripten: |
| 395 |
try: |
| 396 |
f = self.FileIO("/dev/tty", "a") |
| 397 |
except OSError: |
| 398 |
# When run in a cron job there just aren't any |
| 399 |
# ttys, so skip the test. This also handles other |
| 400 |
# OS'es that don't support /dev/tty. |
| 401 |
pass |
| 402 |
else: |
| 403 |
self.assertEqual(f.readable(), False) |
| 404 |
self.assertEqual(f.writable(), True) |
| 405 |
if sys.platform != "darwin" and \ |
| 406 |
'bsd' not in sys.platform and \ |
| 407 |
not sys.platform.startswith(('sunos', 'aix')): |
| 408 |
# Somehow /dev/tty appears seekable on some BSDs |
| 409 |
self.assertEqual(f.seekable(), False) |
| 410 |
self.assertEqual(f.isatty(), True) |
| 411 |
f.close() |
| 412 |
finally: |
| 413 |
os.unlink(TESTFN) |
| 414 |
|
| 415 |
def testInvalidModeStrings(self): |
| 416 |
# check invalid mode strings |
| 417 |
for mode in ("", "aU", "wU+", "rw", "rt"): |
| 418 |
try: |
| 419 |
f = self.FileIO(TESTFN, mode) |
| 420 |
except ValueError: |
| 421 |
pass |
| 422 |
else: |
| 423 |
f.close() |
| 424 |
self.fail('%r is an invalid file mode' % mode) |
| 425 |
|
| 426 |
def testModeStrings(self): |
| 427 |
# test that the mode attribute is correct for various mode strings |
| 428 |
# given as init args |
| 429 |
try: |
| 430 |
for modes in [('w', 'wb'), ('wb', 'wb'), ('wb+', 'rb+'), |
| 431 |
('w+b', 'rb+'), ('a', 'ab'), ('ab', 'ab'), |
| 432 |
('ab+', 'ab+'), ('a+b', 'ab+'), ('r', 'rb'), |
| 433 |
('rb', 'rb'), ('rb+', 'rb+'), ('r+b', 'rb+')]: |
| 434 |
# read modes are last so that TESTFN will exist first |
| 435 |
with self.FileIO(TESTFN, modes[0]) as f: |
| 436 |
self.assertEqual(f.mode, modes[1]) |
| 437 |
finally: |
| 438 |
if os.path.exists(TESTFN): |
| 439 |
os.unlink(TESTFN) |
| 440 |
|
| 441 |
def testUnicodeOpen(self): |
| 442 |
# verify repr works for unicode too |
| 443 |
f = self.FileIO(str(TESTFN), "w") |
| 444 |
f.close() |
| 445 |
os.unlink(TESTFN) |
| 446 |
|
| 447 |
def testBytesOpen(self): |
| 448 |
# Opening a bytes filename |
| 449 |
fn = TESTFN_ASCII.encode("ascii") |
| 450 |
f = self.FileIO(fn, "w") |
| 451 |
try: |
| 452 |
f.write(b"abc") |
| 453 |
f.close() |
| 454 |
with self.open(TESTFN_ASCII, "rb") as f: |
| 455 |
self.assertEqual(f.read(), b"abc") |
| 456 |
finally: |
| 457 |
os.unlink(TESTFN_ASCII) |
| 458 |
|
| 459 |
@unittest.skipIf(sys.getfilesystemencoding() != 'utf-8', |
| 460 |
"test only works for utf-8 filesystems") |
| 461 |
def testUtf8BytesOpen(self): |
| 462 |
# Opening a UTF-8 bytes filename |
| 463 |
try: |
| 464 |
fn = TESTFN_UNICODE.encode("utf-8") |
| 465 |
except UnicodeEncodeError: |
| 466 |
self.skipTest('could not encode %r to utf-8' % TESTFN_UNICODE) |
| 467 |
f = self.FileIO(fn, "w") |
| 468 |
try: |
| 469 |
f.write(b"abc") |
| 470 |
f.close() |
| 471 |
with self.open(TESTFN_UNICODE, "rb") as f: |
| 472 |
self.assertEqual(f.read(), b"abc") |
| 473 |
finally: |
| 474 |
os.unlink(TESTFN_UNICODE) |
| 475 |
|
| 476 |
def testConstructorHandlesNULChars(self): |
| 477 |
fn_with_NUL = 'foo\0bar' |
| 478 |
self.assertRaises(ValueError, self.FileIO, fn_with_NUL, 'w') |
| 479 |
self.assertRaises(ValueError, self.FileIO, bytes(fn_with_NUL, 'ascii'), 'w') |
| 480 |
|
| 481 |
def testInvalidFd(self): |
| 482 |
self.assertRaises(ValueError, self.FileIO, -10) |
| 483 |
self.assertRaises(OSError, self.FileIO, make_bad_fd()) |
| 484 |
if sys.platform == 'win32': |
| 485 |
import msvcrt |
| 486 |
self.assertRaises(OSError, msvcrt.get_osfhandle, make_bad_fd()) |
| 487 |
|
| 488 |
def testBooleanFd(self): |
| 489 |
for fd in False, True: |
| 490 |
with self.assertWarnsRegex(RuntimeWarning, |
| 491 |
'bool is used as a file descriptor') as cm: |
| 492 |
f = self.FileIO(fd, closefd=False) |
| 493 |
f.close() |
| 494 |
self.assertEqual(cm.filename, __file__) |
| 495 |
|
| 496 |
def testBadModeArgument(self): |
| 497 |
# verify that we get a sensible error message for bad mode argument |
| 498 |
bad_mode = "qwerty" |
| 499 |
try: |
| 500 |
f = self.FileIO(TESTFN, bad_mode) |
| 501 |
except ValueError as msg: |
| 502 |
if msg.args[0] != 0: |
| 503 |
s = str(msg) |
| 504 |
if TESTFN in s or bad_mode not in s: |
| 505 |
self.fail("bad error message for invalid mode: %s" % s) |
| 506 |
# if msg.args[0] == 0, we're probably on Windows where there may be |
| 507 |
# no obvious way to discover why open() failed. |
| 508 |
else: |
| 509 |
f.close() |
| 510 |
self.fail("no error for invalid mode: %s" % bad_mode) |
| 511 |
|
| 512 |
def testTruncate(self): |
| 513 |
f = self.FileIO(TESTFN, 'w') |
| 514 |
f.write(bytes(bytearray(range(10)))) |
| 515 |
self.assertEqual(f.tell(), 10) |
| 516 |
f.truncate(5) |
| 517 |
self.assertEqual(f.tell(), 10) |
| 518 |
self.assertEqual(f.seek(0, io.SEEK_END), 5) |
| 519 |
f.truncate(15) |
| 520 |
self.assertEqual(f.tell(), 5) |
| 521 |
self.assertEqual(f.seek(0, io.SEEK_END), 15) |
| 522 |
f.close() |
| 523 |
|
| 524 |
def testTruncateOnWindows(self): |
| 525 |
def bug801631(): |
| 526 |
# SF bug <https://bugs.python.org/issue801631> |
| 527 |
# "file.truncate fault on windows" |
| 528 |
f = self.FileIO(TESTFN, 'w') |
| 529 |
f.write(bytes(range(11))) |
| 530 |
f.close() |
| 531 |
|
| 532 |
f = self.FileIO(TESTFN,'r+') |
| 533 |
data = f.read(5) |
| 534 |
if data != bytes(range(5)): |
| 535 |
self.fail("Read on file opened for update failed %r" % data) |
| 536 |
if f.tell() != 5: |
| 537 |
self.fail("File pos after read wrong %d" % f.tell()) |
| 538 |
|
| 539 |
f.truncate() |
| 540 |
if f.tell() != 5: |
| 541 |
self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 542 |
|
| 543 |
f.close() |
| 544 |
size = os.path.getsize(TESTFN) |
| 545 |
if size != 5: |
| 546 |
self.fail("File size after ftruncate wrong %d" % size) |
| 547 |
|
| 548 |
try: |
| 549 |
bug801631() |
| 550 |
finally: |
| 551 |
os.unlink(TESTFN) |
| 552 |
|
| 553 |
def testAppend(self): |
| 554 |
try: |
| 555 |
f = self.FileIO(TESTFN, 'wb') |
| 556 |
f.write(b'spam') |
| 557 |
f.close() |
| 558 |
f = self.FileIO(TESTFN, 'ab') |
| 559 |
f.write(b'eggs') |
| 560 |
f.close() |
| 561 |
f = self.FileIO(TESTFN, 'rb') |
| 562 |
d = f.read() |
| 563 |
f.close() |
| 564 |
self.assertEqual(d, b'spameggs') |
| 565 |
finally: |
| 566 |
try: |
| 567 |
os.unlink(TESTFN) |
| 568 |
except: |
| 569 |
pass |
| 570 |
|
| 571 |
def testInvalidInit(self): |
| 572 |
self.assertRaises(TypeError, self.FileIO, "1", 0, 0) |
| 573 |
|
| 574 |
def testWarnings(self): |
| 575 |
with check_warnings(quiet=True) as w: |
| 576 |
self.assertEqual(w.warnings, []) |
| 577 |
self.assertRaises(TypeError, self.FileIO, []) |
| 578 |
self.assertEqual(w.warnings, []) |
| 579 |
self.assertRaises(ValueError, self.FileIO, "/some/invalid/name", "rt") |
| 580 |
self.assertEqual(w.warnings, []) |
| 581 |
|
| 582 |
def testUnclosedFDOnException(self): |
| 583 |
class MyException(Exception): pass |
| 584 |
class MyFileIO(self.FileIO): |
| 585 |
def __setattr__(self, name, value): |
| 586 |
if name == "name": |
| 587 |
raise MyException("blocked setting name") |
| 588 |
return super(MyFileIO, self).__setattr__(name, value) |
| 589 |
fd = os.open(__file__, os.O_RDONLY) |
| 590 |
self.assertRaises(MyException, MyFileIO, fd) |
| 591 |
os.close(fd) # should not raise OSError(EBADF) |
| 592 |
|
| 593 |
|
| 594 |
class COtherFileTests(OtherFileTests, unittest.TestCase): |
| 595 |
FileIO = _io.FileIO |
| 596 |
modulename = '_io' |
| 597 |
open = _io.open |
| 598 |
|
| 599 |
@cpython_only |
| 600 |
def testInvalidFd_overflow(self): |
| 601 |
# Issue 15989 |
| 602 |
_testcapi = import_module("_testcapi") |
| 603 |
self.assertRaises(TypeError, self.FileIO, _testcapi.INT_MAX + 1) |
| 604 |
self.assertRaises(TypeError, self.FileIO, _testcapi.INT_MIN - 1) |
| 605 |
|
| 606 |
def test_open_code(self): |
| 607 |
# Check that the default behaviour of open_code matches |
| 608 |
# open("rb") |
| 609 |
with self.FileIO(__file__, "rb") as f: |
| 610 |
expected = f.read() |
| 611 |
with _io.open_code(__file__) as f: |
| 612 |
actual = f.read() |
| 613 |
self.assertEqual(expected, actual) |
| 614 |
|
| 615 |
|
| 616 |
class PyOtherFileTests(OtherFileTests, unittest.TestCase): |
| 617 |
FileIO = _pyio.FileIO |
| 618 |
modulename = '_pyio' |
| 619 |
open = _pyio.open |
| 620 |
|
| 621 |
def test_open_code(self): |
| 622 |
# Check that the default behaviour of open_code matches |
| 623 |
# open("rb") |
| 624 |
with self.FileIO(__file__, "rb") as f: |
| 625 |
expected = f.read() |
| 626 |
with check_warnings(quiet=True) as w: |
| 627 |
# Always test _open_code_with_warning |
| 628 |
with _pyio._open_code_with_warning(__file__) as f: |
| 629 |
actual = f.read() |
| 630 |
self.assertEqual(expected, actual) |
| 631 |
self.assertNotEqual(w.warnings, []) |
| 632 |
|
| 633 |
|
| 634 |
def tearDownModule(): |
| 635 |
# Historically, these tests have been sloppy about removing TESTFN. |
| 636 |
# So get rid of it no matter what. |
| 637 |
if os.path.exists(TESTFN): |
| 638 |
os.unlink(TESTFN) |
| 639 |
|
| 640 |
|
| 641 |
if __name__ == '__main__': |
| 642 |
unittest.main() |