Back to index

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

List of all members.

Public Member Functions

def setUp
def tearDown
def test_constructor
def test_detach
def test_repr
def test_line_buffering
def test_encoding
def test_encoding_errors_reading
def test_encoding_errors_writing
def test_newlines
def test_newlines_input
def test_newlines_output
def test_destructor
def test_override_destructor
def test_error_through_destructor
def test_basic_io
def multi_line_test
def test_telling
def test_seeking
def test_seeking_too
def test_seek_and_tell
def test_encoded_writes
def test_unreadable
def test_read_one_by_one
def test_readlines
def test_read_by_chunk
def test_issue1395_1
def test_issue1395_2
def test_issue1395_3
def test_issue1395_4
def test_issue1395_5
def test_issue2282
def test_append_bom
def test_seek_bom
def test_errors_property
def test_threads_write
def test_flush_error_on_close
def test_multi_close
def test_unseekable
def test_readonly_attributes
def test_rawio
def test_rawio_write_through

Public Attributes

 testdata
 normalized

Detailed Description

Definition at line 2358 of file test_io.py.


Member Function Documentation

def test.test_io.TextIOWrapperTest.multi_line_test (   self,
  f,
  enc 
) [inherited]

Definition at line 1994 of file test_io.py.

01994 
01995     def multi_line_test(self, f, enc):
01996         f.seek(0)
01997         f.truncate()
01998         sample = "s\xff\u0fff\uffff"
01999         wlines = []
02000         for size in (0, 1, 2, 3, 4, 5, 30, 31, 32, 33, 62, 63, 64, 65, 1000):
02001             chars = []
02002             for i in range(size):
02003                 chars.append(sample[i % len(sample)])
02004             line = "".join(chars) + "\n"
02005             wlines.append((f.tell(), line))
02006             f.write(line)
02007         f.seek(0)
02008         rlines = []
02009         while True:
02010             pos = f.tell()
02011             line = f.readline()
02012             if not line:
02013                 break
02014             rlines.append((pos, line))
02015         self.assertEqual(rlines, wlines)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_io.TextIOWrapperTest.setUp (   self) [inherited]

Definition at line 1721 of file test_io.py.

01721 
01722     def setUp(self):
01723         self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n"
01724         self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii")
01725         support.unlink(support.TESTFN)

Here is the caller graph for this function:

def test.test_io.TextIOWrapperTest.tearDown (   self) [inherited]

Definition at line 1726 of file test_io.py.

01726 
01727     def tearDown(self):
01728         support.unlink(support.TESTFN)

Here is the caller graph for this function:

Definition at line 2227 of file test_io.py.

02227 
02228     def test_append_bom(self):
02229         # The BOM is not written again when appending to a non-empty file
02230         filename = support.TESTFN
02231         for charset in ('utf-8-sig', 'utf-16', 'utf-32'):
02232             with self.open(filename, 'w', encoding=charset) as f:
02233                 f.write('aaa')
02234                 pos = f.tell()
02235             with self.open(filename, 'rb') as f:
02236                 self.assertEqual(f.read(), 'aaa'.encode(charset))
02237 
02238             with self.open(filename, 'a', encoding=charset) as f:
02239                 f.write('xxx')
02240             with self.open(filename, 'rb') as f:
02241                 self.assertEqual(f.read(), 'aaaxxx'.encode(charset))

Here is the call graph for this function:

Definition at line 1965 of file test_io.py.

