Martin v. Löwis | b96e0e5 | 2000-09-19 16:35:39 +0000 | [diff] [blame] | 1 | # Tests StringIO and cStringIO |
| 2 | |
| 3 | import string |
| 4 | |
| 5 | def do_test(module): |
| 6 | s = (string.letters+'\n')*5 |
| 7 | f = module.StringIO(s) |
| 8 | print f.read(10) |
| 9 | print f.readline() |
| 10 | print len(f.readlines(60)) |
| 11 | |
Fred Drake | 9543833 | 2000-09-28 04:25:33 +0000 | [diff] [blame] | 12 | f = module.StringIO() |
| 13 | f.write(s) |
| 14 | f.seek(10) |
| 15 | f.truncate() |
| 16 | print `f.getvalue()` |
Jim Fulton | d1229f5 | 2000-10-06 19:21:32 +0000 | [diff] [blame] | 17 | f.seek(0) |
| 18 | f.truncate(5) |
| 19 | print `f.getvalue()` |
| 20 | f.close() |
| 21 | try: |
| 22 | f.write("frobnitz") |
| 23 | except ValueError, e: |
| 24 | print "Caught expected ValueError writing to closed StringIO:" |
| 25 | print e |
| 26 | else: |
| 27 | print "Failed to catch ValueError writing to closed StringIO." |
Fred Drake | 9543833 | 2000-09-28 04:25:33 +0000 | [diff] [blame] | 28 | |
Martin v. Löwis | b96e0e5 | 2000-09-19 16:35:39 +0000 | [diff] [blame] | 29 | # Don't bother testing cStringIO without |
| 30 | import StringIO, cStringIO |
| 31 | do_test(StringIO) |
| 32 | do_test(cStringIO) |