Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """File-like objects that read from or write to a string buffer. |
| 2 | |
| 3 | This implements (nearly) all stdio methods. |
| 4 | |
| 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 |
Fred Drake | e0a7f4f | 2000-09-28 04:21:06 +0000 | [diff] [blame] | 16 | f.truncate([size]) # truncate file at to at most size (default: current pos) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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 |
| 20 | |
| 21 | Notes: |
| 22 | - Using a real file is often faster (but less convenient). |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 23 | - There's also a much faster implementation in C, called cStringIO, but |
| 24 | it's not subclassable. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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 Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 31 | |
Barry Warsaw | c7ed0e3 | 2000-12-12 23:12:23 +0000 | [diff] [blame] | 32 | try: |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 33 | from errno import EINVAL |
Barry Warsaw | c7ed0e3 | 2000-12-12 23:12:23 +0000 | [diff] [blame] | 34 | except ImportError: |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 35 | EINVAL = 22 |
Barry Warsaw | c7ed0e3 | 2000-12-12 23:12:23 +0000 | [diff] [blame] | 36 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 37 | __all__ = ["StringIO"] |
| 38 | |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 39 | class StringIO: |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 40 | def __init__(self, buf = ''): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 41 | self.buf = buf |
| 42 | self.len = len(buf) |
| 43 | self.buflist = [] |
| 44 | self.pos = 0 |
| 45 | self.closed = 0 |
| 46 | self.softspace = 0 |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 47 | |
| 48 | def close(self): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 49 | if not self.closed: |
| 50 | self.closed = 1 |
| 51 | del self.buf, self.pos |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 52 | |
| 53 | def isatty(self): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 54 | if self.closed: |
| 55 | raise ValueError, "I/O operation on closed file" |
| 56 | return 0 |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 57 | |
| 58 | def seek(self, pos, mode = 0): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 59 | if self.closed: |
| 60 | raise ValueError, "I/O operation on closed file" |
| 61 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 62 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 63 | 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 Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 69 | |
| 70 | def tell(self): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 71 | if self.closed: |
| 72 | raise ValueError, "I/O operation on closed file" |
| 73 | return self.pos |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 74 | |
| 75 | def read(self, n = -1): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 76 | if self.closed: |
| 77 | raise ValueError, "I/O operation on closed file" |
| 78 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 79 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 80 | 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 Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 88 | |
| 89 | def readline(self, length=None): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 90 | if self.closed: |
| 91 | raise ValueError, "I/O operation on closed file" |
| 92 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 93 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 94 | 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 Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 106 | |
| 107 | def readlines(self, sizehint = 0): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 108 | 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 Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 118 | |
| 119 | def truncate(self, size=None): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 120 | 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 Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 129 | |
| 130 | def write(self, s): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 131 | 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é Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 140 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 141 | 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 Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 150 | |
| 151 | def writelines(self, list): |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 152 | self.write(''.join(list)) |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 153 | |
| 154 | def flush(self): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 155 | if self.closed: |
| 156 | raise ValueError, "I/O operation on closed file" |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 157 | |
| 158 | def getvalue(self): |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 159 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 160 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 161 | self.buflist = [] |
| 162 | return self.buf |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 163 | |
| 164 | |
| 165 | # A little test suite |
| 166 | |
| 167 | def test(): |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 168 | import sys |
| 169 | if sys.argv[1:]: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 170 | file = sys.argv[1] |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 171 | else: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 172 | file = '/etc/passwd' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 173 | lines = open(file, 'r').readlines() |
| 174 | text = open(file, 'r').read() |
| 175 | f = StringIO() |
| 176 | for line in lines[:-2]: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 177 | f.write(line) |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 178 | f.writelines(lines[-2:]) |
| 179 | if f.getvalue() != text: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 180 | raise RuntimeError, 'write failed' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 181 | 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 Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 193 | raise RuntimeError, 'bad result after seek back' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 194 | 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 Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 200 | raise RuntimeError, 'bad result after seek back from EOF' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 201 | print 'Read', len(list), 'more lines' |
| 202 | print 'File length =', f.tell() |
| 203 | if f.tell() != length: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 204 | raise RuntimeError, 'bad length' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 205 | f.close() |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 206 | |
| 207 | if __name__ == '__main__': |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 208 | test() |