01965 
01966     def test_basic_io(self):
01967         for chunksize in (1, 2, 3, 4, 5, 15, 16, 17, 31, 32, 33, 63, 64, 65):
01968             for enc in "ascii", "latin1", "utf8" :# , "utf-16-be", "utf-16-le":
01969                 f = self.open(support.TESTFN, "w+", encoding=enc)
01970                 f._CHUNK_SIZE = chunksize
01971                 self.assertEqual(f.write("abc"), 3)
01972                 f.close()
01973                 f = self.open(support.TESTFN, "r+", encoding=enc)
01974                 f._CHUNK_SIZE = chunksize
01975                 self.assertEqual(f.tell(), 0)
01976                 self.assertEqual(f.read(), "abc")
01977                 cookie = f.tell()
01978                 self.assertEqual(f.seek(0), 0)
01979                 self.assertEqual(f.read(None), "abc")
01980                 f.seek(0)
01981                 self.assertEqual(f.read(2), "ab")
01982                 self.assertEqual(f.read(1), "c")
01983                 self.assertEqual(f.read(1), "")
01984                 self.assertEqual(f.read(), "")
01985                 self.assertEqual(f.tell(), cookie)
01986                 self.assertEqual(f.seek(0), 0)
01987                 self.assertEqual(f.seek(0, 2), cookie)
01988                 self.assertEqual(f.write("def"), 3)
01989                 self.assertEqual(f.seek(cookie), cookie)
01990                 self.assertEqual(f.read(), "def")
01991                 if enc.startswith("utf"):
01992                     self.multi_line_test(f, enc)
01993                 f.close()

Here is the call graph for this function:

Definition at line 1729 of file test_io.py.

01729 
01730     def test_constructor(self):
01731         r = self.BytesIO(b"\xc3\xa9\n\n")
01732         b = self.BufferedReader(r, 1000)
01733         t = self.TextIOWrapper(b)
01734         t.__init__(b, encoding="latin1", newline="\r\n")
01735         self.assertEqual(t.encoding, "latin1")
01736         self.assertEqual(t.line_buffering, False)
01737         t.__init__(b, encoding="utf8", line_buffering=True)
01738         self.assertEqual(t.encoding, "utf8")
01739         self.assertEqual(t.line_buffering, True)
01740         self.assertEqual("\xe9\n", t.readline())
01741         self.assertRaises(TypeError, t.__init__, b, newline=42)
01742         self.assertRaises(ValueError, t.__init__, b, newline='xyzzy')

Here is the call graph for this function:

Definition at line 1911 of file test_io.py.

01911 
01912     def test_destructor(self):
01913         l = []
01914         base = self.BytesIO
01915         class MyBytesIO(base):
01916             def close(self):
01917                 l.append(self.getvalue())
01918                 base.close(self)
01919         b = MyBytesIO()
01920         t = self.TextIOWrapper(b, encoding="ascii")
01921         t.write("abc")
01922         del t
01923         support.gc_collect()
01924         self.assertEqual([b"abc"], l)

Here is the call graph for this function:

def test.test_io.TextIOWrapperTest.test_detach (   self) [inherited]

Definition at line 1743 of file test_io.py.

01743 
01744     def test_detach(self):
01745         r = self.BytesIO()
01746         b = self.BufferedWriter(r)
01747         t = self.TextIOWrapper(b)
01748         self.assertIs(t.detach(), b)
01749 
01750         t = self.TextIOWrapper(b, encoding="ascii")
01751         t.write("howdy")
01752         self.assertFalse(r.getvalue())
01753         t.detach()
01754         self.assertEqual(r.getvalue(), b"howdy")
01755         self.assertRaises(ValueError, t.detach)

Here is the call graph for this function:

Definition at line 2111 of file test_io.py.

02111 
02112     def test_encoded_writes(self):
02113         data = "1234567890"
02114         tests = ("utf-16",
02115                  "utf-16-le",
02116                  "utf-16-be",
02117                  "utf-32",
02118                  "utf-32-le",
02119                  "utf-32-be")
02120         for encoding in tests:
02121             buf = self.BytesIO()
02122             f = self.TextIOWrapper(buf, encoding=encoding)
02123             # Check if the BOM is written only once (see issue1753).
02124             f.write(data)
02125             f.write(data)
02126             f.seek(0)
02127             self.assertEqual(f.read(), data * 2)
02128             f.seek(0)
02129             self.assertEqual(f.read(), data * 2)
02130             self.assertEqual(buf.getvalue(), (data * 2).encode(encoding))

