annotate Lib/StringIO.py @ 49955:878d40468c4e legacy-trunk

Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
author Vinay Sajip <vinay_sajip@yahoo.co.uk>
date Wed, 03 Dec 2008 23:22:58 +0000
parents 7f76996c7bf4
children 6052751a04c8 e8532cc8a14e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31190
292ad135cef1 Make the module docstring a raw string, so that the backslash in
Walter Dörwald <walter@livinglogic.de>
parents: 30708
diff changeset
1 r"""File-like objects that read from or write to a string buffer.
11691
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 """
15541
58bc99e012a8 Accept Finn Bock's patch #102208 to hardcode EINVAL to 22 when errno
Barry Warsaw <barry@python.org>
parents: 15174
diff changeset
31 try:
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
32 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
33 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
34 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
35
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15555
diff changeset
36 __all__ = ["StringIO"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15555
diff changeset
37
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
38 def _complain_ifclosed(closed):
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
39 if closed:
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
40 raise ValueError, "I/O operation on closed file"
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
41
1680
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 class StringIO:
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
43 """class StringIO([buffer])
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
44
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
45 When a StringIO object is created, it can be initialized to an existing
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
46 string by passing the string to the constructor. If no string is given,
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
47 the StringIO will start empty.
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
48
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
49 The StringIO object can accept either Unicode or 8-bit strings, but
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
50 mixing the two may take some care. If both are used, 8-bit strings that
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
51 cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
52 a UnicodeError to be raised when getvalue() is called.
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
53 """
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
54 def __init__(self, buf = ''):
21656
6b0e16cb65d5 Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents: 20060
diff changeset
55 # Force self.buf to be a string or unicode
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23521
diff changeset
56 if not isinstance(buf, basestring):
21656
6b0e16cb65d5 Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents: 20060
diff changeset
57 buf = str(buf)
6b0e16cb65d5 Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents: 20060
diff changeset
58 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
59 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
60 self.buflist = []
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
61 self.pos = 0
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
62 self.closed = False
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
63 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
64
20032
5c9a955ef17c __iter__(): New method so that StringIO's can participate in the
Barry Warsaw <barry@python.org>
parents: 16522
diff changeset
65 def __iter__(self):
27550
31807880f599 Make StringIO its own iterator, similar to real files.
Guido van Rossum <guido@python.org>
parents: 25305
diff changeset
66 return self
31807880f599 Make StringIO its own iterator, similar to real files.
Guido van Rossum <guido@python.org>
parents: 25305
diff changeset
67
31807880f599 Make StringIO its own iterator, similar to real files.
Guido van Rossum <guido@python.org>
parents: 25305
diff changeset
68 def next(self):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
69 """A file object is its own iterator, for example iter(f) returns f
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
70 (unless f is closed). When a file is used as an iterator, typically
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
71 in a for loop (for example, for line in f: print line), the next()
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
72 method is called repeatedly. This method returns the next input line,
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
73 or raises StopIteration when EOF is hit.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
74 """
36991
8e8d0e03f076 SF patch #1359365: file and cStringIO raise a ValueError when next() is called
Walter Dörwald <walter@livinglogic.de>
parents: 34207
diff changeset
75 _complain_ifclosed(self.closed)
27550
31807880f599 Make StringIO its own iterator, similar to real files.
Guido van Rossum <guido@python.org>
parents: 25305
diff changeset
76 r = self.readline()
31807880f599 Make StringIO its own iterator, similar to real files.
Guido van Rossum <guido@python.org>
parents: 25305
diff changeset
77 if not r:
31807880f599 Make StringIO its own iterator, similar to real files.
Guido van Rossum <guido@python.org>
parents: 25305
diff changeset
78 raise StopIteration
31807880f599 Make StringIO its own iterator, similar to real files.
Guido van Rossum <guido@python.org>
parents: 25305
diff changeset
79 return r
20032
5c9a955ef17c __iter__(): New method so that StringIO's can participate in the
Barry Warsaw <barry@python.org>
parents: 16522
diff changeset
80
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
81 def close(self):
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
82 """Free the memory buffer.
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
83 """
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
84 if not self.closed:
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
85 self.closed = True
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
86 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
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 isatty(self):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
89 """Returns False because StringIO objects are not connected to a
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
90 tty-like device.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
91 """
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
92 _complain_ifclosed(self.closed)
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 21915
diff changeset
93 return False
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
94
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
95 def seek(self, pos, mode = 0):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
96 """Set the file's current position.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
97
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
98 The mode argument is optional and defaults to 0 (absolute file
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
99 positioning); other values are 1 (seek relative to the current
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
100 position) and 2 (seek relative to the file's end).
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
101
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
102 There is no return value.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
103 """
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
104 _complain_ifclosed(self.closed)
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
105 if self.buflist:
16522
f42799c68929 Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
106 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
107 self.buflist = []
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
108 if mode == 1:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
109 pos += self.pos
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
110 elif mode == 2:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
111 pos += self.len
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
112 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
113
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
114 def tell(self):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
115 """Return the file's current position."""
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
116 _complain_ifclosed(self.closed)
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
117 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
118
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
119 def read(self, n = -1):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
120 """Read at most size bytes from the file
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
121 (less if the read hits EOF before obtaining size bytes).
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
122
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
123 If the size argument is negative or omitted, read all data until EOF
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
124 is reached. The bytes are returned as a string object. An empty
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
125 string is returned when EOF is encountered immediately.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
126 """
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
127 _complain_ifclosed(self.closed)
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
128 if self.buflist:
16522
f42799c68929 Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
129 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
130 self.buflist = []
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
131 if n < 0:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
132 newpos = self.len
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
133 else:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
134 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
135 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
136 self.pos = newpos
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
137 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
138
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
139 def readline(self, length=None):
40552
7f76996c7bf4 [Bug #1622533] Make docstrings raw strings because they contain control characters (\0, \1)
Andrew M. Kuchling <amk@amk.ca>
parents: 36991
diff changeset
140 r"""Read one entire line from the file.
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
141
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
142 A trailing newline character is kept in the string (but may be absent
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
143 when a file ends with an incomplete line). If the size argument is
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
144 present and non-negative, it is a maximum byte count (including the
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
145 trailing newline) and an incomplete line may be returned.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
146
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
147 An empty string is returned only when EOF is encountered immediately.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
148
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
149 Note: Unlike stdio's fgets(), the returned string contains null
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
150 characters ('\0') if they occurred in the input.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
151 """
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
152 _complain_ifclosed(self.closed)
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
153 if self.buflist:
16522
f42799c68929 Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
154 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
155 self.buflist = []
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
156 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
157 if i < 0:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
158 newpos = self.len
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
159 else:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
160 newpos = i+1
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
161 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
162 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
163 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
164 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
165 self.pos = newpos
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
166 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
167
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
168 def readlines(self, sizehint = 0):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
169 """Read until EOF using readline() and return a list containing the
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
170 lines thus read.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
171
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
172 If the optional sizehint argument is present, instead of reading up
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
173 to EOF, whole lines totalling approximately sizehint bytes (or more
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
174 to accommodate a final whole line).
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
175 """
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
176 total = 0
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
177 lines = []
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
178 line = self.readline()
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
179 while line:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
180 lines.append(line)
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
181 total += len(line)
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
182 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
183 break
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
184 line = self.readline()
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
185 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
186
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
187 def truncate(self, size=None):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
188 """Truncate the file's size.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
189
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
190 If the optional size argument is present, the file is truncated to
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
191 (at most) that size. The size defaults to the current position.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
192 The current file position is not changed unless the position
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
193 is beyond the new file size.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
194
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
195 If the specified size exceeds the file's current size, the
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
196 file remains unchanged.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
197 """
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31190
diff changeset
198 _complain_ifclosed(self.closed)
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
199 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
200 size = self.pos
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
201 elif size < 0:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
202 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
203 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
204 self.pos = size
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
205 self.buf = self.getvalue()[:size]
34207
19286994f74f SF bug #951915: fix bug in StringIO.truncate - length not changed
Raymond Hettinger <python@rcn.com>
parents: 33545
diff changeset
206 self.len = size
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
207
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
208 def write(self, s):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
209 """Write a string to the file.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
210
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
211 There is no return value.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
212 """
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
213 _complain_ifclosed(self.closed)
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
214 if not s: return
21656
6b0e16cb65d5 Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents: 20060
diff changeset
215 # Force s to be a string or unicode
23645
e1e90b0f1f45 Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents: 23521
diff changeset
216 if not isinstance(s, basestring):
21656
6b0e16cb65d5 Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents: 20060
diff changeset
217 s = str(s)
33544
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
218 spos = self.pos
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
219 slen = self.len
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
220 if spos == slen:
25305
0399c8aea7b2 write(): Special case the common situation of a stream that's only
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
221 self.buflist.append(s)
33545
6d055a275b4b Arghh, checked in wrong draft. Replacing with correct one.
Raymond Hettinger <python@rcn.com>
parents: 33544
diff changeset
222 self.len = self.pos = spos + len(s)
25305
0399c8aea7b2 write(): Special case the common situation of a stream that's only
Fred Drake <fdrake@acm.org>
parents: 23645
diff changeset
223 return
33544
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
224 if spos > slen:
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
225 self.buflist.append('\0'*(spos - slen))
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
226 slen = spos
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
227 newpos = spos + len(s)
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
228 if spos < slen:
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
229 if self.buflist:
16522
f42799c68929 Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
230 self.buf += ''.join(self.buflist)
33544
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
231 self.buflist = [self.buf[:spos], s, self.buf[newpos:]]
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
232 self.buf = ''
33544
d6071a05ba84 Use local variables in StringIO.write().
Raymond Hettinger <python@rcn.com>
parents: 31662
diff changeset
233 if newpos > slen:
33545
6d055a275b4b Arghh, checked in wrong draft. Replacing with correct one.
Raymond Hettinger <python@rcn.com>
parents: 33544
diff changeset
234 slen = newpos
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
235 else:
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
236 self.buflist.append(s)
33545
6d055a275b4b Arghh, checked in wrong draft. Replacing with correct one.
Raymond Hettinger <python@rcn.com>
parents: 33544
diff changeset
237 slen = newpos
6d055a275b4b Arghh, checked in wrong draft. Replacing with correct one.
Raymond Hettinger <python@rcn.com>
parents: 33544
diff changeset
238 self.len = slen
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
239 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
240
31626
1543afab1b67 SF patch #907403: Improvements to cStringIO.writelines()
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
241 def writelines(self, iterable):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
242 """Write a sequence of strings to the file. The sequence can be any
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
243 iterable object producing strings, typically a list of strings. There
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
244 is no return value.
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
245
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
246 (The name is intended to match readlines(); writelines() does not add
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
247 line separators.)
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
248 """
31626
1543afab1b67 SF patch #907403: Improvements to cStringIO.writelines()
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
249 write = self.write
1543afab1b67 SF patch #907403: Improvements to cStringIO.writelines()
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
250 for line in iterable:
1543afab1b67 SF patch #907403: Improvements to cStringIO.writelines()
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
251 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
252
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
253 def flush(self):
31662
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
254 """Flush the internal buffer
88e9c0444d4d Add missing docstrings.
Raymond Hettinger <python@rcn.com>
parents: 31626
diff changeset
255 """
30708
3a99294c8e5b Patch #822994: Consolidate tests for self.closed.
Martin v. Löwis <martin@v.loewis.de>
parents: 27550
diff changeset
256 _complain_ifclosed(self.closed)
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
257
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
258 def getvalue(self):
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
259 """
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
260 Retrieve the entire contents of the "file" at any time before
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
261 the StringIO object's close() method is called.
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
262
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
263 The StringIO object can accept either Unicode or 8-bit strings,
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
264 but mixing the two may take some care. If both are used, 8-bit
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
265 strings that cannot be interpreted as 7-bit ASCII (that use the
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
266 8th bit) will cause a UnicodeError to be raised when getvalue()
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23455
diff changeset
267 is called.
23455
68c776ba5ea5 Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents: 23432
diff changeset
268 """
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
269 if self.buflist:
16522
f42799c68929 Remove silly EMPTYSTRING global. Saves a global lookup.
Marc-André Lemburg <mal@egenix.com>
parents: 16070
diff changeset
270 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
271 self.buflist = []
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
272 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
273
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 # 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
276
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 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
278 import sys
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
279 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
280 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
281 else:
15555
f72345b43662 Untabify! (Barry, this is gonna cost you a bottle of wine! ;)
Fred Drake <fdrake@acm.org>
parents: 15542
diff changeset
282 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
283 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
284 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
285 f = StringIO()
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
286 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
287 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
288 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
289 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
290 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
291 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
292 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
293 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
294 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
295 f.seek(0)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
296 print 'First line =', repr(f.readline())
21915
e163713b47cf SF #515000, print result of f.tell() in test() instead of ignoring
Neal Norwitz <nnorwitz@gmail.com>
parents: 21656
diff changeset
297 print 'Position =', f.tell()
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
298 line = f.readline()
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
299 print 'Second line =', repr(line)
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
300 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
301 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
302 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
303 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
304 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
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
313 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
314 raise RuntimeError, 'bad length'
34207
19286994f74f SF bug #951915: fix bug in StringIO.truncate - length not changed
Raymond Hettinger <python@rcn.com>
parents: 33545
diff changeset
315 f.truncate(length/2)
19286994f74f SF bug #951915: fix bug in StringIO.truncate - length not changed
Raymond Hettinger <python@rcn.com>
parents: 33545
diff changeset
316 f.seek(0, 2)
19286994f74f SF bug #951915: fix bug in StringIO.truncate - length not changed
Raymond Hettinger <python@rcn.com>
parents: 33545
diff changeset
317 print 'Truncated length =', f.tell()
19286994f74f SF bug #951915: fix bug in StringIO.truncate - length not changed
Raymond Hettinger <python@rcn.com>
parents: 33545
diff changeset
318 if f.tell() != length/2:
19286994f74f SF bug #951915: fix bug in StringIO.truncate - length not changed
Raymond Hettinger <python@rcn.com>
parents: 33545
diff changeset
319 raise RuntimeError, 'truncate did not adjust length'
15542
6359480106ec Change the file's indentation from tabs to 4 spaces per level.
Barry Warsaw <barry@python.org>
parents: 15541
diff changeset
320 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
321
0a60460e99f0 Two new generally useful modules: types defines names for all built-in types,
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 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
323 test()