Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
test.test_io.IOTest Class Reference
Inheritance diagram for test.test_io.IOTest:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def setUp
def tearDown
def write_ops
def read_ops
def large_file_ops
def test_invalid_operations
def test_raw_file_io
def test_buffered_file_io
def test_readline
def test_raw_bytes_io
def test_large_file_ops
def test_with_open
def test_append_mode_tell
def test_destructor
def test_IOBase_destructor
def test_RawIOBase_destructor
def test_BufferedIOBase_destructor
def test_TextIOBase_destructor
def test_close_flushes
def test_array_writes
def test_closefd
def test_read_closed
def test_no_closefd_with_filename
def test_closefd_attr
def test_garbage_collection
def test_unbounded_file
def test_flush_error_on_close
def test_multi_close
def test_RawIOBase_read

Public Attributes

 on_del
 on_close
 on_flush

Static Public Attributes

int LARGE = 2

Private Member Functions

def _check_base_destructor

Detailed Description

Definition at line 258 of file test_io.py.


Member Function Documentation

def test.test_io.IOTest._check_base_destructor (   self,
  base 
) [private]

Definition at line 474 of file test_io.py.

00474 
00475     def _check_base_destructor(self, base):
00476         record = []
00477         class MyIO(base):
00478             def __init__(self):
00479                 # This exercises the availability of attributes on object
00480                 # destruction.
00481                 # (in the C version, close() is called by the tp_dealloc
00482                 # function, not by __del__)
00483                 self.on_del = 1
00484                 self.on_close = 2
00485                 self.on_flush = 3
00486             def __del__(self):
00487                 record.append(self.on_del)
00488                 try:
00489                     f = super().__del__
00490                 except AttributeError:
00491                     pass
00492                 else:
00493                     f()
00494             def close(self):
00495                 record.append(self.on_close)
00496                 super().close()
00497             def flush(self):
00498                 record.append(self.on_flush)
00499                 super().flush()
00500         f = MyIO()
00501         del f
00502         support.gc_collect()
00503         self.assertEqual(record, [1, 2, 3])

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_io.IOTest.large_file_ops (   self,
  f 
)

Definition at line 318 of file test_io.py.

00318 
00319     def large_file_ops(self, f):
00320         assert f.readable()
00321         assert f.writable()
00322         self.assertEqual(f.seek(self.LARGE), self.LARGE)
00323         self.assertEqual(f.tell(), self.LARGE)
00324         self.assertEqual(f.write(b"xxx"), 3)
00325         self.assertEqual(f.tell(), self.LARGE + 3)
00326         self.assertEqual(f.seek(-1, 1), self.LARGE + 2)
00327         self.assertEqual(f.truncate(), self.LARGE + 2)
00328         self.assertEqual(f.tell(), self.LARGE + 2)
00329         self.assertEqual(f.seek(0, 2), self.LARGE + 2)
00330         self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
00331         self.assertEqual(f.tell(), self.LARGE + 2)
00332         self.assertEqual(f.seek(0, 2), self.LARGE + 1)
00333         self.assertEqual(f.seek(-1, 2), self.LARGE)
00334         self.assertEqual(f.read(2), b"x")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_io.IOTest.read_ops (   self,
  f,
  buffered = False 
)

Definition at line 288 of file test_io.py.