Here is the call graph for this function:

Definition at line 1784 of file test_io.py.

01784 
01785     def test_encoding(self):
01786         # Check the encoding attribute is always set, and valid
01787         b = self.BytesIO()
01788         t = self.TextIOWrapper(b, encoding="utf8")
01789         self.assertEqual(t.encoding, "utf8")
01790         t = self.TextIOWrapper(b)
01791         self.assertTrue(t.encoding is not None)
01792         codecs.lookup(t.encoding)

Here is the call graph for this function:

Definition at line 1793 of file test_io.py.

01793 
01794     def test_encoding_errors_reading(self):
01795         # (1) default
01796         b = self.BytesIO(b"abc\n\xff\n")
01797         t = self.TextIOWrapper(b, encoding="ascii")
01798         self.assertRaises(UnicodeError, t.read)
01799         # (2) explicit strict
01800         b = self.BytesIO(b"abc\n\xff\n")
01801         t = self.TextIOWrapper(b, encoding="ascii", errors="strict")
01802         self.assertRaises(UnicodeError, t.read)
01803         # (3) ignore
01804         b = self.BytesIO(b"abc\n\xff\n")
01805         t = self.TextIOWrapper(b, encoding="ascii", errors="ignore")
01806         self.assertEqual(t.read(), "abc\n\n")
01807         # (4) replace
01808         b = self.BytesIO(b"abc\n\xff\n")
01809         t = self.TextIOWrapper(b, encoding="ascii", errors="replace")
01810         self.assertEqual(t.read(), "abc\n\ufffd\n")

Here is the call graph for this function:

Definition at line 1811 of file test_io.py.

01811 
01812     def test_encoding_errors_writing(self):
01813         # (1) default
01814         b = self.BytesIO()
01815         t = self.TextIOWrapper(b, encoding="ascii")
01816         self.assertRaises(UnicodeError, t.write, "\xff")
01817         # (2) explicit strict
01818         b = self.BytesIO()
01819         t = self.TextIOWrapper(b, encoding="ascii", errors="strict")
01820         self.assertRaises(UnicodeError, t.write, "\xff")
01821         # (3) ignore
01822         b = self.BytesIO()
01823         t = self.TextIOWrapper(b, encoding="ascii", errors="ignore",
01824                              newline="\n")
01825         t.write("abc\xffdef\n")
01826         t.flush()
01827         self.assertEqual(b.getvalue(), b"abcdef\n")
01828         # (4) replace
01829         b = self.BytesIO()
01830         t = self.TextIOWrapper(b, encoding="ascii", errors="replace",
01831                              newline="\n")
01832         t.write("abc\xffdef\n")
01833         t.flush()
01834         self.assertEqual(b.getvalue(), b"abc?def\n")

Here is the call graph for this function:

Definition at line 1948 of file test_io.py.

01948 
01949     def test_error_through_destructor(self):
01950         # Test that the exception state is not modified by a destructor,
01951         # even if close() fails.
01952         rawio = self.CloseFailureIO()
01953         def f():
01954             self.TextIOWrapper(rawio).xyzzy
01955         with support.captured_output("stderr") as s:
01956             self.assertRaises(AttributeError, f)
01957         s = s.getvalue().strip()
01958         if s:
01959             # The destructor *may* have printed an unraisable error, check it
01960             self.assertEqual(len(s.splitlines()), 1)
01961             self.assertTrue(s.startswith("Exception IOError: "), s)
01962             self.assertTrue(s.endswith(" ignored"), s)

Here is the call graph for this function:

Definition at line 2257 of file test_io.py.

02257 
02258     def test_errors_property(self):
02259         with self.open(support.TESTFN, "w") as f:
02260             self.assertEqual(f.errors, "strict")
02261         with self.open(support.TESTFN, "w", errors="replace") as f:
02262             self.assertEqual(f.errors, "replace")

