home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_STRINGIO.PY < prev    next >
Encoding:
Text File  |  2000-10-12  |  911 b   |  38 lines

  1. # Tests StringIO and cStringIO
  2.  
  3. def do_test(module):
  4.     s = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+'\n')*5
  5.     f = module.StringIO(s)
  6.     print f.read(10)
  7.     print f.readline()
  8.     print len(f.readlines(60))
  9.  
  10.     f = module.StringIO()
  11.     f.write('abcdef')
  12.     f.seek(3)
  13.     f.write('uvwxyz')
  14.     f.write('!')
  15.     print `f.getvalue()`
  16.     f.close()
  17.     f = module.StringIO()
  18.     f.write(s)
  19.     f.seek(10)
  20.     f.truncate()
  21.     print `f.getvalue()`
  22.     f.seek(0)
  23.     f.truncate(5)
  24.     print `f.getvalue()`
  25.     f.close()
  26.     try:
  27.         f.write("frobnitz")
  28.     except ValueError, e:
  29.         print "Caught expected ValueError writing to closed StringIO:"
  30.         print e
  31.     else:
  32.         print "Failed to catch ValueError writing to closed StringIO."
  33.  
  34. # Don't bother testing cStringIO without
  35. import StringIO, cStringIO
  36. do_test(StringIO)
  37. do_test(cStringIO)
  38.