blob: 2aef3c329e166504892245c638fbd4ba9b3004cf [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""File-like objects that read from or write to a string buffer.
2
3This implements (nearly) all stdio methods.
4
5f = StringIO() # ready for writing
6f = StringIO(buf) # ready for reading
7f.close() # explicitly release resources held
8flag = f.isatty() # always false
9pos = f.tell() # get current position
10f.seek(pos) # set current position
11f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF
12buf = f.read() # read until EOF
13buf = f.read(n) # read up to n bytes
14buf = f.readline() # read until end of line ('\n') or EOF
15list = f.readlines()# list of f.readline() results until EOF
Fred Drakee0a7f4f2000-09-28 04:21:06 +000016f.truncate([size]) # truncate file at to at most size (default: current pos)
Guido van Rossum4acc25b2000-02-02 15:10:15 +000017f.write(buf) # write at current position
18f.writelines(list) # for line in list: f.write(line)
19f.getvalue() # return whole file's contents as a string
20
21Notes:
22- Using a real file is often faster (but less convenient).
Guido van Rossum98d9fd32000-02-28 15:12:25 +000023- There's also a much faster implementation in C, called cStringIO, but
24 it's not subclassable.
Guido van Rossum4acc25b2000-02-02 15:10:15 +000025- fileno() is left unimplemented so that code which uses it triggers
26 an exception early.
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).
30"""
Guido van Rossum85d89451994-06-23 11:53:27 +000031
Barry Warsawc7ed0e32000-12-12 23:12:23 +000032try:
Barry Warsawc1401312000-12-12 23:16:51 +000033 from errno import EINVAL
Barry Warsawc7ed0e32000-12-12 23:12:23 +000034except ImportError:
Barry Warsawc1401312000-12-12 23:16:51 +000035 EINVAL = 22
Barry Warsawc7ed0e32000-12-12 23:12:23 +000036
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000037__all__ = ["StringIO"]
38
Guido van Rossum85d89451994-06-23 11:53:27 +000039class StringIO:
Barry Warsawc1401312000-12-12 23:16:51 +000040 def __init__(self, buf = ''):
Fred Drakea63bd1c2000-12-13 20:23:11 +000041 self.buf = buf
42 self.len = len(buf)
43 self.buflist = []
44 self.pos = 0
45 self.closed = 0
46 self.softspace = 0
Barry Warsawc1401312000-12-12 23:16:51 +000047
48 def close(self):
Fred Drakea63bd1c2000-12-13 20:23:11 +000049 if not self.closed:
50 self.closed = 1
51 del self.buf, self.pos
Barry Warsawc1401312000-12-12 23:16:51 +000052
53 def isatty(self):
Fred Drakea63bd1c2000-12-13 20:23:11 +000054 if self.closed:
55 raise ValueError, "I/O operation on closed file"
56 return 0
Barry Warsawc1401312000-12-12 23:16:51 +000057
58 def seek(self, pos, mode = 0):
Fred Drakea63bd1c2000-12-13 20:23:11 +000059 if self.closed:
60 raise ValueError, "I/O operation on closed file"
61 if self.buflist:
Marc-André Lemburg85d6edf2001-02-09 13:37:37 +000062 self.buf += ''.join(self.buflist)
Fred Drakea63bd1c2000-12-13 20:23:11 +000063 self.buflist = []
64 if mode == 1:
65 pos += self.pos
66 elif mode == 2:
67 pos += self.len
68 self.pos = max(0, pos)
Barry Warsawc1401312000-12-12 23:16:51 +000069
70 def tell(self):
Fred Drakea63bd1c2000-12-13 20:23:11 +000071 if self.closed:
72 raise ValueError, "I/O operation on closed file"
73 return self.pos
Barry Warsawc1401312000-12-12 23:16:51 +000074
75 def read(self, n = -1):
Fred Drakea63bd1c2000-12-13 20:23:11 +000076 if self.closed:
77 raise ValueError, "I/O operation on closed file"
78 if self.buflist:
Marc-André Lemburg85d6edf2001-02-09 13:37:37 +000079 self.buf += ''.join(self.buflist)
Fred Drakea63bd1c2000-12-13 20:23:11 +000080 self.buflist = []
81 if n < 0:
82 newpos = self.len
83 else:
84 newpos = min(self.pos+n, self.len)
85 r = self.buf[self.pos:newpos]
86 self.pos = newpos
87 return r
Barry Warsawc1401312000-12-12 23:16:51 +000088
89 def readline(self, length=None):
Fred Drakea63bd1c2000-12-13 20:23:11 +000090 if self.closed:
91 raise ValueError, "I/O operation on closed file"
92 if self.buflist:
Marc-André Lemburg85d6edf2001-02-09 13:37:37 +000093 self.buf += ''.join(self.buflist)
Fred Drakea63bd1c2000-12-13 20:23:11 +000094 self.buflist = []
95 i = self.buf.find('\n', self.pos)
96 if i < 0:
97 newpos = self.len
98 else:
99 newpos = i+1
100 if length is not None:
101 if self.pos + length < newpos:
102 newpos = self.pos + length
103 r = self.buf[self.pos:newpos]
104 self.pos = newpos
105 return r
Barry Warsawc1401312000-12-12 23:16:51 +0000106
107 def readlines(self, sizehint = 0):
Fred Drakea63bd1c2000-12-13 20:23:11 +0000108 total = 0
109 lines = []
110 line = self.readline()
111 while line:
112 lines.append(line)
113 total += len(line)
114 if 0 < sizehint <= total:
115 break
116 line = self.readline()
117 return lines
Barry Warsawc1401312000-12-12 23:16:51 +0000118
119 def truncate(self, size=None):
Fred Drakea63bd1c2000-12-13 20:23:11 +0000120 if self.closed:
121 raise ValueError, "I/O operation on closed file"
122 if size is None:
123 size = self.pos
124 elif size < 0:
125 raise IOError(EINVAL, "Negative size not allowed")
126 elif size < self.pos:
127 self.pos = size
128 self.buf = self.getvalue()[:size]
Barry Warsawc1401312000-12-12 23:16:51 +0000129
130 def write(self, s):
Fred Drakea63bd1c2000-12-13 20:23:11 +0000131 if self.closed:
132 raise ValueError, "I/O operation on closed file"
133 if not s: return
134 if self.pos > self.len:
135 self.buflist.append('\0'*(self.pos - self.len))
136 self.len = self.pos
137 newpos = self.pos + len(s)
138 if self.pos < self.len:
139 if self.buflist:
Marc-André Lemburg85d6edf2001-02-09 13:37:37 +0000140 self.buf += ''.join(self.buflist)
Fred Drakea63bd1c2000-12-13 20:23:11 +0000141 self.buflist = []
142 self.buflist = [self.buf[:self.pos], s, self.buf[newpos:]]
143 self.buf = ''
144 if newpos > self.len:
145 self.len = newpos
146 else:
147 self.buflist.append(s)
148 self.len = newpos
149 self.pos = newpos
Barry Warsawc1401312000-12-12 23:16:51 +0000150
151 def writelines(self, list):
Marc-André Lemburg85d6edf2001-02-09 13:37:37 +0000152 self.write(''.join(list))
Barry Warsawc1401312000-12-12 23:16:51 +0000153
154 def flush(self):
Fred Drakea63bd1c2000-12-13 20:23:11 +0000155 if self.closed:
156 raise ValueError, "I/O operation on closed file"
Barry Warsawc1401312000-12-12 23:16:51 +0000157
158 def getvalue(self):
Fred Drakea63bd1c2000-12-13 20:23:11 +0000159 if self.buflist:
Marc-André Lemburg85d6edf2001-02-09 13:37:37 +0000160 self.buf += ''.join(self.buflist)
Fred Drakea63bd1c2000-12-13 20:23:11 +0000161 self.buflist = []
162 return self.buf
Guido van Rossum85d89451994-06-23 11:53:27 +0000163
164
165# A little test suite
166
167def test():
Barry Warsawc1401312000-12-12 23:16:51 +0000168 import sys
169 if sys.argv[1:]:
Fred Drakea63bd1c2000-12-13 20:23:11 +0000170 file = sys.argv[1]
Barry Warsawc1401312000-12-12 23:16:51 +0000171 else:
Fred Drakea63bd1c2000-12-13 20:23:11 +0000172 file = '/etc/passwd'
Barry Warsawc1401312000-12-12 23:16:51 +0000173 lines = open(file, 'r').readlines()
174 text = open(file, 'r').read()
175 f = StringIO()
176 for line in lines[:-2]:
Fred Drakea63bd1c2000-12-13 20:23:11 +0000177 f.write(line)
Barry Warsawc1401312000-12-12 23:16:51 +0000178 f.writelines(lines[-2:])
179 if f.getvalue() != text:
Fred Drakea63bd1c2000-12-13 20:23:11 +0000180 raise RuntimeError, 'write failed'
Barry Warsawc1401312000-12-12 23:16:51 +0000181 length = f.tell()
182 print 'File length =', length
183 f.seek(len(lines[0]))
184 f.write(lines[1])
185 f.seek(0)
186 print 'First line =', `f.readline()`
187 here = f.tell()
188 line = f.readline()
189 print 'Second line =', `line`
190 f.seek(-len(line), 1)
191 line2 = f.read(len(line))
192 if line != line2:
Fred Drakea63bd1c2000-12-13 20:23:11 +0000193 raise RuntimeError, 'bad result after seek back'
Barry Warsawc1401312000-12-12 23:16:51 +0000194 f.seek(len(line2), 1)
195 list = f.readlines()
196 line = list[-1]
197 f.seek(f.tell() - len(line))
198 line2 = f.read()
199 if line != line2:
Fred Drakea63bd1c2000-12-13 20:23:11 +0000200 raise RuntimeError, 'bad result after seek back from EOF'
Barry Warsawc1401312000-12-12 23:16:51 +0000201 print 'Read', len(list), 'more lines'
202 print 'File length =', f.tell()
203 if f.tell() != length:
Fred Drakea63bd1c2000-12-13 20:23:11 +0000204 raise RuntimeError, 'bad length'
Barry Warsawc1401312000-12-12 23:16:51 +0000205 f.close()
Guido van Rossum85d89451994-06-23 11:53:27 +0000206
207if __name__ == '__main__':
Barry Warsawc1401312000-12-12 23:16:51 +0000208 test()