blob: 334bf853a2c13b8db8bd50579f92eded8aadd70d [file] [log] [blame]
Guido van Rossumaad67612000-05-08 17:31:04 +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
Guido van Rossume8d2f552000-10-09 22:14:43 +000016f.truncate([size]) # truncate file at to at most size (default: current pos)
Guido van Rossumaad67612000-05-08 17:31:04 +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).
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
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 Rossum5c971671996-07-22 15:23:25 +000031
Guido van Rossume8d2f552000-10-09 22:14:43 +000032import errno
Guido van Rossum5c971671996-07-22 15:23:25 +000033import string
34
35class StringIO:
36 def __init__(self, buf = ''):
37 self.buf = buf
38 self.len = len(buf)
39 self.buflist = []
40 self.pos = 0
41 self.closed = 0
42 self.softspace = 0
43 def close(self):
44 if not self.closed:
45 self.closed = 1
46 del self.buf, self.pos
47 def isatty(self):
Guido van Rossum64e736b1998-10-02 01:23:47 +000048 if self.closed:
49 raise ValueError, "I/O operation on closed file"
Guido van Rossum5c971671996-07-22 15:23:25 +000050 return 0
51 def seek(self, pos, mode = 0):
Guido van Rossum64e736b1998-10-02 01:23:47 +000052 if self.closed:
53 raise ValueError, "I/O operation on closed file"
Guido van Rossum5c971671996-07-22 15:23:25 +000054 if self.buflist:
55 self.buf = self.buf + string.joinfields(self.buflist, '')
56 self.buflist = []
57 if mode == 1:
58 pos = pos + self.pos
59 elif mode == 2:
60 pos = pos + self.len
61 self.pos = max(0, pos)
62 def tell(self):
Guido van Rossum64e736b1998-10-02 01:23:47 +000063 if self.closed:
64 raise ValueError, "I/O operation on closed file"
Guido van Rossum5c971671996-07-22 15:23:25 +000065 return self.pos
66 def read(self, n = -1):
Guido van Rossum64e736b1998-10-02 01:23:47 +000067 if self.closed:
68 raise ValueError, "I/O operation on closed file"
Guido van Rossum5c971671996-07-22 15:23:25 +000069 if self.buflist:
70 self.buf = self.buf + string.joinfields(self.buflist, '')
71 self.buflist = []
72 if n < 0:
73 newpos = self.len
74 else:
75 newpos = min(self.pos+n, self.len)
76 r = self.buf[self.pos:newpos]
77 self.pos = newpos
78 return r
Guido van Rossumff712aa1997-08-15 00:45:26 +000079 def readline(self, length=None):
Guido van Rossum64e736b1998-10-02 01:23:47 +000080 if self.closed:
81 raise ValueError, "I/O operation on closed file"
Guido van Rossum5c971671996-07-22 15:23:25 +000082 if self.buflist:
83 self.buf = self.buf + string.joinfields(self.buflist, '')
84 self.buflist = []
85 i = string.find(self.buf, '\n', self.pos)
86 if i < 0:
87 newpos = self.len
88 else:
89 newpos = i+1
Guido van Rossumff712aa1997-08-15 00:45:26 +000090 if length is not None:
91 if self.pos + length < newpos:
92 newpos = self.pos + length
Guido van Rossum5c971671996-07-22 15:23:25 +000093 r = self.buf[self.pos:newpos]
94 self.pos = newpos
95 return r
Jeremy Hyltond635b1d2000-09-26 17:32:27 +000096 def readlines(self, sizehint = 0):
97 total = 0
Guido van Rossum5c971671996-07-22 15:23:25 +000098 lines = []
99 line = self.readline()
100 while line:
101 lines.append(line)
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000102 total += len(line)
103 if 0 < sizehint <= total:
104 break
Guido van Rossum5c971671996-07-22 15:23:25 +0000105 line = self.readline()
106 return lines
Guido van Rossume8d2f552000-10-09 22:14:43 +0000107 def truncate(self, size=None):
108 if self.closed:
109 raise ValueError, "I/O operation on closed file"
110 if size is None:
111 size = self.pos
112 elif size < 0:
113 raise IOError(errno.EINVAL,
114 "Negative size not allowed")
115 elif size < self.pos:
116 self.pos = size
117 self.buf = self.getvalue()[:size]
Guido van Rossum5c971671996-07-22 15:23:25 +0000118 def write(self, s):
Guido van Rossum64e736b1998-10-02 01:23:47 +0000119 if self.closed:
120 raise ValueError, "I/O operation on closed file"
Guido van Rossum5c971671996-07-22 15:23:25 +0000121 if not s: return
122 if self.pos > self.len:
123 self.buflist.append('\0'*(self.pos - self.len))
124 self.len = self.pos
125 newpos = self.pos + len(s)
126 if self.pos < self.len:
127 if self.buflist:
128 self.buf = self.buf + string.joinfields(self.buflist, '')
129 self.buflist = []
130 self.buflist = [self.buf[:self.pos], s, self.buf[newpos:]]
131 self.buf = ''
132 else:
133 self.buflist.append(s)
134 self.len = newpos
135 self.pos = newpos
136 def writelines(self, list):
137 self.write(string.joinfields(list, ''))
138 def flush(self):
Guido van Rossum64e736b1998-10-02 01:23:47 +0000139 if self.closed:
140 raise ValueError, "I/O operation on closed file"
Guido van Rossum5c971671996-07-22 15:23:25 +0000141 def getvalue(self):
142 if self.buflist:
143 self.buf = self.buf + string.joinfields(self.buflist, '')
144 self.buflist = []
145 return self.buf
146
147
148# A little test suite
149
150def test():
151 import sys
152 if sys.argv[1:]:
153 file = sys.argv[1]
154 else:
155 file = '/etc/passwd'
156 lines = open(file, 'r').readlines()
157 text = open(file, 'r').read()
158 f = StringIO()
159 for line in lines[:-2]:
160 f.write(line)
161 f.writelines(lines[-2:])
162 if f.getvalue() != text:
163 raise RuntimeError, 'write failed'
164 length = f.tell()
165 print 'File length =', length
166 f.seek(len(lines[0]))
167 f.write(lines[1])
168 f.seek(0)
169 print 'First line =', `f.readline()`
170 here = f.tell()
171 line = f.readline()
172 print 'Second line =', `line`
173 f.seek(-len(line), 1)
174 line2 = f.read(len(line))
175 if line != line2:
176 raise RuntimeError, 'bad result after seek back'
177 f.seek(len(line2), 1)
178 list = f.readlines()
179 line = list[-1]
180 f.seek(f.tell() - len(line))
181 line2 = f.read()
182 if line != line2:
183 raise RuntimeError, 'bad result after seek back from EOF'
184 print 'Read', len(list), 'more lines'
185 print 'File length =', f.tell()
186 if f.tell() != length:
187 raise RuntimeError, 'bad length'
188 f.close()
189
190if __name__ == '__main__':
191 test()