00288 
00289     def read_ops(self, f, buffered=False):
00290         data = f.read(5)
00291         self.assertEqual(data, b"hello")
00292         data = bytearray(data)
00293         self.assertEqual(f.readinto(data), 5)
00294         self.assertEqual(data, b" worl")
00295         self.assertEqual(f.readinto(data), 2)
00296         self.assertEqual(len(data), 5)
00297         self.assertEqual(data[:2], b"d\n")
00298         self.assertEqual(f.seek(0), 0)
00299         self.assertEqual(f.read(20), b"hello world\n")
00300         self.assertEqual(f.read(1), b"")
00301         self.assertEqual(f.readinto(bytearray(b"x")), 0)
00302         self.assertEqual(f.seek(-6, 2), 6)
00303         self.assertEqual(f.read(5), b"world")
00304         self.assertEqual(f.read(0), b"")
00305         self.assertEqual(f.readinto(bytearray()), 0)
00306         self.assertEqual(f.seek(-6, 1), 5)
00307         self.assertEqual(f.read(5), b" worl")
00308         self.assertEqual(f.tell(), 10)
00309         self.assertRaises(TypeError, f.seek, 0.0)
00310         if buffered:
00311             f.seek(0)
00312             self.assertEqual(f.read(), b"hello world\n")
00313             f.seek(6)
00314             self.assertEqual(f.read(), b"world\n")
00315             self.assertEqual(f.read(), b"")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_io.IOTest.setUp (   self)

Definition at line 260 of file test_io.py.

00260 
00261     def setUp(self):
00262         support.unlink(support.TESTFN)

Here is the caller graph for this function:

Definition at line 263 of file test_io.py.

00263 
00264     def tearDown(self):
00265         support.unlink(support.TESTFN)

Here is the caller graph for this function:

Definition at line 438 of file test_io.py.

00438 
00439     def test_append_mode_tell(self):
00440         with self.open(support.TESTFN, "wb") as f:
00441             f.write(b"xxx")
00442         with self.open(support.TESTFN, "ab", buffering=0) as f:
00443             self.assertEqual(f.tell(), 3)
00444         with self.open(support.TESTFN, "ab") as f:
00445             self.assertEqual(f.tell(), 3)
00446         with self.open(support.TESTFN, "a") as f:
00447             self.assertTrue(f.tell() > 0)

Here is the call graph for this function:

Definition at line 522 of file test_io.py.

00522 
00523     def test_array_writes(self):
00524         a = array.array('i', range(10))
00525         n = len(a.tobytes())
00526         with self.open(support.TESTFN, "wb", 0) as f:
00527             self.assertEqual(f.write(a), n)
00528         with self.open(support.TESTFN, "wb") as f:
00529             self.assertEqual(f.write(a), n)

Here is the call graph for this function:

Definition at line 370 of file test_io.py.

00370 
00371     def test_buffered_file_io(self):
00372         with self.open(support.TESTFN, "wb") as f:
00373             self.assertEqual(f.readable(), False)
00374             self.assertEqual(f.writable(), True)
00375             self.assertEqual(f.seekable(), True)
00376             self.write_ops(f)
00377         with self.open(support.TESTFN, "rb") as f:
00378             self.assertEqual(f.readable(), True)
00379             self.assertEqual(f.writable(), False)
00380             self.assertEqual(f.seekable(), True)
00381             self.read_ops(f, True)

Here is the call graph for this function:

Definition at line 510 of file test_io.py.

00510 
00511     def test_BufferedIOBase_destructor(self):
00512         self._check_base_destructor(self.BufferedIOBase)

Here is the call graph for this function:

Definition at line 516 of file test_io.py.

00516 
00517     def test_close_flushes(self):
00518         with self.open(support.TESTFN, "wb") as f:
00519             f.write(b"xxx")
00520         with self.open(support.TESTFN, "rb") as f:
00521             self.assertEqual(f.read(), b"xxx")

Here is the call graph for this function:

Definition at line 530 of file test_io.py.

00530 
00531     def test_closefd(self):
00532         self.assertRaises(ValueError, self.open, support.TESTFN, 'w',
00533                           closefd=False)

Here is the call graph for this function:

Definition at line 548 of file test_io.py.

00548 
00549     def test_closefd_attr(self):
00550         with self.open(support.TESTFN, "wb") as f:
00551             f.write(b"egg\n")
00552         with self.open(support.TESTFN, "r") as f:
00553             self.assertEqual(f.buffer.raw.closefd, True)
00554             file = self.open(f.fileno(), "r", closefd=False)
00555             self.assertEqual(file.buffer.raw.closefd, False)