Here is the call graph for this function:

Definition at line 2285 of file test_io.py.

02285 
02286     def test_flush_error_on_close(self):
02287         txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
02288         def bad_flush():
02289             raise IOError()
02290         txt.flush = bad_flush
02291         self.assertRaises(IOError, txt.close) # exception not swallowed

Here is the call graph for this function:

Definition at line 2168 of file test_io.py.

02168 
02169     def test_issue1395_1(self):
02170         txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
02171 
02172         # read one char at a time
02173         reads = ""
02174         while True:
02175             c = txt.read(1)
02176             if not c:
02177                 break
02178             reads += c
02179         self.assertEqual(reads, self.normalized)

Here is the call graph for this function:

Definition at line 2180 of file test_io.py.

02180 
02181     def test_issue1395_2(self):
02182         txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
02183         txt._CHUNK_SIZE = 4
02184 
02185         reads = ""
02186         while True:
02187             c = txt.read(4)
02188             if not c:
02189                 break
02190             reads += c
02191         self.assertEqual(reads, self.normalized)

Here is the call graph for this function:

Definition at line 2192 of file test_io.py.

02192 
02193     def test_issue1395_3(self):
02194         txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
02195         txt._CHUNK_SIZE = 4
02196 
02197         reads = txt.read(4)
02198         reads += txt.read(4)
02199         reads += txt.readline()
02200         reads += txt.readline()
02201         reads += txt.readline()
02202         self.assertEqual(reads, self.normalized)

Here is the call graph for this function:

Definition at line 2203 of file test_io.py.

02203 
02204     def test_issue1395_4(self):
02205         txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
02206         txt._CHUNK_SIZE = 4
02207 
02208         reads = txt.read(4)
02209         reads += txt.read()
02210         self.assertEqual(reads, self.normalized)

Here is the call graph for this function:

Definition at line 2211 of file test_io.py.

02211 
02212     def test_issue1395_5(self):
02213         txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
02214         txt._CHUNK_SIZE = 4
02215 
02216         reads = txt.read(4)
02217         pos = txt.tell()
02218         txt.seek(0)
02219         txt.seek(pos)
02220         self.assertEqual(txt.read(4), "BBB\n")

Here is the call graph for this function:

Definition at line 2221 of file test_io.py.

02221 
02222     def test_issue2282(self):
02223         buffer = self.BytesIO(self.testdata)
02224         txt = self.TextIOWrapper(buffer, encoding="ascii")
02225 
02226         self.assertEqual(buffer.seekable(), txt.seekable())

Here is the call graph for this function:

Definition at line 1773 of file test_io.py.

01773 
01774     def test_line_buffering(self):
01775         r = self.BytesIO()
01776         b = self.BufferedWriter(r, 1000)
01777         t = self.TextIOWrapper(b, newline="\n", line_buffering=True)
01778         t.write("X")
01779         self.assertEqual(r.getvalue(), b"")  # No flush happened
01780         t.write("Y\nZ")
01781         self.assertEqual(r.getvalue(), b"XY\nZ")  # All got flushed
01782         t.write("A\rB")
01783         self.assertEqual(r.getvalue(), b"XY\nZA\rB")

Here is the call graph for this function:

Definition at line 2292 of file test_io.py.

02292 
02293     def test_multi_close(self):
02294         txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
02295         txt.close()
02296         txt.close()
02297         txt.close()
02298         self.assertRaises(ValueError, txt.flush)

Here is the call graph for this function:

Definition at line 1835 of file test_io.py.

