Mercurial > cpython-lru_cache-weakref
annotate Lib/StringIO.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | a0005d26773d |
| children |
| rev | line source |
|---|---|
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
1 """File-like objects that read from or write to a string buffer. |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
2 |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
3 This implements (nearly) all stdio methods. |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
4 |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
5 f = StringIO() # ready for writing |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
6 f = StringIO(buf) # ready for reading |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
7 f.close() # explicitly release resources held |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
8 flag = f.isatty() # always false |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
9 pos = f.tell() # get current position |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
10 f.seek(pos) # set current position |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
11 f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
12 buf = f.read() # read until EOF |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
13 buf = f.read(n) # read up to n bytes |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
14 buf = f.readline() # read until end of line ('\n') or EOF |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
15 list = f.readlines()# list of f.readline() results until EOF |
|
14904
b15fce868e50
Add truncate() method to StringIO objects.
Fred Drake <fdrake@acm.org>
parents:
14680
diff
changeset
|
16 f.truncate([size]) # truncate file at to at most size (default: current pos) |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
17 f.write(buf) # write at current position |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
18 f.writelines(list) # for line in list: f.write(line) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
19 f.getvalue() # return whole file's contents as a string |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
20 |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
21 Notes: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
22 - Using a real file is often faster (but less convenient). |
|
11870
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
11691
diff
changeset
|
23 - There's also a much faster implementation in C, called cStringIO, but |
|
d7eca5b0960a
Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents:
11691
diff
changeset
|
24 it's not subclassable. |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
25 - fileno() is left unimplemented so that code which uses it triggers |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
26 an exception early. |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
27 - Seeking far beyond EOF and then writing will insert real null |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
28 bytes that occupy space in the buffer. |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
29 - There's a simple test set (see end of this file). |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
9016
diff
changeset
|
30 """ |
| 21820 | 31 import types |
|
15541
58bc99e012a8
Accept Finn Bock's patch #102208 to hardcode EINVAL to 22 when errno
Barry Warsaw <barry@python.org>
parents:
15174
diff
changeset
|
32 try: |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
33 from errno import EINVAL |
|
15541
58bc99e012a8
Accept Finn Bock's patch #102208 to hardcode EINVAL to 22 when errno
Barry Warsaw <barry@python.org>
parents:
15174
diff
changeset
|
34 except ImportError: |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
35 EINVAL = 22 |
|
15541
58bc99e012a8
Accept Finn Bock's patch #102208 to hardcode EINVAL to 22 when errno
Barry Warsaw <barry@python.org>
parents:
15174
diff
changeset
|
36 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15555
diff
changeset
|
37 __all__ = ["StringIO"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15555
diff
changeset
|
38 |
|
1680
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 class StringIO: |
|
25622
a0005d26773d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23456
diff
changeset
|
40 """class StringIO([buffer]) |
|
a0005d26773d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23456
diff
changeset
|
41 |
|
23456
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
42 When a StringIO object is created, it can be initialized to an existing |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
43 string by passing the string to the constructor. If no string is given, |
|
25622
a0005d26773d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23456
diff
changeset
|
44 the StringIO will start empty. |
|
23456
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
45 |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
46 The StringIO object can accept either Unicode or 8-bit strings, but |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
47 mixing the two may take some care. If both are used, 8-bit strings that |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
48 cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause |
|
25622
a0005d26773d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23456
diff
changeset
|
49 a UnicodeError to be raised when getvalue() is called. |
|
23456
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
50 """ |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
51 def __init__(self, buf = ''): |
| 21820 | 52 # Force self.buf to be a string or unicode |
|
22382
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21820
diff
changeset
|
53 if type(buf) not in types.StringTypes: |
| 21820 | 54 buf = str(buf) |
| 55 self.buf = buf | |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
56 self.len = len(buf) |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
57 self.buflist = [] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
58 self.pos = 0 |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
59 self.closed = 0 |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
60 self.softspace = 0 |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
61 |
|
20032
5c9a955ef17c
__iter__(): New method so that StringIO's can participate in the
Barry Warsaw <barry@python.org>
parents:
16522
diff
changeset
|
62 def __iter__(self): |
|
5c9a955ef17c
__iter__(): New method so that StringIO's can participate in the
Barry Warsaw <barry@python.org>
parents:
16522
diff
changeset
|
63 return iter(self.readline, '') |
|
5c9a955ef17c
__iter__(): New method so that StringIO's can participate in the
Barry Warsaw <barry@python.org>
parents:
16522
diff
changeset
|
64 |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
65 def close(self): |
|
23456
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
66 """Free the memory buffer.""" |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
67 if not self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
68 self.closed = 1 |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
69 del self.buf, self.pos |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
70 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
71 def isatty(self): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
72 if self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
73 raise ValueError, "I/O operation on closed file" |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
74 return 0 |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
75 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
76 def seek(self, pos, mode = 0): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
77 if self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
78 raise ValueError, "I/O operation on closed file" |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
79 if self.buflist: |
|
16522
f42799c68929
Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
80 self.buf += ''.join(self.buflist) |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
81 self.buflist = [] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
82 if mode == 1: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
83 pos += self.pos |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
84 elif mode == 2: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
85 pos += self.len |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
86 self.pos = max(0, pos) |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
87 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
88 def tell(self): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
89 if self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
90 raise ValueError, "I/O operation on closed file" |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
91 return self.pos |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
92 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
93 def read(self, n = -1): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
94 if self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
95 raise ValueError, "I/O operation on closed file" |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
96 if self.buflist: |
|
16522
f42799c68929
Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
97 self.buf += ''.join(self.buflist) |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
98 self.buflist = [] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
99 if n < 0: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
100 newpos = self.len |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
101 else: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
102 newpos = min(self.pos+n, self.len) |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
103 r = self.buf[self.pos:newpos] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
104 self.pos = newpos |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
105 return r |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
106 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
107 def readline(self, length=None): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
108 if self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
109 raise ValueError, "I/O operation on closed file" |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
110 if self.buflist: |
|
16522
f42799c68929
Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
111 self.buf += ''.join(self.buflist) |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
112 self.buflist = [] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
113 i = self.buf.find('\n', self.pos) |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
114 if i < 0: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
115 newpos = self.len |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
116 else: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
117 newpos = i+1 |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
118 if length is not None: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
119 if self.pos + length < newpos: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
120 newpos = self.pos + length |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
121 r = self.buf[self.pos:newpos] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
122 self.pos = newpos |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
123 return r |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
124 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
125 def readlines(self, sizehint = 0): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
126 total = 0 |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
127 lines = [] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
128 line = self.readline() |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
129 while line: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
130 lines.append(line) |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
131 total += len(line) |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
132 if 0 < sizehint <= total: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
133 break |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
134 line = self.readline() |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
135 return lines |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
136 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
137 def truncate(self, size=None): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
138 if self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
139 raise ValueError, "I/O operation on closed file" |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
140 if size is None: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
141 size = self.pos |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
142 elif size < 0: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
143 raise IOError(EINVAL, "Negative size not allowed") |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
144 elif size < self.pos: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
145 self.pos = size |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
146 self.buf = self.getvalue()[:size] |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
147 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
148 def write(self, s): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
149 if self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
150 raise ValueError, "I/O operation on closed file" |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
151 if not s: return |
| 21820 | 152 # Force s to be a string or unicode |
|
22382
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21820
diff
changeset
|
153 if type(s) not in types.StringTypes: |
| 21820 | 154 s = str(s) |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
155 if self.pos > self.len: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
156 self.buflist.append('\0'*(self.pos - self.len)) |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
157 self.len = self.pos |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
158 newpos = self.pos + len(s) |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
159 if self.pos < self.len: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
160 if self.buflist: |
|
16522
f42799c68929
Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
161 self.buf += ''.join(self.buflist) |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
162 self.buflist = [] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
163 self.buflist = [self.buf[:self.pos], s, self.buf[newpos:]] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
164 self.buf = '' |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
165 if newpos > self.len: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
166 self.len = newpos |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
167 else: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
168 self.buflist.append(s) |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
169 self.len = newpos |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
170 self.pos = newpos |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
171 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
172 def writelines(self, list): |
|
16522
f42799c68929
Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
173 self.write(''.join(list)) |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
174 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
175 def flush(self): |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
176 if self.closed: |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
177 raise ValueError, "I/O operation on closed file" |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
178 |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
179 def getvalue(self): |
|
23456
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
180 """ |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
181 Retrieve the entire contents of the "file" at any time before |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
182 the StringIO object's close() method is called. |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
183 |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
184 The StringIO object can accept either Unicode or 8-bit strings, |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
185 but mixing the two may take some care. If both are used, 8-bit |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
186 strings that cannot be interpreted as 7-bit ASCII (that use the |
|
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
187 8th bit) will cause a UnicodeError to be raised when getvalue() |
|
25622
a0005d26773d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23456
diff
changeset
|
188 is called. |
|
23456
64eb119b816d
Add docstrings excerpted from the library reference.
Raymond Hettinger <python@rcn.com>
parents:
22382
diff
changeset
|
189 """ |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
190 if self.buflist: |
|
16522
f42799c68929
Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents:
16070
diff
changeset
|
191 self.buf += ''.join(self.buflist) |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
192 self.buflist = [] |
|
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
193 return self.buf |
|
1680
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 |
|
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 |
|
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 # A little test suite |
|
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 |
|
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 def test(): |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
199 import sys |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
200 if sys.argv[1:]: |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
201 file = sys.argv[1] |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
202 else: |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
203 file = '/etc/passwd' |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
204 lines = open(file, 'r').readlines() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
205 text = open(file, 'r').read() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
206 f = StringIO() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
207 for line in lines[:-2]: |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
208 f.write(line) |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
209 f.writelines(lines[-2:]) |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
210 if f.getvalue() != text: |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
211 raise RuntimeError, 'write failed' |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
212 length = f.tell() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
213 print 'File length =', length |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
214 f.seek(len(lines[0])) |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
215 f.write(lines[1]) |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
216 f.seek(0) |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
217 print 'First line =', `f.readline()` |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
218 here = f.tell() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
219 line = f.readline() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
220 print 'Second line =', `line` |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
221 f.seek(-len(line), 1) |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
222 line2 = f.read(len(line)) |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
223 if line != line2: |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
224 raise RuntimeError, 'bad result after seek back' |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
225 f.seek(len(line2), 1) |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
226 list = f.readlines() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
227 line = list[-1] |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
228 f.seek(f.tell() - len(line)) |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
229 line2 = f.read() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
230 if line != line2: |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
231 raise RuntimeError, 'bad result after seek back from EOF' |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
232 print 'Read', len(list), 'more lines' |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
233 print 'File length =', f.tell() |
|
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
234 if f.tell() != length: |
|
15555
f72345b43662
Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents:
15542
diff
changeset
|
235 raise RuntimeError, 'bad length' |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
236 f.close() |
|
1680
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 |
|
0a60460e99f0
Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 if __name__ == '__main__': |
|
15542
6359480106ec
Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents:
15541
diff
changeset
|
239 test() |