Here is the call graph for this function:

Definition at line 448 of file test_io.py.

00448 
00449     def test_destructor(self):
00450         record = []
00451         class MyFileIO(self.FileIO):
00452             def __del__(self):
00453                 record.append(1)
00454                 try:
00455                     f = super().__del__
00456                 except AttributeError:
00457                     pass
00458                 else:
00459                     f()
00460             def close(self):
00461                 record.append(2)
00462                 super().close()
00463             def flush(self):
00464                 record.append(3)
00465                 super().flush()
00466         with support.check_warnings(('', ResourceWarning)):
00467             f = MyFileIO(support.TESTFN, "wb")
00468             f.write(b"xxx")
00469             del f
00470             support.gc_collect()
00471             self.assertEqual(record, [1, 2, 3])
00472             with self.open(support.TESTFN, "rb") as f:
00473                 self.assertEqual(f.read(), b"xxx")

Here is the call graph for this function:

Definition at line 586 of file test_io.py.

00586 
00587     def test_flush_error_on_close(self):
00588         f = self.open(support.TESTFN, "wb", buffering=0)
00589         def bad_flush():
00590             raise IOError()
00591         f.flush = bad_flush
00592         self.assertRaises(IOError, f.close) # exception not swallowed

Here is the call graph for this function:

Definition at line 556 of file test_io.py.

00556 
00557     def test_garbage_collection(self):
00558         # FileIO objects are collected, and collecting them flushes
00559         # all data to disk.
00560         with support.check_warnings(('', ResourceWarning)):
00561             f = self.FileIO(support.TESTFN, "wb")
00562             f.write(b"abcxxx")
00563             f.f = f
00564             wr = weakref.ref(f)
00565             del f
00566             support.gc_collect()
00567         self.assertTrue(wr() is None, wr)
00568         with self.open(support.TESTFN, "rb") as f:
00569             self.assertEqual(f.read(), b"abcxxx")

Here is the call graph for this function:

Definition at line 335 of file test_io.py.

00335 
00336     def test_invalid_operations(self):
00337         # Try writing on a file opened in read mode and vice-versa.
00338         exc = self.UnsupportedOperation
00339         for mode in ("w", "wb"):
00340             with self.open(support.TESTFN, mode) as fp:
00341                 self.assertRaises(exc, fp.read)
00342                 self.assertRaises(exc, fp.readline)
00343         with self.open(support.TESTFN, "wb", buffering=0) as fp:
00344             self.assertRaises(exc, fp.read)
00345             self.assertRaises(exc, fp.readline)
00346         with self.open(support.TESTFN, "rb", buffering=0) as fp:
00347             self.assertRaises(exc, fp.write, b"blah")
00348             self.assertRaises(exc, fp.writelines, [b"blah\n"])
00349         with self.open(support.TESTFN, "rb") as fp:
00350             self.assertRaises(exc, fp.write, b"blah")
00351             self.assertRaises(exc, fp.writelines, [b"blah\n"])
00352         with self.open(support.TESTFN, "r") as fp:
00353             self.assertRaises(exc, fp.write, "blah")
00354             self.assertRaises(exc, fp.writelines, ["blah\n"])
00355             # Non-zero seeking from current or end pos
00356             self.assertRaises(exc, fp.seek, 1, self.SEEK_CUR)
00357             self.assertRaises(exc, fp.seek, -1, self.SEEK_END)

Here is the call graph for this function:

Definition at line 504 of file test_io.py.

00504 
00505     def test_IOBase_destructor(self):
00506         self._check_base_destructor(self.IOBase)

Here is the call graph for this function:

Definition at line 404 of file test_io.py.