01835 
01836     def test_newlines(self):
01837         input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ]
01838 
01839         tests = [
01840             [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ],
01841             [ '', input_lines ],
01842             [ '\n', [ "unix\n", "windows\r\n", "os9\rlast\n", "nonl" ] ],
01843             [ '\r\n', [ "unix\nwindows\r\n", "os9\rlast\nnonl" ] ],
01844             [ '\r', [ "unix\nwindows\r", "\nos9\r", "last\nnonl" ] ],
01845         ]
01846         encodings = (
01847             'utf-8', 'latin-1',
01848             'utf-16', 'utf-16-le', 'utf-16-be',
01849             'utf-32', 'utf-32-le', 'utf-32-be',
01850         )
01851 
01852         # Try a range of buffer sizes to test the case where \r is the last
01853         # character in TextIOWrapper._pending_line.
01854         for encoding in encodings:
01855             # XXX: str.encode() should return bytes
01856             data = bytes(''.join(input_lines).encode(encoding))
01857             for do_reads in (False, True):
01858                 for bufsize in range(1, 10):
01859                     for newline, exp_lines in tests:
01860                         bufio = self.BufferedReader(self.BytesIO(data), bufsize)
01861                         textio = self.TextIOWrapper(bufio, newline=newline,
01862                                                   encoding=encoding)
01863                         if do_reads:
01864                             got_lines = []
01865                             while True:
01866                                 c2 = textio.read(2)
01867                                 if c2 == '':
01868                                     break
01869                                 self.assertEqual(len(c2), 2)
01870                                 got_lines.append(c2 + textio.readline())
01871                         else:
01872                             got_lines = list(textio)
01873 
01874                         for got_line, exp_line in zip(got_lines, exp_lines):
01875                             self.assertEqual(got_line, exp_line)
01876                         self.assertEqual(len(got_lines), len(exp_lines))

Here is the call graph for this function:

Definition at line 1877 of file test_io.py.

