/Lib/test/test_io.py

http://unladen-swallow.googlecode.com/ · Python · 1347 lines · 1145 code · 150 blank · 52 comment · 69 complexity · 537f232ea31be27a17281665b6069a17 MD5 · raw file

  1. """Unit tests for io.py."""
  2. from __future__ import print_function
  3. from __future__ import unicode_literals
  4. import os
  5. import sys
  6. import time
  7. import array
  8. import threading
  9. import random
  10. import unittest
  11. from itertools import chain, cycle
  12. from test import test_support
  13. import codecs
  14. import io # The module under test
  15. class MockRawIO(io.RawIOBase):
  16. def __init__(self, read_stack=()):
  17. self._read_stack = list(read_stack)
  18. self._write_stack = []
  19. def read(self, n=None):
  20. try:
  21. return self._read_stack.pop(0)
  22. except:
  23. return b""
  24. def write(self, b):
  25. self._write_stack.append(b[:])
  26. return len(b)
  27. def writable(self):
  28. return True
  29. def fileno(self):
  30. return 42
  31. def readable(self):
  32. return True
  33. def seekable(self):
  34. return True
  35. def seek(self, pos, whence):
  36. pass
  37. def tell(self):
  38. return 42
  39. class MockFileIO(io.BytesIO):
  40. def __init__(self, data):
  41. self.read_history = []
  42. io.BytesIO.__init__(self, data)
  43. def read(self, n=None):
  44. res = io.BytesIO.read(self, n)
  45. self.read_history.append(None if res is None else len(res))
  46. return res
  47. class MockNonBlockWriterIO(io.RawIOBase):
  48. def __init__(self, blocking_script):
  49. self._blocking_script = list(blocking_script)
  50. self._write_stack = []
  51. def write(self, b):
  52. self._write_stack.append(b[:])
  53. n = self._blocking_script.pop(0)
  54. if (n < 0):
  55. raise io.BlockingIOError(0, "test blocking", -n)
  56. else:
  57. return n
  58. def writable(self):
  59. return True
  60. class IOTest(unittest.TestCase):
  61. def tearDown(self):
  62. test_support.unlink(test_support.TESTFN)
  63. def write_ops(self, f):
  64. self.assertEqual(f.write(b"blah."), 5)
  65. self.assertEqual(f.seek(0), 0)
  66. self.assertEqual(f.write(b"Hello."), 6)
  67. self.assertEqual(f.tell(), 6)
  68. self.assertEqual(f.seek(-1, 1), 5)
  69. self.assertEqual(f.tell(), 5)
  70. self.assertEqual(f.write(bytearray(b" world\n\n\n")), 9)
  71. self.assertEqual(f.seek(0), 0)
  72. self.assertEqual(f.write(b"h"), 1)
  73. self.assertEqual(f.seek(-1, 2), 13)
  74. self.assertEqual(f.tell(), 13)
  75. self.assertEqual(f.truncate(12), 12)
  76. self.assertEqual(f.tell(), 12)
  77. self.assertRaises(TypeError, f.seek, 0.0)
  78. def read_ops(self, f, buffered=False):
  79. data = f.read(5)
  80. self.assertEqual(data, b"hello")
  81. data = bytearray(data)
  82. self.assertEqual(f.readinto(data), 5)
  83. self.assertEqual(data, b" worl")
  84. self.assertEqual(f.readinto(data), 2)
  85. self.assertEqual(len(data), 5)
  86. self.assertEqual(data[:2], b"d\n")
  87. self.assertEqual(f.seek(0), 0)
  88. self.assertEqual(f.read(20), b"hello world\n")
  89. self.assertEqual(f.read(1), b"")
  90. self.assertEqual(f.readinto(bytearray(b"x")), 0)
  91. self.assertEqual(f.seek(-6, 2), 6)
  92. self.assertEqual(f.read(5), b"world")
  93. self.assertEqual(f.read(0), b"")
  94. self.assertEqual(f.readinto(bytearray()), 0)
  95. self.assertEqual(f.seek(-6, 1), 5)
  96. self.assertEqual(f.read(5), b" worl")
  97. self.assertEqual(f.tell(), 10)
  98. self.assertRaises(TypeError, f.seek, 0.0)
  99. if buffered:
  100. f.seek(0)
  101. self.assertEqual(f.read(), b"hello world\n")
  102. f.seek(6)
  103. self.assertEqual(f.read(), b"world\n")
  104. self.assertEqual(f.read(), b"")
  105. LARGE = 2**31
  106. def large_file_ops(self, f):
  107. assert f.readable()
  108. assert f.writable()
  109. self.assertEqual(f.seek(self.LARGE), self.LARGE)
  110. self.assertEqual(f.tell(), self.LARGE)
  111. self.assertEqual(f.write(b"xxx"), 3)
  112. self.assertEqual(f.tell(), self.LARGE + 3)
  113. self.assertEqual(f.seek(-1, 1), self.LARGE + 2)
  114. self.assertEqual(f.truncate(), self.LARGE + 2)
  115. self.assertEqual(f.tell(), self.LARGE + 2)
  116. self.assertEqual(f.seek(0, 2), self.LARGE + 2)
  117. self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
  118. self.assertEqual(f.tell(), self.LARGE + 1)
  119. self.assertEqual(f.seek(0, 2), self.LARGE + 1)
  120. self.assertEqual(f.seek(-1, 2), self.LARGE)
  121. self.assertEqual(f.read(2), b"x")
  122. def test_raw_file_io(self):
  123. f = io.open(test_support.TESTFN, "wb", buffering=0)
  124. self.assertEqual(f.readable(), False)
  125. self.assertEqual(f.writable(), True)
  126. self.assertEqual(f.seekable(), True)
  127. self.write_ops(f)
  128. f.close()
  129. f = io.open(test_support.TESTFN, "rb", buffering=0)
  130. self.assertEqual(f.readable(), True)
  131. self.assertEqual(f.writable(), False)
  132. self.assertEqual(f.seekable(), True)
  133. self.read_ops(f)
  134. f.close()
  135. def test_buffered_file_io(self):
  136. f = io.open(test_support.TESTFN, "wb")
  137. self.assertEqual(f.readable(), False)
  138. self.assertEqual(f.writable(), True)
  139. self.assertEqual(f.seekable(), True)
  140. self.write_ops(f)
  141. f.close()
  142. f = io.open(test_support.TESTFN, "rb")
  143. self.assertEqual(f.readable(), True)
  144. self.assertEqual(f.writable(), False)
  145. self.assertEqual(f.seekable(), True)
  146. self.read_ops(f, True)
  147. f.close()
  148. def test_readline(self):
  149. f = io.open(test_support.TESTFN, "wb")
  150. f.write(b"abc\ndef\nxyzzy\nfoo")
  151. f.close()
  152. f = io.open(test_support.TESTFN, "rb")
  153. self.assertEqual(f.readline(), b"abc\n")
  154. self.assertEqual(f.readline(10), b"def\n")
  155. self.assertEqual(f.readline(2), b"xy")
  156. self.assertEqual(f.readline(4), b"zzy\n")
  157. self.assertEqual(f.readline(), b"foo")
  158. f.close()
  159. def test_raw_bytes_io(self):
  160. f = io.BytesIO()
  161. self.write_ops(f)
  162. data = f.getvalue()
  163. self.assertEqual(data, b"hello world\n")
  164. f = io.BytesIO(data)
  165. self.read_ops(f, True)
  166. def test_large_file_ops(self):
  167. # On Windows and Mac OSX this test comsumes large resources; It takes
  168. # a long time to build the >2GB file and takes >2GB of disk space
  169. # therefore the resource must be enabled to run this test.
  170. if sys.platform[:3] in ('win', 'os2') or sys.platform == 'darwin':
  171. if not test_support.is_resource_enabled("largefile"):
  172. print("\nTesting large file ops skipped on %s." % sys.platform,
  173. file=sys.stderr)
  174. print("It requires %d bytes and a long time." % self.LARGE,
  175. file=sys.stderr)
  176. print("Use 'regrtest.py -u largefile test_io' to run it.",
  177. file=sys.stderr)
  178. return
  179. f = io.open(test_support.TESTFN, "w+b", 0)
  180. self.large_file_ops(f)
  181. f.close()
  182. f = io.open(test_support.TESTFN, "w+b")
  183. self.large_file_ops(f)
  184. f.close()
  185. def test_with_open(self):
  186. for bufsize in (0, 1, 100):
  187. f = None
  188. with open(test_support.TESTFN, "wb", bufsize) as f:
  189. f.write(b"xxx")
  190. self.assertEqual(f.closed, True)
  191. f = None
  192. try:
  193. with open(test_support.TESTFN, "wb", bufsize) as f:
  194. 1/0
  195. except ZeroDivisionError:
  196. self.assertEqual(f.closed, True)
  197. else:
  198. self.fail("1/0 didn't raise an exception")
  199. # issue 5008
  200. def test_append_mode_tell(self):
  201. with io.open(test_support.TESTFN, "wb") as f:
  202. f.write(b"xxx")
  203. with io.open(test_support.TESTFN, "ab", buffering=0) as f:
  204. self.assertEqual(f.tell(), 3)
  205. with io.open(test_support.TESTFN, "ab") as f:
  206. self.assertEqual(f.tell(), 3)
  207. with io.open(test_support.TESTFN, "a") as f:
  208. self.assert_(f.tell() > 0)
  209. def test_destructor(self):
  210. record = []
  211. class MyFileIO(io.FileIO):
  212. def __del__(self):
  213. record.append(1)
  214. io.FileIO.__del__(self)
  215. def close(self):
  216. record.append(2)
  217. io.FileIO.close(self)
  218. def flush(self):
  219. record.append(3)
  220. io.FileIO.flush(self)
  221. f = MyFileIO(test_support.TESTFN, "w")
  222. f.write("xxx")
  223. del f
  224. self.assertEqual(record, [1, 2, 3])
  225. def test_close_flushes(self):
  226. f = io.open(test_support.TESTFN, "wb")
  227. f.write(b"xxx")
  228. f.close()
  229. f = io.open(test_support.TESTFN, "rb")
  230. self.assertEqual(f.read(), b"xxx")
  231. f.close()
  232. def XXXtest_array_writes(self):
  233. # XXX memory view not available yet
  234. a = array.array('i', range(10))
  235. n = len(memoryview(a))
  236. f = io.open(test_support.TESTFN, "wb", 0)
  237. self.assertEqual(f.write(a), n)
  238. f.close()
  239. f = io.open(test_support.TESTFN, "wb")
  240. self.assertEqual(f.write(a), n)
  241. f.close()
  242. def test_closefd(self):
  243. self.assertRaises(ValueError, io.open, test_support.TESTFN, 'w',
  244. closefd=False)
  245. def testReadClosed(self):
  246. with io.open(test_support.TESTFN, "w") as f:
  247. f.write("egg\n")
  248. with io.open(test_support.TESTFN, "r") as f:
  249. file = io.open(f.fileno(), "r", closefd=False)
  250. self.assertEqual(file.read(), "egg\n")
  251. file.seek(0)
  252. file.close()
  253. self.assertRaises(ValueError, file.read)
  254. def test_no_closefd_with_filename(self):
  255. # can't use closefd in combination with a file name
  256. self.assertRaises(ValueError,
  257. io.open, test_support.TESTFN, "r", closefd=False)
  258. def test_closefd_attr(self):
  259. with io.open(test_support.TESTFN, "wb") as f:
  260. f.write(b"egg\n")
  261. with io.open(test_support.TESTFN, "r") as f:
  262. self.assertEqual(f.buffer.raw.closefd, True)
  263. file = io.open(f.fileno(), "r", closefd=False)
  264. self.assertEqual(file.buffer.raw.closefd, False)
  265. class MemorySeekTestMixin:
  266. def testInit(self):
  267. buf = self.buftype("1234567890")
  268. bytesIo = self.ioclass(buf)
  269. def testRead(self):
  270. buf = self.buftype("1234567890")
  271. bytesIo = self.ioclass(buf)
  272. self.assertEquals(buf[:1], bytesIo.read(1))
  273. self.assertEquals(buf[1:5], bytesIo.read(4))
  274. self.assertEquals(buf[5:], bytesIo.read(900))
  275. self.assertEquals(self.EOF, bytesIo.read())
  276. def testReadNoArgs(self):
  277. buf = self.buftype("1234567890")
  278. bytesIo = self.ioclass(buf)
  279. self.assertEquals(buf, bytesIo.read())
  280. self.assertEquals(self.EOF, bytesIo.read())
  281. def testSeek(self):
  282. buf = self.buftype("1234567890")
  283. bytesIo = self.ioclass(buf)
  284. bytesIo.read(5)
  285. bytesIo.seek(0)
  286. self.assertEquals(buf, bytesIo.read())
  287. bytesIo.seek(3)
  288. self.assertEquals(buf[3:], bytesIo.read())
  289. self.assertRaises(TypeError, bytesIo.seek, 0.0)
  290. def testTell(self):
  291. buf = self.buftype("1234567890")
  292. bytesIo = self.ioclass(buf)
  293. self.assertEquals(0, bytesIo.tell())
  294. bytesIo.seek(5)
  295. self.assertEquals(5, bytesIo.tell())
  296. bytesIo.seek(10000)
  297. self.assertEquals(10000, bytesIo.tell())
  298. class BytesIOTest(MemorySeekTestMixin, unittest.TestCase):
  299. @staticmethod
  300. def buftype(s):
  301. return s.encode("utf-8")
  302. ioclass = io.BytesIO
  303. EOF = b""
  304. class StringIOTest(MemorySeekTestMixin, unittest.TestCase):
  305. buftype = str
  306. ioclass = io.StringIO
  307. EOF = ""
  308. class BufferedReaderTest(unittest.TestCase):
  309. def testRead(self):
  310. rawio = MockRawIO((b"abc", b"d", b"efg"))
  311. bufio = io.BufferedReader(rawio)
  312. self.assertEquals(b"abcdef", bufio.read(6))
  313. def testBuffering(self):
  314. data = b"abcdefghi"
  315. dlen = len(data)
  316. tests = [
  317. [ 100, [ 3, 1, 4, 8 ], [ dlen, 0 ] ],
  318. [ 100, [ 3, 3, 3], [ dlen ] ],
  319. [ 4, [ 1, 2, 4, 2 ], [ 4, 4, 1 ] ],
  320. ]
  321. for bufsize, buf_read_sizes, raw_read_sizes in tests:
  322. rawio = MockFileIO(data)
  323. bufio = io.BufferedReader(rawio, buffer_size=bufsize)
  324. pos = 0
  325. for nbytes in buf_read_sizes:
  326. self.assertEquals(bufio.read(nbytes), data[pos:pos+nbytes])
  327. pos += nbytes
  328. self.assertEquals(rawio.read_history, raw_read_sizes)
  329. def testReadNonBlocking(self):
  330. # Inject some None's in there to simulate EWOULDBLOCK
  331. rawio = MockRawIO((b"abc", b"d", None, b"efg", None, None))
  332. bufio = io.BufferedReader(rawio)
  333. self.assertEquals(b"abcd", bufio.read(6))
  334. self.assertEquals(b"e", bufio.read(1))
  335. self.assertEquals(b"fg", bufio.read())
  336. self.assert_(None is bufio.read())
  337. self.assertEquals(b"", bufio.read())
  338. def testReadToEof(self):
  339. rawio = MockRawIO((b"abc", b"d", b"efg"))
  340. bufio = io.BufferedReader(rawio)
  341. self.assertEquals(b"abcdefg", bufio.read(9000))
  342. def testReadNoArgs(self):
  343. rawio = MockRawIO((b"abc", b"d", b"efg"))
  344. bufio = io.BufferedReader(rawio)
  345. self.assertEquals(b"abcdefg", bufio.read())
  346. def testFileno(self):
  347. rawio = MockRawIO((b"abc", b"d", b"efg"))
  348. bufio = io.BufferedReader(rawio)
  349. self.assertEquals(42, bufio.fileno())
  350. def testFilenoNoFileno(self):
  351. # XXX will we always have fileno() function? If so, kill
  352. # this test. Else, write it.
  353. pass
  354. def testThreads(self):
  355. try:
  356. # Write out many bytes with exactly the same number of 0's,
  357. # 1's... 255's. This will help us check that concurrent reading
  358. # doesn't duplicate or forget contents.
  359. N = 1000
  360. l = range(256) * N
  361. random.shuffle(l)
  362. s = bytes(bytearray(l))
  363. with io.open(test_support.TESTFN, "wb") as f:
  364. f.write(s)
  365. with io.open(test_support.TESTFN, "rb", buffering=0) as raw:
  366. bufio = io.BufferedReader(raw, 8)
  367. errors = []
  368. results = []
  369. def f():
  370. try:
  371. # Intra-buffer read then buffer-flushing read
  372. for n in cycle([1, 19]):
  373. s = bufio.read(n)
  374. if not s:
  375. break
  376. # list.append() is atomic
  377. results.append(s)
  378. except Exception as e:
  379. errors.append(e)
  380. raise
  381. threads = [threading.Thread(target=f) for x in range(20)]
  382. for t in threads:
  383. t.start()
  384. time.sleep(0.02) # yield
  385. for t in threads:
  386. t.join()
  387. self.assertFalse(errors,
  388. "the following exceptions were caught: %r" % errors)
  389. s = b''.join(results)
  390. for i in range(256):
  391. c = bytes(bytearray([i]))
  392. self.assertEqual(s.count(c), N)
  393. finally:
  394. test_support.unlink(test_support.TESTFN)
  395. class BufferedWriterTest(unittest.TestCase):
  396. def testWrite(self):
  397. # Write to the buffered IO but don't overflow the buffer.
  398. writer = MockRawIO()
  399. bufio = io.BufferedWriter(writer, 8)
  400. bufio.write(b"abc")
  401. self.assertFalse(writer._write_stack)
  402. def testWriteOverflow(self):
  403. writer = MockRawIO()
  404. bufio = io.BufferedWriter(writer, 8)
  405. bufio.write(b"abc")
  406. bufio.write(b"defghijkl")
  407. self.assertEquals(b"abcdefghijkl", writer._write_stack[0])
  408. def testWriteNonBlocking(self):
  409. raw = MockNonBlockWriterIO((9, 2, 22, -6, 10, 12, 12))
  410. bufio = io.BufferedWriter(raw, 8, 16)
  411. bufio.write(b"asdf")
  412. bufio.write(b"asdfa")
  413. self.assertEquals(b"asdfasdfa", raw._write_stack[0])
  414. bufio.write(b"asdfasdfasdf")
  415. self.assertEquals(b"asdfasdfasdf", raw._write_stack[1])
  416. bufio.write(b"asdfasdfasdf")
  417. self.assertEquals(b"dfasdfasdf", raw._write_stack[2])
  418. self.assertEquals(b"asdfasdfasdf", raw._write_stack[3])
  419. bufio.write(b"asdfasdfasdf")
  420. # XXX I don't like this test. It relies too heavily on how the
  421. # algorithm actually works, which we might change. Refactor
  422. # later.
  423. def testFileno(self):
  424. rawio = MockRawIO((b"abc", b"d", b"efg"))
  425. bufio = io.BufferedWriter(rawio)
  426. self.assertEquals(42, bufio.fileno())
  427. def testFlush(self):
  428. writer = MockRawIO()
  429. bufio = io.BufferedWriter(writer, 8)
  430. bufio.write(b"abc")
  431. bufio.flush()
  432. self.assertEquals(b"abc", writer._write_stack[0])
  433. def testThreads(self):
  434. # BufferedWriter should not raise exceptions or crash
  435. # when called from multiple threads.
  436. try:
  437. # We use a real file object because it allows us to
  438. # exercise situations where the GIL is released before
  439. # writing the buffer to the raw streams. This is in addition
  440. # to concurrency issues due to switching threads in the middle
  441. # of Python code.
  442. with io.open(test_support.TESTFN, "wb", buffering=0) as raw:
  443. bufio = io.BufferedWriter(raw, 8)
  444. errors = []
  445. def f():
  446. try:
  447. # Write enough bytes to flush the buffer
  448. s = b"a" * 19
  449. for i in range(50):
  450. bufio.write(s)
  451. except Exception as e:
  452. errors.append(e)
  453. raise
  454. threads = [threading.Thread(target=f) for x in range(20)]
  455. for t in threads:
  456. t.start()
  457. time.sleep(0.02) # yield
  458. for t in threads:
  459. t.join()
  460. self.assertFalse(errors,
  461. "the following exceptions were caught: %r" % errors)
  462. finally:
  463. test_support.unlink(test_support.TESTFN)
  464. class BufferedRWPairTest(unittest.TestCase):
  465. def testRWPair(self):
  466. r = MockRawIO(())
  467. w = MockRawIO()
  468. pair = io.BufferedRWPair(r, w)
  469. self.assertFalse(pair.closed)
  470. # XXX More Tests
  471. class BufferedRandomTest(unittest.TestCase):
  472. def testReadAndWrite(self):
  473. raw = MockRawIO((b"asdf", b"ghjk"))
  474. rw = io.BufferedRandom(raw, 8, 12)
  475. self.assertEqual(b"as", rw.read(2))
  476. rw.write(b"ddd")
  477. rw.write(b"eee")
  478. self.assertFalse(raw._write_stack) # Buffer writes
  479. self.assertEqual(b"ghjk", rw.read()) # This read forces write flush
  480. self.assertEquals(b"dddeee", raw._write_stack[0])
  481. def testSeekAndTell(self):
  482. raw = io.BytesIO(b"asdfghjkl")
  483. rw = io.BufferedRandom(raw)
  484. self.assertEquals(b"as", rw.read(2))
  485. self.assertEquals(2, rw.tell())
  486. rw.seek(0, 0)
  487. self.assertEquals(b"asdf", rw.read(4))
  488. rw.write(b"asdf")
  489. rw.seek(0, 0)
  490. self.assertEquals(b"asdfasdfl", rw.read())
  491. self.assertEquals(9, rw.tell())
  492. rw.seek(-4, 2)
  493. self.assertEquals(5, rw.tell())
  494. rw.seek(2, 1)
  495. self.assertEquals(7, rw.tell())
  496. self.assertEquals(b"fl", rw.read(11))
  497. self.assertRaises(TypeError, rw.seek, 0.0)
  498. # To fully exercise seek/tell, the StatefulIncrementalDecoder has these
  499. # properties:
  500. # - A single output character can correspond to many bytes of input.
  501. # - The number of input bytes to complete the character can be
  502. # undetermined until the last input byte is received.
  503. # - The number of input bytes can vary depending on previous input.
  504. # - A single input byte can correspond to many characters of output.
  505. # - The number of output characters can be undetermined until the
  506. # last input byte is received.
  507. # - The number of output characters can vary depending on previous input.
  508. class StatefulIncrementalDecoder(codecs.IncrementalDecoder):
  509. """
  510. For testing seek/tell behavior with a stateful, buffering decoder.
  511. Input is a sequence of words. Words may be fixed-length (length set
  512. by input) or variable-length (period-terminated). In variable-length
  513. mode, extra periods are ignored. Possible words are:
  514. - 'i' followed by a number sets the input length, I (maximum 99).
  515. When I is set to 0, words are space-terminated.
  516. - 'o' followed by a number sets the output length, O (maximum 99).
  517. - Any other word is converted into a word followed by a period on
  518. the output. The output word consists of the input word truncated
  519. or padded out with hyphens to make its length equal to O. If O
  520. is 0, the word is output verbatim without truncating or padding.
  521. I and O are initially set to 1. When I changes, any buffered input is
  522. re-scanned according to the new I. EOF also terminates the last word.
  523. """
  524. def __init__(self, errors='strict'):
  525. codecs.IncrementalDecoder.__init__(self, errors)
  526. self.reset()
  527. def __repr__(self):
  528. return '<SID %x>' % id(self)
  529. def reset(self):
  530. self.i = 1
  531. self.o = 1
  532. self.buffer = bytearray()
  533. def getstate(self):
  534. i, o = self.i ^ 1, self.o ^ 1 # so that flags = 0 after reset()
  535. return bytes(self.buffer), i*100 + o
  536. def setstate(self, state):
  537. buffer, io = state
  538. self.buffer = bytearray(buffer)
  539. i, o = divmod(io, 100)
  540. self.i, self.o = i ^ 1, o ^ 1
  541. def decode(self, input, final=False):
  542. output = ''
  543. for b in input:
  544. if self.i == 0: # variable-length, terminated with period
  545. if b == '.':
  546. if self.buffer:
  547. output += self.process_word()
  548. else:
  549. self.buffer.append(b)
  550. else: # fixed-length, terminate after self.i bytes
  551. self.buffer.append(b)
  552. if len(self.buffer) == self.i:
  553. output += self.process_word()
  554. if final and self.buffer: # EOF terminates the last word
  555. output += self.process_word()
  556. return output
  557. def process_word(self):
  558. output = ''
  559. if self.buffer[0] == ord('i'):
  560. self.i = min(99, int(self.buffer[1:] or 0)) # set input length
  561. elif self.buffer[0] == ord('o'):
  562. self.o = min(99, int(self.buffer[1:] or 0)) # set output length
  563. else:
  564. output = self.buffer.decode('ascii')
  565. if len(output) < self.o:
  566. output += '-'*self.o # pad out with hyphens
  567. if self.o:
  568. output = output[:self.o] # truncate to output length
  569. output += '.'
  570. self.buffer = bytearray()
  571. return output
  572. codecEnabled = False
  573. @classmethod
  574. def lookupTestDecoder(cls, name):
  575. if cls.codecEnabled and name == 'test_decoder':
  576. latin1 = codecs.lookup('latin-1')
  577. return codecs.CodecInfo(
  578. name='test_decoder', encode=latin1.encode, decode=None,
  579. incrementalencoder=None,
  580. streamreader=None, streamwriter=None,
  581. incrementaldecoder=cls)
  582. # Register the previous decoder for testing.
  583. # Disabled by default, tests will enable it.
  584. codecs.register(StatefulIncrementalDecoder.lookupTestDecoder)
  585. class StatefulIncrementalDecoderTest(unittest.TestCase):
  586. """
  587. Make sure the StatefulIncrementalDecoder actually works.
  588. """
  589. test_cases = [
  590. # I=1, O=1 (fixed-length input == fixed-length output)
  591. (b'abcd', False, 'a.b.c.d.'),
  592. # I=0, O=0 (variable-length input, variable-length output)
  593. (b'oiabcd', True, 'abcd.'),
  594. # I=0, O=0 (should ignore extra periods)
  595. (b'oi...abcd...', True, 'abcd.'),
  596. # I=0, O=6 (variable-length input, fixed-length output)
  597. (b'i.o6.x.xyz.toolongtofit.', False, 'x-----.xyz---.toolon.'),
  598. # I=2, O=6 (fixed-length input < fixed-length output)
  599. (b'i.i2.o6xyz', True, 'xy----.z-----.'),
  600. # I=6, O=3 (fixed-length input > fixed-length output)
  601. (b'i.o3.i6.abcdefghijklmnop', True, 'abc.ghi.mno.'),
  602. # I=0, then 3; O=29, then 15 (with longer output)
  603. (b'i.o29.a.b.cde.o15.abcdefghijabcdefghij.i3.a.b.c.d.ei00k.l.m', True,
  604. 'a----------------------------.' +
  605. 'b----------------------------.' +
  606. 'cde--------------------------.' +
  607. 'abcdefghijabcde.' +
  608. 'a.b------------.' +
  609. '.c.------------.' +
  610. 'd.e------------.' +
  611. 'k--------------.' +
  612. 'l--------------.' +
  613. 'm--------------.')
  614. ]
  615. def testDecoder(self):
  616. # Try a few one-shot test cases.
  617. for input, eof, output in self.test_cases:
  618. d = StatefulIncrementalDecoder()
  619. self.assertEquals(d.decode(input, eof), output)
  620. # Also test an unfinished decode, followed by forcing EOF.
  621. d = StatefulIncrementalDecoder()
  622. self.assertEquals(d.decode(b'oiabcd'), '')
  623. self.assertEquals(d.decode(b'', 1), 'abcd.')
  624. class TextIOWrapperTest(unittest.TestCase):
  625. def setUp(self):
  626. self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n"
  627. self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii")
  628. def tearDown(self):
  629. test_support.unlink(test_support.TESTFN)
  630. def testLineBuffering(self):
  631. r = io.BytesIO()
  632. b = io.BufferedWriter(r, 1000)
  633. t = io.TextIOWrapper(b, newline="\n", line_buffering=True)
  634. t.write(u"X")
  635. self.assertEquals(r.getvalue(), b"") # No flush happened
  636. t.write(u"Y\nZ")
  637. self.assertEquals(r.getvalue(), b"XY\nZ") # All got flushed
  638. t.write(u"A\rB")
  639. self.assertEquals(r.getvalue(), b"XY\nZA\rB")
  640. def testEncodingErrorsReading(self):
  641. # (1) default
  642. b = io.BytesIO(b"abc\n\xff\n")
  643. t = io.TextIOWrapper(b, encoding="ascii")
  644. self.assertRaises(UnicodeError, t.read)
  645. # (2) explicit strict
  646. b = io.BytesIO(b"abc\n\xff\n")
  647. t = io.TextIOWrapper(b, encoding="ascii", errors="strict")
  648. self.assertRaises(UnicodeError, t.read)
  649. # (3) ignore
  650. b = io.BytesIO(b"abc\n\xff\n")
  651. t = io.TextIOWrapper(b, encoding="ascii", errors="ignore")
  652. self.assertEquals(t.read(), "abc\n\n")
  653. # (4) replace
  654. b = io.BytesIO(b"abc\n\xff\n")
  655. t = io.TextIOWrapper(b, encoding="ascii", errors="replace")
  656. self.assertEquals(t.read(), u"abc\n\ufffd\n")
  657. def testEncodingErrorsWriting(self):
  658. # (1) default
  659. b = io.BytesIO()
  660. t = io.TextIOWrapper(b, encoding="ascii")
  661. self.assertRaises(UnicodeError, t.write, u"\xff")
  662. # (2) explicit strict
  663. b = io.BytesIO()
  664. t = io.TextIOWrapper(b, encoding="ascii", errors="strict")
  665. self.assertRaises(UnicodeError, t.write, u"\xff")
  666. # (3) ignore
  667. b = io.BytesIO()
  668. t = io.TextIOWrapper(b, encoding="ascii", errors="ignore",
  669. newline="\n")
  670. t.write(u"abc\xffdef\n")
  671. t.flush()
  672. self.assertEquals(b.getvalue(), b"abcdef\n")
  673. # (4) replace
  674. b = io.BytesIO()
  675. t = io.TextIOWrapper(b, encoding="ascii", errors="replace",
  676. newline="\n")
  677. t.write(u"abc\xffdef\n")
  678. t.flush()
  679. self.assertEquals(b.getvalue(), b"abc?def\n")
  680. def testNewlinesInput(self):
  681. testdata = b"AAA\nBBB\nCCC\rDDD\rEEE\r\nFFF\r\nGGG"
  682. normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
  683. for newline, expected in [
  684. (None, normalized.decode("ascii").splitlines(True)),
  685. ("", testdata.decode("ascii").splitlines(True)),
  686. ("\n", ["AAA\n", "BBB\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]),
  687. ("\r\n", ["AAA\nBBB\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]),
  688. ("\r", ["AAA\nBBB\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]),
  689. ]:
  690. buf = io.BytesIO(testdata)
  691. txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline)
  692. self.assertEquals(txt.readlines(), expected)
  693. txt.seek(0)
  694. self.assertEquals(txt.read(), "".join(expected))
  695. def testNewlinesOutput(self):
  696. testdict = {
  697. "": b"AAA\nBBB\nCCC\nX\rY\r\nZ",
  698. "\n": b"AAA\nBBB\nCCC\nX\rY\r\nZ",
  699. "\r": b"AAA\rBBB\rCCC\rX\rY\r\rZ",
  700. "\r\n": b"AAA\r\nBBB\r\nCCC\r\nX\rY\r\r\nZ",
  701. }
  702. tests = [(None, testdict[os.linesep])] + sorted(testdict.items())
  703. for newline, expected in tests:
  704. buf = io.BytesIO()
  705. txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline)
  706. txt.write("AAA\nB")
  707. txt.write("BB\nCCC\n")
  708. txt.write("X\rY\r\nZ")
  709. txt.flush()
  710. self.assertEquals(buf.closed, False)
  711. self.assertEquals(buf.getvalue(), expected)
  712. def testNewlines(self):
  713. input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ]
  714. tests = [
  715. [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ],
  716. [ '', input_lines ],
  717. [ '\n', [ "unix\n", "windows\r\n", "os9\rlast\n", "nonl" ] ],
  718. [ '\r\n', [ "unix\nwindows\r\n", "os9\rlast\nnonl" ] ],
  719. [ '\r', [ "unix\nwindows\r", "\nos9\r", "last\nnonl" ] ],
  720. ]
  721. encodings = (
  722. 'utf-8', 'latin-1',
  723. 'utf-16', 'utf-16-le', 'utf-16-be',
  724. 'utf-32', 'utf-32-le', 'utf-32-be',
  725. )
  726. # Try a range of buffer sizes to test the case where \r is the last
  727. # character in TextIOWrapper._pending_line.
  728. for encoding in encodings:
  729. # XXX: str.encode() should return bytes
  730. data = bytes(''.join(input_lines).encode(encoding))
  731. for do_reads in (False, True):
  732. for bufsize in range(1, 10):
  733. for newline, exp_lines in tests:
  734. bufio = io.BufferedReader(io.BytesIO(data), bufsize)
  735. textio = io.TextIOWrapper(bufio, newline=newline,
  736. encoding=encoding)
  737. if do_reads:
  738. got_lines = []
  739. while True:
  740. c2 = textio.read(2)
  741. if c2 == '':
  742. break
  743. self.assertEquals(len(c2), 2)
  744. got_lines.append(c2 + textio.readline())
  745. else:
  746. got_lines = list(textio)
  747. for got_line, exp_line in zip(got_lines, exp_lines):
  748. self.assertEquals(got_line, exp_line)
  749. self.assertEquals(len(got_lines), len(exp_lines))
  750. def testNewlinesInput(self):
  751. testdata = b"AAA\nBBB\nCCC\rDDD\rEEE\r\nFFF\r\nGGG"
  752. normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
  753. for newline, expected in [
  754. (None, normalized.decode("ascii").splitlines(True)),
  755. ("", testdata.decode("ascii").splitlines(True)),
  756. ("\n", ["AAA\n", "BBB\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]),
  757. ("\r\n", ["AAA\nBBB\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]),
  758. ("\r", ["AAA\nBBB\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]),
  759. ]:
  760. buf = io.BytesIO(testdata)
  761. txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline)
  762. self.assertEquals(txt.readlines(), expected)
  763. txt.seek(0)
  764. self.assertEquals(txt.read(), "".join(expected))
  765. def testNewlinesOutput(self):
  766. data = u"AAA\nBBB\rCCC\n"
  767. data_lf = b"AAA\nBBB\rCCC\n"
  768. data_cr = b"AAA\rBBB\rCCC\r"
  769. data_crlf = b"AAA\r\nBBB\rCCC\r\n"
  770. save_linesep = os.linesep
  771. try:
  772. for os.linesep, newline, expected in [
  773. ("\n", None, data_lf),
  774. ("\r\n", None, data_crlf),
  775. ("\n", "", data_lf),
  776. ("\r\n", "", data_lf),
  777. ("\n", "\n", data_lf),
  778. ("\r\n", "\n", data_lf),
  779. ("\n", "\r", data_cr),
  780. ("\r\n", "\r", data_cr),
  781. ("\n", "\r\n", data_crlf),
  782. ("\r\n", "\r\n", data_crlf),
  783. ]:
  784. buf = io.BytesIO()
  785. txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline)
  786. txt.write(data)
  787. txt.close()
  788. self.assertEquals(buf.closed, True)
  789. self.assertRaises(ValueError, buf.getvalue)
  790. finally:
  791. os.linesep = save_linesep
  792. # Systematic tests of the text I/O API
  793. def testBasicIO(self):
  794. for chunksize in (1, 2, 3, 4, 5, 15, 16, 17, 31, 32, 33, 63, 64, 65):
  795. for enc in "ascii", "latin1", "utf8" :# , "utf-16-be", "utf-16-le":
  796. f = io.open(test_support.TESTFN, "w+", encoding=enc)
  797. f._CHUNK_SIZE = chunksize
  798. self.assertEquals(f.write(u"abc"), 3)
  799. f.close()
  800. f = io.open(test_support.TESTFN, "r+", encoding=enc)
  801. f._CHUNK_SIZE = chunksize
  802. self.assertEquals(f.tell(), 0)
  803. self.assertEquals(f.read(), u"abc")
  804. cookie = f.tell()
  805. self.assertEquals(f.seek(0), 0)
  806. self.assertEquals(f.read(2), u"ab")
  807. self.assertEquals(f.read(1), u"c")
  808. self.assertEquals(f.read(1), u"")
  809. self.assertEquals(f.read(), u"")
  810. self.assertEquals(f.tell(), cookie)
  811. self.assertEquals(f.seek(0), 0)
  812. self.assertEquals(f.seek(0, 2), cookie)
  813. self.assertEquals(f.write(u"def"), 3)
  814. self.assertEquals(f.seek(cookie), cookie)
  815. self.assertEquals(f.read(), u"def")
  816. if enc.startswith("utf"):
  817. self.multi_line_test(f, enc)
  818. f.close()
  819. def multi_line_test(self, f, enc):
  820. f.seek(0)
  821. f.truncate()
  822. sample = u"s\xff\u0fff\uffff"
  823. wlines = []
  824. for size in (0, 1, 2, 3, 4, 5, 30, 31, 32, 33, 62, 63, 64, 65, 1000):
  825. chars = []
  826. for i in range(size):
  827. chars.append(sample[i % len(sample)])
  828. line = u"".join(chars) + u"\n"
  829. wlines.append((f.tell(), line))
  830. f.write(line)
  831. f.seek(0)
  832. rlines = []
  833. while True:
  834. pos = f.tell()
  835. line = f.readline()
  836. if not line:
  837. break
  838. rlines.append((pos, line))
  839. self.assertEquals(rlines, wlines)
  840. def testTelling(self):
  841. f = io.open(test_support.TESTFN, "w+", encoding="utf8")
  842. p0 = f.tell()
  843. f.write(u"\xff\n")
  844. p1 = f.tell()
  845. f.write(u"\xff\n")
  846. p2 = f.tell()
  847. f.seek(0)
  848. self.assertEquals(f.tell(), p0)
  849. self.assertEquals(f.readline(), u"\xff\n")
  850. self.assertEquals(f.tell(), p1)
  851. self.assertEquals(f.readline(), u"\xff\n")
  852. self.assertEquals(f.tell(), p2)
  853. f.seek(0)
  854. for line in f:
  855. self.assertEquals(line, u"\xff\n")
  856. self.assertRaises(IOError, f.tell)
  857. self.assertEquals(f.tell(), p2)
  858. f.close()
  859. def testSeeking(self):
  860. chunk_size = io.TextIOWrapper._CHUNK_SIZE
  861. prefix_size = chunk_size - 2
  862. u_prefix = "a" * prefix_size
  863. prefix = bytes(u_prefix.encode("utf-8"))
  864. self.assertEquals(len(u_prefix), len(prefix))
  865. u_suffix = "\u8888\n"
  866. suffix = bytes(u_suffix.encode("utf-8"))
  867. line = prefix + suffix
  868. f = io.open(test_support.TESTFN, "wb")
  869. f.write(line*2)
  870. f.close()
  871. f = io.open(test_support.TESTFN, "r", encoding="utf-8")
  872. s = f.read(prefix_size)
  873. self.assertEquals(s, unicode(prefix, "ascii"))
  874. self.assertEquals(f.tell(), prefix_size)
  875. self.assertEquals(f.readline(), u_suffix)
  876. def testSeekingToo(self):
  877. # Regression test for a specific bug
  878. data = b'\xe0\xbf\xbf\n'
  879. f = io.open(test_support.TESTFN, "wb")
  880. f.write(data)
  881. f.close()
  882. f = io.open(test_support.TESTFN, "r", encoding="utf-8")
  883. f._CHUNK_SIZE # Just test that it exists
  884. f._CHUNK_SIZE = 2
  885. f.readline()
  886. f.tell()
  887. def testSeekAndTell(self):
  888. """Test seek/tell using the StatefulIncrementalDecoder."""
  889. def testSeekAndTellWithData(data, min_pos=0):
  890. """Tell/seek to various points within a data stream and ensure
  891. that the decoded data returned by read() is consistent."""
  892. f = io.open(test_support.TESTFN, 'wb')
  893. f.write(data)
  894. f.close()
  895. f = io.open(test_support.TESTFN, encoding='test_decoder')
  896. decoded = f.read()
  897. f.close()
  898. for i in range(min_pos, len(decoded) + 1): # seek positions
  899. for j in [1, 5, len(decoded) - i]: # read lengths
  900. f = io.open(test_support.TESTFN, encoding='test_decoder')
  901. self.assertEquals(f.read(i), decoded[:i])
  902. cookie = f.tell()
  903. self.assertEquals(f.read(j), decoded[i:i + j])
  904. f.seek(cookie)
  905. self.assertEquals(f.read(), decoded[i:])
  906. f.close()
  907. # Enable the test decoder.
  908. StatefulIncrementalDecoder.codecEnabled = 1
  909. # Run the tests.
  910. try:
  911. # Try each test case.
  912. for input, _, _ in StatefulIncrementalDecoderTest.test_cases:
  913. testSeekAndTellWithData(input)
  914. # Position each test case so that it crosses a chunk boundary.
  915. CHUNK_SIZE = io.TextIOWrapper._CHUNK_SIZE
  916. for input, _, _ in StatefulIncrementalDecoderTest.test_cases:
  917. offset = CHUNK_SIZE - len(input)//2
  918. prefix = b'.'*offset
  919. # Don't bother seeking into the prefix (takes too long).
  920. min_pos = offset*2
  921. testSeekAndTellWithData(prefix + input, min_pos)
  922. # Ensure our test decoder won't interfere with subsequent tests.
  923. finally:
  924. StatefulIncrementalDecoder.codecEnabled = 0
  925. def testEncodedWrites(self):
  926. data = u"1234567890"
  927. tests = ("utf-16",
  928. "utf-16-le",
  929. "utf-16-be",
  930. "utf-32",
  931. "utf-32-le",
  932. "utf-32-be")
  933. for encoding in tests:
  934. buf = io.BytesIO()
  935. f = io.TextIOWrapper(buf, encoding=encoding)
  936. # Check if the BOM is written only once (see issue1753).
  937. f.write(data)
  938. f.write(data)
  939. f.seek(0)
  940. self.assertEquals(f.read(), data * 2)
  941. self.assertEquals(buf.getvalue(), (data * 2).encode(encoding))
  942. def timingTest(self):
  943. timer = time.time
  944. enc = "utf8"
  945. line = "\0\x0f\xff\u0fff\uffff\U000fffff\U0010ffff"*3 + "\n"
  946. nlines = 10000
  947. nchars = len(line)
  948. nbytes = len(line.encode(enc))
  949. for chunk_size in (32, 64, 128, 256):
  950. f = io.open(test_support.TESTFN, "w+", encoding=enc)
  951. f._CHUNK_SIZE = chunk_size
  952. t0 = timer()
  953. for i in range(nlines):
  954. f.write(line)
  955. f.flush()
  956. t1 = timer()
  957. f.seek(0)
  958. for line in f:
  959. pass
  960. t2 = timer()
  961. f.seek(0)
  962. while f.readline():
  963. pass
  964. t3 = timer()
  965. f.seek(0)
  966. while f.readline():
  967. f.tell()
  968. t4 = timer()
  969. f.close()
  970. if test_support.verbose:
  971. print("\nTiming test: %d lines of %d characters (%d bytes)" %
  972. (nlines, nchars, nbytes))
  973. print("File chunk size: %6s" % f._CHUNK_SIZE)
  974. print("Writing: %6.3f seconds" % (t1-t0))
  975. print("Reading using iteration: %6.3f seconds" % (t2-t1))
  976. print("Reading using readline(): %6.3f seconds" % (t3-t2))
  977. print("Using readline()+tell(): %6.3f seconds" % (t4-t3))
  978. def testReadOneByOne(self):
  979. txt = io.TextIOWrapper(io.BytesIO(b"AA\r\nBB"))
  980. reads = ""
  981. while True:
  982. c = txt.read(1)
  983. if not c:
  984. break
  985. reads += c
  986. self.assertEquals(reads, "AA\nBB")
  987. # read in amounts equal to TextIOWrapper._CHUNK_SIZE which is 128.
  988. def testReadByChunk(self):
  989. # make sure "\r\n" straddles 128 char boundary.
  990. txt = io.TextIOWrapper(io.BytesIO(b"A" * 127 + b"\r\nB"))
  991. reads = ""
  992. while True:
  993. c = txt.read(128)
  994. if not c:
  995. break
  996. reads += c
  997. self.assertEquals(reads, "A"*127+"\nB")
  998. def test_issue1395_1(self):
  999. txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii")
  1000. # read one char at a time
  1001. reads = ""
  1002. while True:
  1003. c = txt.read(1)
  1004. if not c:
  1005. break
  1006. reads += c
  1007. self.assertEquals(reads, self.normalized)
  1008. def test_issue1395_2(self):
  1009. txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii")
  1010. txt._CHUNK_SIZE = 4
  1011. reads = ""
  1012. while True:
  1013. c = txt.read(4)
  1014. if not c:
  1015. break
  1016. reads += c
  1017. self.assertEquals(reads, self.normalized)
  1018. def test_issue1395_3(self):
  1019. txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii")
  1020. txt._CHUNK_SIZE = 4
  1021. reads = txt.read(4)
  1022. reads += txt.read(4)
  1023. reads += txt.readline()
  1024. reads += txt.readline()
  1025. reads += txt.readline()
  1026. self.assertEquals(reads, self.normalized)
  1027. def test_issue1395_4(self):
  1028. txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii")
  1029. txt._CHUNK_SIZE = 4
  1030. reads = txt.read(4)
  1031. reads += txt.read()
  1032. self.assertEquals(reads, self.normalized)
  1033. def test_issue1395_5(self):
  1034. txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii")
  1035. txt._CHUNK_SIZE = 4
  1036. reads = txt.read(4)
  1037. pos = txt.tell()
  1038. txt.seek(0)
  1039. txt.seek(pos)
  1040. self.assertEquals(txt.read(4), "BBB\n")
  1041. def test_issue2282(self):
  1042. buffer = io.BytesIO(self.testdata)
  1043. txt = io.TextIOWrapper(buffer, encoding="ascii")
  1044. self.assertEqual(buffer.seekable(), txt.seekable())
  1045. def check_newline_decoder_utf8(self, decoder):
  1046. # UTF-8 specific tests for a newline decoder
  1047. def _check_decode(b, s, **kwargs):
  1048. # We exercise getstate() / setstate() as well as decode()
  1049. state = decoder.getstate()
  1050. self.assertEquals(decoder.decode(b, **kwargs), s)
  1051. decoder.setstate(state)
  1052. self.assertEquals(decoder.decode(b, **kwargs), s)
  1053. _check_decode(b'\xe8\xa2\x88', "\u8888")
  1054. _check_decode(b'\xe8', "")
  1055. _check_decode(b'\xa2', "")
  1056. _check_decode(b'\x88', "\u8888")
  1057. _check_decode(b'\xe8', "")
  1058. _check_decode(b'\xa2', "")
  1059. _check_decode(b'\x88', "\u8888")
  1060. _check_decode(b'\xe8', "")
  1061. self.assertRaises(UnicodeDecodeError, decoder.decode, b'', final=True)
  1062. decoder.reset()
  1063. _check_decode(b'\n', "\n")
  1064. _check_decode(b'\r', "")
  1065. _check_decode(b'', "\n", final=True)
  1066. _check_decode(b'\r', "\n", final=True)
  1067. _check_decode(b'\r', "")
  1068. _check_decode(b'a', "\na")
  1069. _check_decode(b'\r\r\n', "\n\n")
  1070. _check_decode(b'\r', "")
  1071. _check_decode(b'\r', "\n")
  1072. _check_decode(b'\na', "\na")
  1073. _check_decode(b'\xe8\xa2\x88\r\n', "\u8888\n")
  1074. _check_decode(b'\xe8\xa2\x88', "\u8888")
  1075. _check_decode(b'\n', "\n")
  1076. _check_decode(b'\xe8\xa2\x88\r', "\u8888")
  1077. _check_decode(b'\n', "\n")
  1078. def check_newline_decoder(self, decoder, encoding):
  1079. result = []
  1080. encoder = codecs.getincrementalencoder(encoding)()
  1081. def _decode_bytewise(s):
  1082. for b in encoder.encode(s):
  1083. result.append(decoder.decode(b))
  1084. self.assertEquals(decoder.newlines, None)
  1085. _decode_bytewise("abc\n\r")
  1086. self.assertEquals(decoder.newlines, '\n')
  1087. _decode_bytewise("\nabc")
  1088. self.assertEquals(decoder.newlines, ('\n', '\r\n'))
  1089. _decode_bytewise("abc\r")
  1090. self.assertEquals(decoder.newlines, ('\n', '\r\n'))
  1091. _decode_bytewise("abc")
  1092. self.assertEquals(decoder.newlines, ('\r', '\n', '\r\n'))
  1093. _decode_bytewise("abc\r")
  1094. self.assertEquals("".join(result), "abc\n\nabcabc\nabcabc")
  1095. decoder.reset()
  1096. self.assertEquals(decoder.decode("abc".encode(encoding)), "abc")
  1097. self.assertEquals(decoder.newlines, None)
  1098. def test_newline_decoder(self):
  1099. encodings = (
  1100. 'utf-8', 'latin-1',
  1101. 'utf-16', 'utf-16-le', 'utf-16-be',
  1102. 'utf-32', 'utf-32-le', 'utf-32-be',
  1103. )
  1104. for enc in encodings:
  1105. decoder = codecs.getincrementaldecoder(enc)()
  1106. decoder = io.IncrementalNewlineDecoder(decoder, translate=True)
  1107. self.check_newline_decoder(decoder, enc)
  1108. decoder = codecs.getincrementaldecoder("utf-8")()
  1109. decoder = io.IncrementalNewlineDecoder(decoder, translate=True)
  1110. self.check_newline_decoder_utf8(decoder)
  1111. # XXX Tests for open()
  1112. class MiscIOTest(unittest.TestCase):
  1113. def tearDown(self):
  1114. test_support.unlink(test_support.TESTFN)
  1115. def testImport__all__(self):
  1116. for name in io.__all__:
  1117. obj = getattr(io, name, None)
  1118. self.assert_(obj is not None, name)
  1119. if name == "open":
  1120. continue
  1121. elif "error" in name.lower():
  1122. self.assert_(issubclass(obj, Exception), name)
  1123. else:
  1124. self.assert_(issubclass(obj, io.IOBase))
  1125. def test_attributes(self):
  1126. f = io.open(test_support.TESTFN, "wb", buffering=0)
  1127. self.assertEquals(f.mode, "wb")
  1128. f.close()
  1129. f = io.open(test_support.TESTFN, "U")
  1130. self.assertEquals(f.name, test_support.TESTFN)
  1131. self.assertEquals(f.buffer.name, test_support.TESTFN)
  1132. self.assertEquals(f.buffer.raw.name, test_support.TESTFN)
  1133. self.assertEquals(f.mode, "U")
  1134. self.assertEquals(f.buffer.mode, "rb")
  1135. self.assertEquals(f.buffer.raw.mode, "rb")
  1136. f.close()
  1137. f = io.open(test_support.TESTFN, "w+")
  1138. self.assertEquals(f.mode, "w+")
  1139. self.assertEquals(f.buffer.mode, "rb+") # Does it really matter?
  1140. self.assertEquals(f.buffer.raw.mode, "rb+")
  1141. g = io.open(f.fileno(), "wb", closefd=False)
  1142. self.assertEquals(g.mode, "wb")
  1143. self.assertEquals(g.raw.mode, "wb")
  1144. self.assertEquals(g.name, f.fileno())
  1145. self.assertEquals(g.raw.name, f.fileno())
  1146. f.close()
  1147. g.close()
  1148. def test_main():
  1149. test_support.run_unittest(IOTest, BytesIOTest, StringIOTest,
  1150. BufferedReaderTest, BufferedWriterTest,
  1151. BufferedRWPairTest, BufferedRandomTest,
  1152. StatefulIncrementalDecoderTest,
  1153. TextIOWrapperTest, MiscIOTest)
  1154. if __name__ == "__main__":
  1155. unittest.main()