1 # class StringIO implements file-like objects that read/write a
2 # string buffer (a.k.a. "memory files").
4 # This implements (nearly) all stdio methods.
6 # f = StringIO() # ready for writing
7 # f = StringIO(buf) # ready for reading
8 # f.close() # explicitly release resources held
9 # flag = f.isatty() # always false
10 # pos = f.tell() # get current position
11 # f.seek(pos) # set current position
12 # f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF
13 # buf = f.read() # read until EOF
14 # buf = f.read(n) # read up to n bytes
15 # buf = f.readline() # read until end of line ('\n') or EOF
16 # list = f.readlines()# list of f.readline() results until EOF
17 # f.write(buf) # write at current position
18 # f.writelines(list) # for line in list: f.write(line)
19 # f.getvalue() # return whole file's contents as a string
22 # - Using a real file is often faster (but less convenient).
23 # - fileno() is left unimplemented so that code which uses it triggers
25 # - Seeking far beyond EOF and then writing will insert real null
26 # bytes that occupy space in the buffer.
27 # - There's a simple test set (see end of this file).
32 def __init__(self
, buf
= ''):
42 del self
.buf
, self
.pos
45 raise ValueError, "I/O operation on closed file"
47 def seek(self
, pos
, mode
= 0):
49 raise ValueError, "I/O operation on closed file"
51 self
.buf
= self
.buf
+ string
.joinfields(self
.buflist
, '')
57 self
.pos
= max(0, pos
)
60 raise ValueError, "I/O operation on closed file"
62 def read(self
, n
= -1):
64 raise ValueError, "I/O operation on closed file"
66 self
.buf
= self
.buf
+ string
.joinfields(self
.buflist
, '')
71 newpos
= min(self
.pos
+n
, self
.len)
72 r
= self
.buf
[self
.pos
:newpos
]
75 def readline(self
, length
=None):
77 raise ValueError, "I/O operation on closed file"
79 self
.buf
= self
.buf
+ string
.joinfields(self
.buflist
, '')
81 i
= string
.find(self
.buf
, '\n', self
.pos
)
86 if length
is not None:
87 if self
.pos
+ length
< newpos
:
88 newpos
= self
.pos
+ length
89 r
= self
.buf
[self
.pos
:newpos
]
94 line
= self
.readline()
97 line
= self
.readline()
101 raise ValueError, "I/O operation on closed file"
103 if self
.pos
> self
.len:
104 self
.buflist
.append('\0'*(self
.pos
- self
.len))
106 newpos
= self
.pos
+ len(s
)
107 if self
.pos
< self
.len:
109 self
.buf
= self
.buf
+ string
.joinfields(self
.buflist
, '')
111 self
.buflist
= [self
.buf
[:self
.pos
], s
, self
.buf
[newpos
:]]
114 self
.buflist
.append(s
)
117 def writelines(self
, list):
118 self
.write(string
.joinfields(list, ''))
121 raise ValueError, "I/O operation on closed file"
124 self
.buf
= self
.buf
+ string
.joinfields(self
.buflist
, '')
129 # A little test suite
137 lines
= open(file, 'r').readlines()
138 text
= open(file, 'r').read()
140 for line
in lines
[:-2]:
142 f
.writelines(lines
[-2:])
143 if f
.getvalue() != text
:
144 raise RuntimeError, 'write failed'
146 print 'File length =', length
147 f
.seek(len(lines
[0]))
150 print 'First line =', `f
.readline()`
153 print 'Second line =', `line`
154 f
.seek(-len(line
), 1)
155 line2
= f
.read(len(line
))
157 raise RuntimeError, 'bad result after seek back'
158 f
.seek(len(line2
), 1)
161 f
.seek(f
.tell() - len(line
))
164 raise RuntimeError, 'bad result after seek back from EOF'
165 print 'Read', len(list), 'more lines'
166 print 'File length =', f
.tell()
167 if f
.tell() != length
:
168 raise RuntimeError, 'bad length'
171 if __name__
== '__main__':