01877 
01878     def test_newlines_input(self):
01879         testdata = b"AAA\nBB\x00B\nCCC\rDDD\rEEE\r\nFFF\r\nGGG"
01880         normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
01881         for newline, expected in [
01882             (None, normalized.decode("ascii").splitlines(True)),
01883             ("", testdata.decode("ascii").splitlines(True)),
01884             ("\n", ["AAA\n", "BB\x00B\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]),
01885             ("\r\n", ["AAA\nBB\x00B\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]),
01886             ("\r",  ["AAA\nBB\x00B\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]),
01887             ]:
01888             buf = self.BytesIO(testdata)
01889             txt = self.TextIOWrapper(buf, encoding="ascii", newline=newline)
01890             self.assertEqual(txt.readlines(), expected)
01891             txt.seek(0)
01892             self.assertEqual(txt.read(), "".join(expected))

Here is the call graph for this function:

Definition at line 1893 of file test_io.py.

01893 
01894     def test_newlines_output(self):
01895         testdict = {
01896             "": b"AAA\nBBB\nCCC\nX\rY\r\nZ",
01897             "\n": b"AAA\nBBB\nCCC\nX\rY\r\nZ",
01898             "\r": b"AAA\rBBB\rCCC\rX\rY\r\rZ",
01899             "\r\n": b"AAA\r\nBBB\r\nCCC\r\nX\rY\r\r\nZ",
01900             }
01901         tests = [(None, testdict[os.linesep])] + sorted(testdict.items())
01902         for newline, expected in tests:
01903             buf = self.BytesIO()
01904             txt = self.TextIOWrapper(buf, encoding="ascii", newline=newline)
01905             txt.write("AAA\nB")
01906             txt.write("BB\nCCC\n")
01907             txt.write("X\rY\r\nZ")
01908             txt.flush()
01909             self.assertEqual(buf.closed, False)
01910             self.assertEqual(buf.getvalue(), expected)

Here is the call graph for this function:

Definition at line 1925 of file test_io.py.

01925 
01926     def test_override_destructor(self):
01927         record = []
01928         class MyTextIO(self.TextIOWrapper):
01929             def __del__(self):
01930                 record.append(1)
01931                 try:
01932                     f = super().__del__
01933                 except AttributeError:
01934                     pass
01935                 else:
01936                     f()
01937             def close(self):
01938                 record.append(2)
01939                 super().close()
01940             def flush(self):
01941                 record.append(3)
01942                 super().flush()
01943         b = self.BytesIO()
01944         t = MyTextIO(b, encoding="ascii")
01945         del t
01946         support.gc_collect()
01947         self.assertEqual(record, [1, 2, 3])

Here is the call graph for this function:

def test.test_io.TextIOWrapperTest.test_rawio (   self) [inherited]

Definition at line 2310 of file test_io.py.

02310 
02311     def test_rawio(self):
02312         # Issue #12591: TextIOWrapper must work with raw I/O objects, so
02313         # that subprocess.Popen() can have the required unbuffered
02314         # semantics with universal_newlines=True.
02315         raw = self.MockRawIO([b'abc', b'def', b'ghi\njkl\nopq\n'])
02316         txt = self.TextIOWrapper(raw, encoding='ascii', newline='\n')
02317         # Reads
02318         self.assertEqual(txt.read(4), 'abcd')
02319         self.assertEqual(txt.readline(), 'efghi\n')
02320         self.assertEqual(list(txt), ['jkl\n', 'opq\n'])

Here is the call graph for this function:

Definition at line 2321 of file test_io.py.

02321 
02322     def test_rawio_write_through(self):
02323         # Issue #12591: with write_through=True, writes don't need a flush
02324         raw = self.MockRawIO([b'abc', b'def', b'ghi\njkl\nopq\n'])
02325         txt = self.TextIOWrapper(raw, encoding='ascii', newline='\n',
02326                                  write_through=True)
02327         txt.write('1')
02328         txt.write('23\n4')
02329         txt.write('5')
02330         self.assertEqual(b''.join(raw._write_stack), b'123\n45')

Here is the call graph for this function:

Definition at line 2157 of file test_io.py.

02157 
02158     def test_read_by_chunk(self):
02159         # make sure "\r\n" straddles 128 char boundary.
02160         txt = self.TextIOWrapper(self.BytesIO(b"A" * 127 + b"\r\nB"))
02161         reads = ""
02162         while True:
02163             c = txt.read(128)
02164             if not c:
02165                 break
02166             reads += c
02167         self.assertEqual(reads, "A"*127+"\nB")

Here is the call graph for this function:

Definition at line 2138 of file test_io.py.

02138 
02139     def test_read_one_by_one(self):
02140         txt = self.TextIOWrapper(self.BytesIO(b"AA\r\nBB"))
02141         reads = ""
02142         while True:
02143             c = txt.read(1)
02144             if not c:
02145                 break
02146             reads += c
02147         self.assertEqual(reads, "AA\nBB")

Here is the call graph for this function:

Definition at line 2148 of file test_io.py.

02148 
02149     def test_readlines(self):
02150         txt = self.TextIOWrapper(self.BytesIO(b"AA\nBB\nCC"))
02151         self.assertEqual(txt.readlines(), ["AA\n", "BB\n", "CC"])
02152         txt.seek(0)
02153         self.assertEqual(txt.readlines(None), ["AA\n", "BB\n", "CC"])
02154         txt.seek(0)
02155         self.assertEqual(txt.readlines(5), ["AA\n", "BB\n"])

Here is the call graph for this function:

Definition at line 2304 of file test_io.py.

02304 
02305     def test_readonly_attributes(self):
02306         txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
02307         buf = self.BytesIO(self.testdata)
02308         with self.assertRaises(AttributeError):
02309             txt.buffer = buf

Here is the call graph for this function:

def test.test_io.TextIOWrapperTest.test_repr (   self) [inherited]

Definition at line 1756 of file test_io.py.

01756 
01757     def test_repr(self):
01758         raw = self.BytesIO("hello".encode("utf-8"))
01759         b = self.BufferedReader(raw)
01760         t = self.TextIOWrapper(b, encoding="utf-8")
01761         modname = self.TextIOWrapper.__module__
01762         self.assertEqual(repr(t),
01763                          "<%s.TextIOWrapper encoding='utf-8'>" % modname)
01764         raw.name = "dummy"
01765         self.assertEqual(repr(t),
01766                          "<%s.TextIOWrapper name='dummy' encoding='utf-8'>" % modname)
01767         t.mode = "r"
01768         self.assertEqual(repr(t),
01769                          "<%s.TextIOWrapper name='dummy' mode='r' encoding='utf-8'>" % modname)
01770         raw.name = b"dummy"
01771         self.assertEqual(repr(t),
01772                          "<%s.TextIOWrapper name=b'dummy' mode='r' encoding='utf-8'>" % modname)

Here is the call graph for this function:

Definition at line 2064 of file test_io.py.

02064 
02065     def test_seek_and_tell(self):
02066         #Test seek/tell using the StatefulIncrementalDecoder.
02067         # Make test faster by doing smaller seeks
02068         CHUNK_SIZE = 128
02069 
02070         def test_seek_and_tell_with_data(data, min_pos=0):
02071             """Tell/seek to various points within a data stream and ensure
02072             that the decoded data returned by read() is consistent."""
02073             f = self.open(support.TESTFN, 'wb')
02074             f.write(data)
02075             f.close()
02076             f = self.open(support.TESTFN, encoding='test_decoder')
02077             f._CHUNK_SIZE = CHUNK_SIZE
02078             decoded = f.read()
02079             f.close()
02080 
02081             for i in range(min_pos, len(decoded) + 1): # seek positions
02082                 for j in [1, 5, len(decoded) - i]: # read lengths
02083                     f = self.open(support.TESTFN, encoding='test_decoder')
02084                     self.assertEqual(f.read(i), decoded[:i])
02085                     cookie = f.tell()
02086                     self.assertEqual(f.read(j), decoded[i:i + j])
02087                     f.seek(cookie)
02088                     self.assertEqual(f.read(), decoded[i:])
02089                     f.close()
02090 
02091         # Enable the test decoder.
02092         StatefulIncrementalDecoder.codecEnabled = 1
02093 
02094         # Run the tests.
02095         try:
02096             # Try each test case.
02097             for input, _, _ in StatefulIncrementalDecoderTest.test_cases:
02098                 test_seek_and_tell_with_data(input)
02099 
02100             # Position each test case so that it crosses a chunk boundary.
02101             for input, _, _ in StatefulIncrementalDecoderTest.test_cases:
02102                 offset = CHUNK_SIZE - len(input)//2
02103                 prefix = b'.'*offset
02104                 # Don't bother seeking into the prefix (takes too long).
02105                 min_pos = offset*2
02106                 test_seek_and_tell_with_data(prefix + input, min_pos)
02107 
02108         # Ensure our test decoder won't interfere with subsequent tests.
02109         finally:
02110             StatefulIncrementalDecoder.codecEnabled = 0

Here is the call graph for this function:

Definition at line 2242 of file test_io.py.

02242 
02243     def test_seek_bom(self):
02244         # Same test, but when seeking manually
02245         filename = support.TESTFN
02246         for charset in ('utf-8-sig', 'utf-16', 'utf-32'):
02247             with self.open(filename, 'w', encoding=charset) as f:
02248                 f.write('aaa')
02249                 pos = f.tell()
02250             with self.open(filename, 'r+', encoding=charset) as f:
02251                 f.seek(pos)
02252                 f.write('zzz')
02253                 f.seek(0)
02254                 f.write('bbb')
02255             with self.open(filename, 'rb') as f:
02256                 self.assertEqual(f.read(), 'bbbzzz'.encode(charset))

Here is the call graph for this function:

def test.test_io.TextIOWrapperTest.test_seeking (   self) [inherited]

Definition at line 2036 of file test_io.py.

02036 
02037     def test_seeking(self):
02038         chunk_size = _default_chunk_size()
02039         prefix_size = chunk_size - 2
02040         u_prefix = "a" * prefix_size
02041         prefix = bytes(u_prefix.encode("utf-8"))
02042         self.assertEqual(len(u_prefix), len(prefix))
02043         u_suffix = "\u8888\n"
02044         suffix = bytes(u_suffix.encode("utf-8"))
02045         line = prefix + suffix
02046         with self.open(support.TESTFN, "wb") as f:
02047             f.write(line*2)
02048         with self.open(support.TESTFN, "r", encoding="utf-8") as f:
02049             s = f.read(prefix_size)
02050             self.assertEqual(s, str(prefix, "ascii"))
02051             self.assertEqual(f.tell(), prefix_size)
02052             self.assertEqual(f.readline(), u_suffix)

Here is the call graph for this function:

Definition at line 2053 of file test_io.py.

02053 
02054     def test_seeking_too(self):
02055         # Regression test for a specific bug
02056         data = b'\xe0\xbf\xbf\n'
02057         with self.open(support.TESTFN, "wb") as f:
02058             f.write(data)
02059         with self.open(support.TESTFN, "r", encoding="utf-8") as f:
02060             f._CHUNK_SIZE  # Just test that it exists
02061             f._CHUNK_SIZE = 2
02062             f.readline()
02063             f.tell()

Here is the call graph for this function:

def test.test_io.TextIOWrapperTest.test_telling (   self) [inherited]

Definition at line 2016 of file test_io.py.

02016 
02017     def test_telling(self):
02018         f = self.open(support.TESTFN, "w+", encoding="utf8")
02019         p0 = f.tell()
02020         f.write("\xff\n")
02021         p1 = f.tell()
02022         f.write("\xff\n")
02023         p2 = f.tell()
02024         f.seek(0)
02025         self.assertEqual(f.tell(), p0)
02026         self.assertEqual(f.readline(), "\xff\n")
02027         self.assertEqual(f.tell(), p1)
02028         self.assertEqual(f.readline(), "\xff\n")
02029         self.assertEqual(f.tell(), p2)
02030         f.seek(0)
02031         for line in f:
02032             self.assertEqual(line, "\xff\n")
02033             self.assertRaises(IOError, f.tell)
02034         self.assertEqual(f.tell(), p2)
02035         f.close()

Here is the call graph for this function:

Definition at line 2264 of file test_io.py.

02264 
02265     def test_threads_write(self):
02266         # Issue6750: concurrent writes could duplicate data
02267         event = threading.Event()
02268         with self.open(support.TESTFN, "w", buffering=1) as f:
02269             def run(n):
02270                 text = "Thread%03d\n" % n
02271                 event.wait()
02272                 f.write(text)
02273             threads = [threading.Thread(target=lambda n=x: run(n))
02274                        for x in range(20)]
02275             for t in threads:
02276                 t.start()
02277             time.sleep(0.02)
02278             event.set()
02279             for t in threads:
02280                 t.join()
02281         with self.open(support.TESTFN) as f:
02282             content = f.read()
02283             for n in range(20):
02284                 self.assertEqual(content.count("Thread%03d\n" % n), 1)

Here is the call graph for this function:

Definition at line 2131 of file test_io.py.

02131 
02132     def test_unreadable(self):
02133         class UnReadable(self.BytesIO):
02134             def readable(self):
02135                 return False
02136         txt = self.TextIOWrapper(UnReadable())
02137         self.assertRaises(IOError, txt.read)

Here is the call graph for this function:

Definition at line 2299 of file test_io.py.

02299 
02300     def test_unseekable(self):
02301         txt = self.TextIOWrapper(self.MockUnseekableIO(self.testdata))
02302         self.assertRaises(self.UnsupportedOperation, txt.tell)
02303         self.assertRaises(self.UnsupportedOperation, txt.seek, 0)

Here is the call graph for this function:


Member Data Documentation

Definition at line 1723 of file test_io.py.

Definition at line 1722 of file test_io.py.


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