/Lib/test/test_file.py

http://unladen-swallow.googlecode.com/ · Python · 575 lines · 439 code · 75 blank · 61 comment · 95 complexity · a27ff241ac70e20440244c5c91752b85 MD5 · raw file

  1. import sys
  2. import os
  3. import unittest
  4. import itertools
  5. import time
  6. import threading
  7. from array import array
  8. from weakref import proxy
  9. from test import test_support
  10. from test.test_support import TESTFN, findfile, run_unittest
  11. from UserList import UserList
  12. class AutoFileTests(unittest.TestCase):
  13. # file tests for which a test file is automatically set up
  14. def setUp(self):
  15. self.f = open(TESTFN, 'wb')
  16. def tearDown(self):
  17. if self.f:
  18. self.f.close()
  19. os.remove(TESTFN)
  20. def testWeakRefs(self):
  21. # verify weak references
  22. p = proxy(self.f)
  23. p.write('teststring')
  24. self.assertEquals(self.f.tell(), p.tell())
  25. self.f.close()
  26. self.f = None
  27. self.assertRaises(ReferenceError, getattr, p, 'tell')
  28. def testAttributes(self):
  29. # verify expected attributes exist
  30. f = self.f
  31. softspace = f.softspace
  32. f.name # merely shouldn't blow up
  33. f.mode # ditto
  34. f.closed # ditto
  35. # verify softspace is writable
  36. f.softspace = softspace # merely shouldn't blow up
  37. # verify the others aren't
  38. for attr in 'name', 'mode', 'closed':
  39. self.assertRaises((AttributeError, TypeError), setattr, f, attr, 'oops')
  40. def testReadinto(self):
  41. # verify readinto
  42. self.f.write('12')
  43. self.f.close()
  44. a = array('c', 'x'*10)
  45. self.f = open(TESTFN, 'rb')
  46. n = self.f.readinto(a)
  47. self.assertEquals('12', a.tostring()[:n])
  48. def testWritelinesUserList(self):
  49. # verify writelines with instance sequence
  50. l = UserList(['1', '2'])
  51. self.f.writelines(l)
  52. self.f.close()
  53. self.f = open(TESTFN, 'rb')
  54. buf = self.f.read()
  55. self.assertEquals(buf, '12')
  56. def testWritelinesIntegers(self):
  57. # verify writelines with integers
  58. self.assertRaises(TypeError, self.f.writelines, [1, 2, 3])
  59. def testWritelinesIntegersUserList(self):
  60. # verify writelines with integers in UserList
  61. l = UserList([1,2,3])
  62. self.assertRaises(TypeError, self.f.writelines, l)
  63. def testWritelinesNonString(self):
  64. # verify writelines with non-string object
  65. class NonString:
  66. pass
  67. self.assertRaises(TypeError, self.f.writelines,
  68. [NonString(), NonString()])
  69. def testRepr(self):
  70. # verify repr works
  71. self.assert_(repr(self.f).startswith("<open file '" + TESTFN))
  72. def testErrors(self):
  73. f = self.f
  74. self.assertEquals(f.name, TESTFN)
  75. self.assert_(not f.isatty())
  76. self.assert_(not f.closed)
  77. self.assertRaises(TypeError, f.readinto, "")
  78. f.close()
  79. self.assert_(f.closed)
  80. def testMethods(self):
  81. methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
  82. 'readline', 'readlines', 'seek', 'tell', 'truncate',
  83. 'write', 'xreadlines', '__iter__']
  84. if sys.platform.startswith('atheos'):
  85. methods.remove('truncate')
  86. # __exit__ should close the file
  87. self.f.__exit__(None, None, None)
  88. self.assert_(self.f.closed)
  89. for methodname in methods:
  90. method = getattr(self.f, methodname)
  91. # should raise on closed file
  92. self.assertRaises(ValueError, method)
  93. self.assertRaises(ValueError, self.f.writelines, [])
  94. # file is closed, __exit__ shouldn't do anything
  95. self.assertEquals(self.f.__exit__(None, None, None), None)
  96. # it must also return None if an exception was given
  97. try:
  98. 1/0
  99. except:
  100. self.assertEquals(self.f.__exit__(*sys.exc_info()), None)
  101. def testReadWhenWriting(self):
  102. self.assertRaises(IOError, self.f.read)
  103. class OtherFileTests(unittest.TestCase):
  104. def testOpenDir(self):
  105. this_dir = os.path.dirname(__file__)
  106. for mode in (None, "w"):
  107. try:
  108. if mode:
  109. f = open(this_dir, mode)
  110. else:
  111. f = open(this_dir)
  112. except IOError as e:
  113. self.assertEqual(e.filename, this_dir)
  114. else:
  115. self.fail("opening a directory didn't raise an IOError")
  116. def testModeStrings(self):
  117. # check invalid mode strings
  118. for mode in ("", "aU", "wU+"):
  119. try:
  120. f = open(TESTFN, mode)
  121. except ValueError:
  122. pass
  123. else:
  124. f.close()
  125. self.fail('%r is an invalid file mode' % mode)
  126. # Some invalid modes fail on Windows, but pass on Unix
  127. # Issue3965: avoid a crash on Windows when filename is unicode
  128. for name in (TESTFN, unicode(TESTFN), unicode(TESTFN + '\t')):
  129. try:
  130. f = open(name, "rr")
  131. except IOError:
  132. pass
  133. else:
  134. f.close()
  135. def testStdin(self):
  136. # This causes the interpreter to exit on OSF1 v5.1.
  137. if sys.platform != 'osf1V5':
  138. self.assertRaises(IOError, sys.stdin.seek, -1)
  139. else:
  140. print >>sys.__stdout__, (
  141. ' Skipping sys.stdin.seek(-1), it may crash the interpreter.'
  142. ' Test manually.')
  143. self.assertRaises(IOError, sys.stdin.truncate)
  144. def testUnicodeOpen(self):
  145. # verify repr works for unicode too
  146. f = open(unicode(TESTFN), "w")
  147. self.assert_(repr(f).startswith("<open file u'" + TESTFN))
  148. f.close()
  149. os.unlink(TESTFN)
  150. def testBadModeArgument(self):
  151. # verify that we get a sensible error message for bad mode argument
  152. bad_mode = "qwerty"
  153. try:
  154. f = open(TESTFN, bad_mode)
  155. except ValueError, msg:
  156. if msg[0] != 0:
  157. s = str(msg)
  158. if s.find(TESTFN) != -1 or s.find(bad_mode) == -1:
  159. self.fail("bad error message for invalid mode: %s" % s)
  160. # if msg[0] == 0, we're probably on Windows where there may be
  161. # no obvious way to discover why open() failed.
  162. else:
  163. f.close()
  164. self.fail("no error for invalid mode: %s" % bad_mode)
  165. def testSetBufferSize(self):
  166. # make sure that explicitly setting the buffer size doesn't cause
  167. # misbehaviour especially with repeated close() calls
  168. for s in (-1, 0, 1, 512):
  169. try:
  170. f = open(TESTFN, 'w', s)
  171. f.write(str(s))
  172. f.close()
  173. f.close()
  174. f = open(TESTFN, 'r', s)
  175. d = int(f.read())
  176. f.close()
  177. f.close()
  178. except IOError, msg:
  179. self.fail('error setting buffer size %d: %s' % (s, str(msg)))
  180. self.assertEquals(d, s)
  181. def testTruncateOnWindows(self):
  182. os.unlink(TESTFN)
  183. def bug801631():
  184. # SF bug <http://www.python.org/sf/801631>
  185. # "file.truncate fault on windows"
  186. f = open(TESTFN, 'wb')
  187. f.write('12345678901') # 11 bytes
  188. f.close()
  189. f = open(TESTFN,'rb+')
  190. data = f.read(5)
  191. if data != '12345':
  192. self.fail("Read on file opened for update failed %r" % data)
  193. if f.tell() != 5:
  194. self.fail("File pos after read wrong %d" % f.tell())
  195. f.truncate()
  196. if f.tell() != 5:
  197. self.fail("File pos after ftruncate wrong %d" % f.tell())
  198. f.close()
  199. size = os.path.getsize(TESTFN)
  200. if size != 5:
  201. self.fail("File size after ftruncate wrong %d" % size)
  202. try:
  203. bug801631()
  204. finally:
  205. os.unlink(TESTFN)
  206. def testIteration(self):
  207. # Test the complex interaction when mixing file-iteration and the
  208. # various read* methods. Ostensibly, the mixture could just be tested
  209. # to work when it should work according to the Python language,
  210. # instead of fail when it should fail according to the current CPython
  211. # implementation. People don't always program Python the way they
  212. # should, though, and the implemenation might change in subtle ways,
  213. # so we explicitly test for errors, too; the test will just have to
  214. # be updated when the implementation changes.
  215. dataoffset = 16384
  216. filler = "ham\n"
  217. assert not dataoffset % len(filler), \
  218. "dataoffset must be multiple of len(filler)"
  219. nchunks = dataoffset // len(filler)
  220. testlines = [
  221. "spam, spam and eggs\n",
  222. "eggs, spam, ham and spam\n",
  223. "saussages, spam, spam and eggs\n",
  224. "spam, ham, spam and eggs\n",
  225. "spam, spam, spam, spam, spam, ham, spam\n",
  226. "wonderful spaaaaaam.\n"
  227. ]
  228. methods = [("readline", ()), ("read", ()), ("readlines", ()),
  229. ("readinto", (array("c", " "*100),))]
  230. try:
  231. # Prepare the testfile
  232. bag = open(TESTFN, "w")
  233. bag.write(filler * nchunks)
  234. bag.writelines(testlines)
  235. bag.close()
  236. # Test for appropriate errors mixing read* and iteration
  237. for methodname, args in methods:
  238. f = open(TESTFN)
  239. if f.next() != filler:
  240. self.fail, "Broken testfile"
  241. meth = getattr(f, methodname)
  242. try:
  243. meth(*args)
  244. except ValueError:
  245. pass
  246. else:
  247. self.fail("%s%r after next() didn't raise ValueError" %
  248. (methodname, args))
  249. f.close()
  250. # Test to see if harmless (by accident) mixing of read* and
  251. # iteration still works. This depends on the size of the internal
  252. # iteration buffer (currently 8192,) but we can test it in a
  253. # flexible manner. Each line in the bag o' ham is 4 bytes
  254. # ("h", "a", "m", "\n"), so 4096 lines of that should get us
  255. # exactly on the buffer boundary for any power-of-2 buffersize
  256. # between 4 and 16384 (inclusive).
  257. f = open(TESTFN)
  258. for i in range(nchunks):
  259. f.next()
  260. testline = testlines.pop(0)
  261. try:
  262. line = f.readline()
  263. except ValueError:
  264. self.fail("readline() after next() with supposedly empty "
  265. "iteration-buffer failed anyway")
  266. if line != testline:
  267. self.fail("readline() after next() with empty buffer "
  268. "failed. Got %r, expected %r" % (line, testline))
  269. testline = testlines.pop(0)
  270. buf = array("c", "\x00" * len(testline))
  271. try:
  272. f.readinto(buf)
  273. except ValueError:
  274. self.fail("readinto() after next() with supposedly empty "
  275. "iteration-buffer failed anyway")
  276. line = buf.tostring()
  277. if line != testline:
  278. self.fail("readinto() after next() with empty buffer "
  279. "failed. Got %r, expected %r" % (line, testline))
  280. testline = testlines.pop(0)
  281. try:
  282. line = f.read(len(testline))
  283. except ValueError:
  284. self.fail("read() after next() with supposedly empty "
  285. "iteration-buffer failed anyway")
  286. if line != testline:
  287. self.fail("read() after next() with empty buffer "
  288. "failed. Got %r, expected %r" % (line, testline))
  289. try:
  290. lines = f.readlines()
  291. except ValueError:
  292. self.fail("readlines() after next() with supposedly empty "
  293. "iteration-buffer failed anyway")
  294. if lines != testlines:
  295. self.fail("readlines() after next() with empty buffer "
  296. "failed. Got %r, expected %r" % (line, testline))
  297. # Reading after iteration hit EOF shouldn't hurt either
  298. f = open(TESTFN)
  299. try:
  300. for line in f:
  301. pass
  302. try:
  303. f.readline()
  304. f.readinto(buf)
  305. f.read()
  306. f.readlines()
  307. except ValueError:
  308. self.fail("read* failed after next() consumed file")
  309. finally:
  310. f.close()
  311. finally:
  312. os.unlink(TESTFN)
  313. class FileSubclassTests(unittest.TestCase):
  314. def testExit(self):
  315. # test that exiting with context calls subclass' close
  316. class C(file):
  317. def __init__(self, *args):
  318. self.subclass_closed = False
  319. file.__init__(self, *args)
  320. def close(self):
  321. self.subclass_closed = True
  322. file.close(self)
  323. with C(TESTFN, 'w') as f:
  324. pass
  325. self.failUnless(f.subclass_closed)
  326. class FileThreadingTests(unittest.TestCase):
  327. # These tests check the ability to call various methods of file objects
  328. # (including close()) concurrently without crashing the Python interpreter.
  329. # See #815646, #595601
  330. def setUp(self):
  331. self.f = None
  332. self.filename = TESTFN
  333. with open(self.filename, "w") as f:
  334. f.write("\n".join("0123456789"))
  335. self._count_lock = threading.Lock()
  336. self.close_count = 0
  337. self.close_success_count = 0
  338. def tearDown(self):
  339. if self.f:
  340. try:
  341. self.f.close()
  342. except (EnvironmentError, ValueError):
  343. pass
  344. try:
  345. os.remove(self.filename)
  346. except EnvironmentError:
  347. pass
  348. def _create_file(self):
  349. self.f = open(self.filename, "w+")
  350. def _close_file(self):
  351. with self._count_lock:
  352. self.close_count += 1
  353. self.f.close()
  354. with self._count_lock:
  355. self.close_success_count += 1
  356. def _close_and_reopen_file(self):
  357. self._close_file()
  358. # if close raises an exception thats fine, self.f remains valid so
  359. # we don't need to reopen.
  360. self._create_file()
  361. def _run_workers(self, func, nb_workers, duration=0.2):
  362. with self._count_lock:
  363. self.close_count = 0
  364. self.close_success_count = 0
  365. self.do_continue = True
  366. threads = []
  367. try:
  368. for i in range(nb_workers):
  369. t = threading.Thread(target=func)
  370. t.start()
  371. threads.append(t)
  372. for _ in xrange(100):
  373. time.sleep(duration/100)
  374. with self._count_lock:
  375. if self.close_count-self.close_success_count > nb_workers+1:
  376. if test_support.verbose:
  377. print 'Q',
  378. break
  379. time.sleep(duration)
  380. finally:
  381. self.do_continue = False
  382. for t in threads:
  383. t.join()
  384. def _test_close_open_io(self, io_func, nb_workers=5):
  385. def worker():
  386. self._create_file()
  387. funcs = itertools.cycle((
  388. lambda: io_func(),
  389. lambda: self._close_and_reopen_file(),
  390. ))
  391. for f in funcs:
  392. if not self.do_continue:
  393. break
  394. try:
  395. f()
  396. except (IOError, ValueError):
  397. pass
  398. self._run_workers(worker, nb_workers)
  399. if test_support.verbose:
  400. # Useful verbose statistics when tuning this test to take
  401. # less time to run but still ensuring that its still useful.
  402. #
  403. # the percent of close calls that raised an error
  404. percent = 100. - 100.*self.close_success_count/self.close_count
  405. print self.close_count, ('%.4f ' % percent),
  406. def test_close_open(self):
  407. def io_func():
  408. pass
  409. self._test_close_open_io(io_func)
  410. def test_close_open_flush(self):
  411. def io_func():
  412. self.f.flush()
  413. self._test_close_open_io(io_func)
  414. def test_close_open_iter(self):
  415. def io_func():
  416. list(iter(self.f))
  417. self._test_close_open_io(io_func)
  418. def test_close_open_isatty(self):
  419. def io_func():
  420. self.f.isatty()
  421. self._test_close_open_io(io_func)
  422. def test_close_open_print(self):
  423. def io_func():
  424. print >> self.f, ''
  425. self._test_close_open_io(io_func)
  426. def test_close_open_read(self):
  427. def io_func():
  428. self.f.read(0)
  429. self._test_close_open_io(io_func)
  430. def test_close_open_readinto(self):
  431. def io_func():
  432. a = array('c', 'xxxxx')
  433. self.f.readinto(a)
  434. self._test_close_open_io(io_func)
  435. def test_close_open_readline(self):
  436. def io_func():
  437. self.f.readline()
  438. self._test_close_open_io(io_func)
  439. def test_close_open_readlines(self):
  440. def io_func():
  441. self.f.readlines()
  442. self._test_close_open_io(io_func)
  443. def test_close_open_seek(self):
  444. def io_func():
  445. self.f.seek(0, 0)
  446. self._test_close_open_io(io_func)
  447. def test_close_open_tell(self):
  448. def io_func():
  449. self.f.tell()
  450. self._test_close_open_io(io_func)
  451. def test_close_open_truncate(self):
  452. def io_func():
  453. self.f.truncate()
  454. self._test_close_open_io(io_func)
  455. def test_close_open_write(self):
  456. def io_func():
  457. self.f.write('')
  458. self._test_close_open_io(io_func)
  459. def test_close_open_writelines(self):
  460. def io_func():
  461. self.f.writelines('')
  462. self._test_close_open_io(io_func)
  463. class StdoutTests(unittest.TestCase):
  464. def test_move_stdout_on_write(self):
  465. # Issue 3242: sys.stdout can be replaced (and freed) during a
  466. # print statement; prevent a segfault in this case
  467. save_stdout = sys.stdout
  468. class File:
  469. def write(self, data):
  470. if '\n' in data:
  471. sys.stdout = save_stdout
  472. try:
  473. sys.stdout = File()
  474. print "some text"
  475. finally:
  476. sys.stdout = save_stdout
  477. def test_del_stdout_before_print(self):
  478. # Issue 4597: 'print' with no argument wasn't reporting when
  479. # sys.stdout was deleted.
  480. save_stdout = sys.stdout
  481. del sys.stdout
  482. try:
  483. print
  484. except RuntimeError as e:
  485. self.assertEquals(str(e), "lost sys.stdout")
  486. else:
  487. self.fail("Expected RuntimeError")
  488. finally:
  489. sys.stdout = save_stdout
  490. def test_main():
  491. # Historically, these tests have been sloppy about removing TESTFN.
  492. # So get rid of it no matter what.
  493. try:
  494. run_unittest(AutoFileTests, OtherFileTests, FileSubclassTests,
  495. FileThreadingTests, StdoutTests)
  496. finally:
  497. if os.path.exists(TESTFN):
  498. os.unlink(TESTFN)
  499. if __name__ == '__main__':
  500. test_main()