00404 
00405     def test_large_file_ops(self):
00406         # On Windows and Mac OSX this test comsumes large resources; It takes
00407         # a long time to build the >2GB file and takes >2GB of disk space
00408         # therefore the resource must be enabled to run this test.
00409         if sys.platform[:3] == 'win' or sys.platform == 'darwin':
00410             if not support.is_resource_enabled("largefile"):
00411                 print("\nTesting large file ops skipped on %s." % sys.platform,
00412                       file=sys.stderr)
00413                 print("It requires %d bytes and a long time." % self.LARGE,
00414                       file=sys.stderr)
00415                 print("Use 'regrtest.py -u largefile test_io' to run it.",
00416                       file=sys.stderr)
00417                 return
00418         with self.open(support.TESTFN, "w+b", 0) as f:
00419             self.large_file_ops(f)
00420         with self.open(support.TESTFN, "w+b") as f:
00421             self.large_file_ops(f)

Here is the call graph for this function:

Definition at line 593 of file test_io.py.

00593 
00594     def test_multi_close(self):
00595         f = self.open(support.TESTFN, "wb", buffering=0)
00596         f.close()
00597         f.close()
00598         f.close()
00599         self.assertRaises(ValueError, f.flush)

Here is the call graph for this function:

Definition at line 544 of file test_io.py.

00544 
00545     def test_no_closefd_with_filename(self):
00546         # can't use closefd in combination with a file name
00547         self.assertRaises(ValueError, self.open, support.TESTFN, "r", closefd=False)

Here is the call graph for this function:

Definition at line 396 of file test_io.py.

00396 
00397     def test_raw_bytes_io(self):
00398         f = self.BytesIO()
00399         self.write_ops(f)
00400         data = f.getvalue()
00401         self.assertEqual(data, b"hello world\n")
00402         f = self.BytesIO(data)
00403         self.read_ops(f, True)

Here is the call graph for this function:

Definition at line 358 of file test_io.py.

00358 
00359     def test_raw_file_io(self):
00360         with self.open(support.TESTFN, "wb", buffering=0) as f:
00361             self.assertEqual(f.readable(), False)
00362             self.assertEqual(f.writable(), True)
00363             self.assertEqual(f.seekable(), True)
00364             self.write_ops(f)
00365         with self.open(support.TESTFN, "rb", buffering=0) as f:
00366             self.assertEqual(f.readable(), True)
00367             self.assertEqual(f.writable(), False)
00368             self.assertEqual(f.seekable(), True)
00369             self.read_ops(f)

Here is the call graph for this function:

Definition at line 507 of file test_io.py.

00507 
00508     def test_RawIOBase_destructor(self):
00509         self._check_base_destructor(self.RawIOBase)

Here is the call graph for this function:

Definition at line 600 of file test_io.py.

00600 
00601     def test_RawIOBase_read(self):
00602         # Exercise the default RawIOBase.read() implementation (which calls
00603         # readinto() internally).
00604         rawio = self.MockRawIOWithoutRead((b"abc", b"d", None, b"efg", None))
00605         self.assertEqual(rawio.read(2), b"ab")
00606         self.assertEqual(rawio.read(2), b"c")
00607         self.assertEqual(rawio.read(2), b"d")
00608         self.assertEqual(rawio.read(2), None)
00609         self.assertEqual(rawio.read(2), b"ef")
00610         self.assertEqual(rawio.read(2), b"g")
00611         self.assertEqual(rawio.read(2), None)
00612         self.assertEqual(rawio.read(2), b"")

Here is the call graph for this function:

Definition at line 534 of file test_io.py.

00534 
00535     def test_read_closed(self):
00536         with self.open(support.TESTFN, "w") as f:
00537             f.write("egg\n")
00538         with self.open(support.TESTFN, "r") as f:
00539             file = self.open(f.fileno(), "r", closefd=False)
00540             self.assertEqual(file.read(), "egg\n")
00541             file.seek(0)
00542             file.close()
00543             self.assertRaises(ValueError, file.read)

Here is the call graph for this function:

Definition at line 382 of file test_io.py.

