1 """File-like objects that read from or write to a string buffer.
3 This implements (nearly) all stdio methods.
5 f = StringIO() # ready for writing
6 f = StringIO(buf) # ready for reading
7 f.close() # explicitly release resources held
8 flag = f.isatty() # always false
9 pos = f.tell() # get current position
10 f.seek(pos) # set current position
11 f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF
12 buf = f.read() # read until EOF
13 buf = f.read(n) # read up to n bytes
14 buf = f.readline() # read until end of line ('\n') or EOF
15 list = f.readlines()# list of f.readline() results until EOF
16 f.truncate([size]) # truncate file at to at most size (default: current pos)
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 - There's also a much faster implementation in C, called cStringIO, but
24 it's not subclassable.
25 - fileno() is left unimplemented so that code which uses it triggers
27 - Seeking far beyond EOF and then writing will insert real null
28 bytes that occupy space in the buffer.
29 - There's a simple test set (see end of this file).
33 from errno
import EINVAL
37 __all__
= ["StringIO"]
40 def __init__(self
, buf
= ''):
51 del self
.buf
, self
.pos
55 raise ValueError, "I/O operation on closed file"
58 def seek(self
, pos
, mode
= 0):
60 raise ValueError, "I/O operation on closed file"
62 self
.buf
+= ''.join(self
.buflist
)
68 self
.pos
= max(0, pos
)
72 raise ValueError, "I/O operation on closed file"
75 def read(self
, n
= -1):
77 raise ValueError, "I/O operation on closed file"
79 self
.buf
+= ''.join(self
.buflist
)
84 newpos
= min(self
.pos
+n
, self
.len)
85 r
= self
.buf
[self
.pos
:newpos
]
89 def readline(self
, length
=None):
91 raise ValueError, "I/O operation on closed file"
93 self
.buf
+= ''.join(self
.buflist
)
95 i
= self
.buf
.find('\n', self
.pos
)
100 if length
is not None:
101 if self
.pos
+ length
< newpos
:
102 newpos
= self
.pos
+ length
103 r
= self
.buf
[self
.pos
:newpos
]
107 def readlines(self
, sizehint
= 0):
110 line
= self
.readline()
114 if 0 < sizehint
<= total
:
116 line
= self
.readline()
119 def truncate(self
, size
=None):
121 raise ValueError, "I/O operation on closed file"
125 raise IOError(EINVAL
, "Negative size not allowed")
126 elif size
< self
.pos
:
128 self
.buf
= self
.getvalue()[:size
]
132 raise ValueError, "I/O operation on closed file"
134 if self
.pos
> self
.len:
135 self
.buflist
.append('\0'*(self
.pos
- self
.len))
137 newpos
= self
.pos
+ len(s
)
138 if self
.pos
< self
.len:
140 self
.buf
+= ''.join(self
.buflist
)
142 self
.buflist
= [self
.buf
[:self
.pos
], s
, self
.buf
[newpos
:]]
144 if newpos
> self
.len:
147 self
.buflist
.append(s
)
151 def writelines(self
, list):
152 self
.write(''.join(list))
156 raise ValueError, "I/O operation on closed file"
160 self
.buf
+= ''.join(self
.buflist
)
165 # A little test suite
173 lines
= open(file, 'r').readlines()
174 text
= open(file, 'r').read()
176 for line
in lines
[:-2]:
178 f
.writelines(lines
[-2:])
179 if f
.getvalue() != text
:
180 raise RuntimeError, 'write failed'
182 print 'File length =', length
183 f
.seek(len(lines
[0]))
186 print 'First line =', `f
.readline()`
189 print 'Second line =', `line`
190 f
.seek(-len(line
), 1)
191 line2
= f
.read(len(line
))
193 raise RuntimeError, 'bad result after seek back'
194 f
.seek(len(line2
), 1)
197 f
.seek(f
.tell() - len(line
))
200 raise RuntimeError, 'bad result after seek back from EOF'
201 print 'Read', len(list), 'more lines'
202 print 'File length =', f
.tell()
203 if f
.tell() != length
:
204 raise RuntimeError, 'bad length'
207 if __name__
== '__main__':