PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-python/2.7/test/test_fileinput.py

https://bitbucket.org/evelyn559/pypy
Python | 225 lines | 200 code | 14 blank | 11 comment | 25 complexity | 35befeca0aab6556c1231de660f1d6a0 MD5 | raw file
  1. '''
  2. Tests for fileinput module.
  3. Nick Mathewson
  4. '''
  5. import unittest
  6. from test.test_support import verbose, TESTFN, run_unittest
  7. from test.test_support import unlink as safe_unlink
  8. import sys, re
  9. from StringIO import StringIO
  10. from fileinput import FileInput, hook_encoded
  11. # The fileinput module has 2 interfaces: the FileInput class which does
  12. # all the work, and a few functions (input, etc.) that use a global _state
  13. # variable. We only test the FileInput class, since the other functions
  14. # only provide a thin facade over FileInput.
  15. # Write lines (a list of lines) to temp file number i, and return the
  16. # temp file's name.
  17. def writeTmp(i, lines, mode='w'): # opening in text mode is the default
  18. name = TESTFN + str(i)
  19. f = open(name, mode)
  20. f.writelines(lines)
  21. f.close()
  22. return name
  23. def remove_tempfiles(*names):
  24. for name in names:
  25. safe_unlink(name)
  26. class BufferSizesTests(unittest.TestCase):
  27. def test_buffer_sizes(self):
  28. # First, run the tests with default and teeny buffer size.
  29. for round, bs in (0, 0), (1, 30):
  30. try:
  31. t1 = writeTmp(1, ["Line %s of file 1\n" % (i+1) for i in range(15)])
  32. t2 = writeTmp(2, ["Line %s of file 2\n" % (i+1) for i in range(10)])
  33. t3 = writeTmp(3, ["Line %s of file 3\n" % (i+1) for i in range(5)])
  34. t4 = writeTmp(4, ["Line %s of file 4\n" % (i+1) for i in range(1)])
  35. self.buffer_size_test(t1, t2, t3, t4, bs, round)
  36. finally:
  37. remove_tempfiles(t1, t2, t3, t4)
  38. def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
  39. pat = re.compile(r'LINE (\d+) OF FILE (\d+)')
  40. start = 1 + round*6
  41. if verbose:
  42. print '%s. Simple iteration (bs=%s)' % (start+0, bs)
  43. fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
  44. lines = list(fi)
  45. fi.close()
  46. self.assertEqual(len(lines), 31)
  47. self.assertEqual(lines[4], 'Line 5 of file 1\n')
  48. self.assertEqual(lines[30], 'Line 1 of file 4\n')
  49. self.assertEqual(fi.lineno(), 31)
  50. self.assertEqual(fi.filename(), t4)
  51. if verbose:
  52. print '%s. Status variables (bs=%s)' % (start+1, bs)
  53. fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
  54. s = "x"
  55. while s and s != 'Line 6 of file 2\n':
  56. s = fi.readline()
  57. self.assertEqual(fi.filename(), t2)
  58. self.assertEqual(fi.lineno(), 21)
  59. self.assertEqual(fi.filelineno(), 6)
  60. self.assertFalse(fi.isfirstline())
  61. self.assertFalse(fi.isstdin())
  62. if verbose:
  63. print '%s. Nextfile (bs=%s)' % (start+2, bs)
  64. fi.nextfile()
  65. self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
  66. self.assertEqual(fi.lineno(), 22)
  67. fi.close()
  68. if verbose:
  69. print '%s. Stdin (bs=%s)' % (start+3, bs)
  70. fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
  71. savestdin = sys.stdin
  72. try:
  73. sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
  74. lines = list(fi)
  75. self.assertEqual(len(lines), 33)
  76. self.assertEqual(lines[32], 'Line 2 of stdin\n')
  77. self.assertEqual(fi.filename(), '<stdin>')
  78. fi.nextfile()
  79. finally:
  80. sys.stdin = savestdin
  81. if verbose:
  82. print '%s. Boundary conditions (bs=%s)' % (start+4, bs)
  83. fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
  84. self.assertEqual(fi.lineno(), 0)
  85. self.assertEqual(fi.filename(), None)
  86. fi.nextfile()
  87. self.assertEqual(fi.lineno(), 0)
  88. self.assertEqual(fi.filename(), None)
  89. if verbose:
  90. print '%s. Inplace (bs=%s)' % (start+5, bs)
  91. savestdout = sys.stdout
  92. try:
  93. fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
  94. for line in fi:
  95. line = line[:-1].upper()
  96. print line
  97. fi.close()
  98. finally:
  99. sys.stdout = savestdout
  100. fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
  101. for line in fi:
  102. self.assertEqual(line[-1], '\n')
  103. m = pat.match(line[:-1])
  104. self.assertNotEqual(m, None)
  105. self.assertEqual(int(m.group(1)), fi.filelineno())
  106. fi.close()
  107. class FileInputTests(unittest.TestCase):
  108. def test_zero_byte_files(self):
  109. try:
  110. t1 = writeTmp(1, [""])
  111. t2 = writeTmp(2, [""])
  112. t3 = writeTmp(3, ["The only line there is.\n"])
  113. t4 = writeTmp(4, [""])
  114. fi = FileInput(files=(t1, t2, t3, t4))
  115. line = fi.readline()
  116. self.assertEqual(line, 'The only line there is.\n')
  117. self.assertEqual(fi.lineno(), 1)
  118. self.assertEqual(fi.filelineno(), 1)
  119. self.assertEqual(fi.filename(), t3)
  120. line = fi.readline()
  121. self.assertFalse(line)
  122. self.assertEqual(fi.lineno(), 1)
  123. self.assertEqual(fi.filelineno(), 0)
  124. self.assertEqual(fi.filename(), t4)
  125. fi.close()
  126. finally:
  127. remove_tempfiles(t1, t2, t3, t4)
  128. def test_files_that_dont_end_with_newline(self):
  129. try:
  130. t1 = writeTmp(1, ["A\nB\nC"])
  131. t2 = writeTmp(2, ["D\nE\nF"])
  132. fi = FileInput(files=(t1, t2))
  133. lines = list(fi)
  134. self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
  135. self.assertEqual(fi.filelineno(), 3)
  136. self.assertEqual(fi.lineno(), 6)
  137. finally:
  138. remove_tempfiles(t1, t2)
  139. def test_unicode_filenames(self):
  140. try:
  141. t1 = writeTmp(1, ["A\nB"])
  142. encoding = sys.getfilesystemencoding()
  143. if encoding is None:
  144. encoding = 'ascii'
  145. fi = FileInput(files=unicode(t1, encoding))
  146. lines = list(fi)
  147. self.assertEqual(lines, ["A\n", "B"])
  148. finally:
  149. remove_tempfiles(t1)
  150. def test_fileno(self):
  151. try:
  152. t1 = writeTmp(1, ["A\nB"])
  153. t2 = writeTmp(2, ["C\nD"])
  154. fi = FileInput(files=(t1, t2))
  155. self.assertEqual(fi.fileno(), -1)
  156. line = fi.next()
  157. self.assertNotEqual(fi.fileno(), -1)
  158. fi.nextfile()
  159. self.assertEqual(fi.fileno(), -1)
  160. line = list(fi)
  161. self.assertEqual(fi.fileno(), -1)
  162. finally:
  163. remove_tempfiles(t1, t2)
  164. def test_opening_mode(self):
  165. try:
  166. # invalid mode, should raise ValueError
  167. fi = FileInput(mode="w")
  168. self.fail("FileInput should reject invalid mode argument")
  169. except ValueError:
  170. pass
  171. try:
  172. # try opening in universal newline mode
  173. t1 = writeTmp(1, ["A\nB\r\nC\rD"], mode="wb")
  174. fi = FileInput(files=t1, mode="U")
  175. lines = list(fi)
  176. self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
  177. finally:
  178. remove_tempfiles(t1)
  179. def test_file_opening_hook(self):
  180. try:
  181. # cannot use openhook and inplace mode
  182. fi = FileInput(inplace=1, openhook=lambda f,m: None)
  183. self.fail("FileInput should raise if both inplace "
  184. "and openhook arguments are given")
  185. except ValueError:
  186. pass
  187. try:
  188. fi = FileInput(openhook=1)
  189. self.fail("FileInput should check openhook for being callable")
  190. except ValueError:
  191. pass
  192. try:
  193. t1 = writeTmp(1, ["A\nB"], mode="wb")
  194. fi = FileInput(files=t1, openhook=hook_encoded("rot13"))
  195. lines = list(fi)
  196. self.assertEqual(lines, ["N\n", "O"])
  197. finally:
  198. remove_tempfiles(t1)
  199. def test_main():
  200. run_unittest(BufferSizesTests, FileInputTests)
  201. if __name__ == "__main__":
  202. test_main()