00382 
00383     def test_readline(self):
00384         with self.open(support.TESTFN, "wb") as f:
00385             f.write(b"abc\ndef\nxyzzy\nfoo\x00bar\nanother line")
00386         with self.open(support.TESTFN, "rb") as f:
00387             self.assertEqual(f.readline(), b"abc\n")
00388             self.assertEqual(f.readline(10), b"def\n")
00389             self.assertEqual(f.readline(2), b"xy")
00390             self.assertEqual(f.readline(4), b"zzy\n")
00391             self.assertEqual(f.readline(), b"foo\x00bar\n")
00392             self.assertEqual(f.readline(None), b"another line")
00393             self.assertRaises(TypeError, f.readline, 5.3)
00394         with self.open(support.TESTFN, "r") as f:
00395             self.assertRaises(TypeError, f.readline, 5.3)

Here is the call graph for this function:

Definition at line 513 of file test_io.py.

00513 
00514     def test_TextIOBase_destructor(self):
00515         self._check_base_destructor(self.TextIOBase)

Here is the call graph for this function:

Definition at line 570 of file test_io.py.

00570 
00571     def test_unbounded_file(self):
00572         # Issue #1174606: reading from an unbounded stream such as /dev/zero.
00573         zero = "/dev/zero"
00574         if not os.path.exists(zero):
00575             self.skipTest("{0} does not exist".format(zero))
00576         if sys.maxsize > 0x7FFFFFFF:
00577             self.skipTest("test can only run in a 32-bit address space")
00578         if support.real_max_memuse < support._2G:
00579             self.skipTest("test requires at least 2GB of memory")
00580         with self.open(zero, "rb", buffering=0) as f:
00581             self.assertRaises(OverflowError, f.read)
00582         with self.open(zero, "rb") as f:
00583             self.assertRaises(OverflowError, f.read)
00584         with self.open(zero, "r") as f:
00585             self.assertRaises(OverflowError, f.read)

Here is the call graph for this function:

Definition at line 422 of file test_io.py.

00422 
00423     def test_with_open(self):
00424         for bufsize in (0, 1, 100):
00425             f = None
00426             with self.open(support.TESTFN, "wb", bufsize) as f:
00427                 f.write(b"xxx")
00428             self.assertEqual(f.closed, True)
00429             f = None
00430             try:
00431                 with self.open(support.TESTFN, "wb", bufsize) as f:
00432                     1/0
00433             except ZeroDivisionError:
00434                 self.assertEqual(f.closed, True)
00435             else:
00436                 self.fail("1/0 didn't raise an exception")

Here is the call graph for this function:

def test.test_io.IOTest.write_ops (   self,
  f 
)

Definition at line 266 of file test_io.py.

00266 
00267     def write_ops(self, f):
00268         self.assertEqual(f.write(b"blah."), 5)
00269         f.truncate(0)
00270         self.assertEqual(f.tell(), 5)
00271         f.seek(0)
00272 
00273         self.assertEqual(f.write(b"blah."), 5)
00274         self.assertEqual(f.seek(0), 0)
00275         self.assertEqual(f.write(b"Hello."), 6)
00276         self.assertEqual(f.tell(), 6)
00277         self.assertEqual(f.seek(-1, 1), 5)
00278         self.assertEqual(f.tell(), 5)
00279         self.assertEqual(f.write(bytearray(b" world\n\n\n")), 9)
00280         self.assertEqual(f.seek(0), 0)
00281         self.assertEqual(f.write(b"h"), 1)
00282         self.assertEqual(f.seek(-1, 2), 13)
00283         self.assertEqual(f.tell(), 13)
00284 
00285         self.assertEqual(f.truncate(12), 12)
00286         self.assertEqual(f.tell(), 13)
00287         self.assertRaises(TypeError, f.seek, 0.0)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 316 of file test_io.py.

Definition at line 483 of file test_io.py.

Definition at line 482 of file test_io.py.

Definition at line 484 of file test_io.py.


The documentation for this class was generated from the following file: