2000-09-19 16:35:39 +00:00
|
|
|
# Tests StringIO and cStringIO
|
|
|
|
|
|
|
|
def do_test(module):
|
2000-10-11 21:34:53 +00:00
|
|
|
s = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+'\n')*5
|
2000-09-19 16:35:39 +00:00
|
|
|
f = module.StringIO(s)
|
|
|
|
print f.read(10)
|
|
|
|
print f.readline()
|
|
|
|
print len(f.readlines(60))
|
|
|
|
|
2000-10-12 16:46:28 +00:00
|
|
|
f = module.StringIO()
|
|
|
|
f.write('abcdef')
|
|
|
|
f.seek(3)
|
|
|
|
f.write('uvwxyz')
|
|
|
|
f.write('!')
|
|
|
|
print `f.getvalue()`
|
|
|
|
f.close()
|
2000-09-28 04:25:33 +00:00
|
|
|
f = module.StringIO()
|
|
|
|
f.write(s)
|
|
|
|
f.seek(10)
|
|
|
|
f.truncate()
|
|
|
|
print `f.getvalue()`
|
2000-10-06 19:21:32 +00:00
|
|
|
f.seek(0)
|
|
|
|
f.truncate(5)
|
|
|
|
print `f.getvalue()`
|
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
f.write("frobnitz")
|
|
|
|
except ValueError, e:
|
|
|
|
print "Caught expected ValueError writing to closed StringIO:"
|
|
|
|
print e
|
|
|
|
else:
|
|
|
|
print "Failed to catch ValueError writing to closed StringIO."
|
2000-09-28 04:25:33 +00:00
|
|
|
|
2000-09-19 16:35:39 +00:00
|
|
|
# Don't bother testing cStringIO without
|
|
|
|
import StringIO, cStringIO
|
|
|
|
do_test(StringIO)
|
|
|
|
do_test(cStringIO)
|