@@ -66,6 +66,12 @@ def __iter__(self):
6666 return self
6767
6868 def next (self ):
69+ """A file object is its own iterator, for example iter(f) returns f
70+ (unless f is closed). When a file is used as an iterator, typically
71+ in a for loop (for example, for line in f: print line), the next()
72+ method is called repeatedly. This method returns the next input line,
73+ or raises StopIteration when EOF is hit.
74+ """
6975 if self .closed :
7076 raise StopIteration
7177 r = self .readline ()
@@ -81,10 +87,21 @@ def close(self):
8187 del self .buf , self .pos
8288
8389 def isatty (self ):
90+ """Returns False because StringIO objects are not connected to a
91+ tty-like device.
92+ """
8493 _complain_ifclosed (self .closed )
8594 return False
8695
8796 def seek (self , pos , mode = 0 ):
97+ """Set the file's current position.
98+
99+ The mode argument is optional and defaults to 0 (absolute file
100+ positioning); other values are 1 (seek relative to the current
101+ position) and 2 (seek relative to the file's end).
102+
103+ There is no return value.
104+ """
88105 _complain_ifclosed (self .closed )
89106 if self .buflist :
90107 self .buf += '' .join (self .buflist )
@@ -96,10 +113,18 @@ def seek(self, pos, mode = 0):
96113 self .pos = max (0 , pos )
97114
98115 def tell (self ):
116+ """Return the file's current position."""
99117 _complain_ifclosed (self .closed )
100118 return self .pos
101119
102120 def read (self , n = - 1 ):
121+ """Read at most size bytes from the file
122+ (less if the read hits EOF before obtaining size bytes).
123+
124+ If the size argument is negative or omitted, read all data until EOF
125+ is reached. The bytes are returned as a string object. An empty
126+ string is returned when EOF is encountered immediately.
127+ """
103128 _complain_ifclosed (self .closed )
104129 if self .buflist :
105130 self .buf += '' .join (self .buflist )
@@ -113,6 +138,18 @@ def read(self, n = -1):
113138 return r
114139
115140 def readline (self , length = None ):
141+ """Read one entire line from the file.
142+
143+ A trailing newline character is kept in the string (but may be absent
144+ when a file ends with an incomplete line). If the size argument is
145+ present and non-negative, it is a maximum byte count (including the
146+ trailing newline) and an incomplete line may be returned.
147+
148+ An empty string is returned only when EOF is encountered immediately.
149+
150+ Note: Unlike stdio's fgets(), the returned string contains null
151+ characters ('\0 ') if they occurred in the input.
152+ """
116153 _complain_ifclosed (self .closed )
117154 if self .buflist :
118155 self .buf += '' .join (self .buflist )
@@ -130,6 +167,13 @@ def readline(self, length=None):
130167 return r
131168
132169 def readlines (self , sizehint = 0 ):
170+ """Read until EOF using readline() and return a list containing the
171+ lines thus read.
172+
173+ If the optional sizehint argument is present, instead of reading up
174+ to EOF, whole lines totalling approximately sizehint bytes (or more
175+ to accommodate a final whole line).
176+ """
133177 total = 0
134178 lines = []
135179 line = self .readline ()
@@ -142,6 +186,16 @@ def readlines(self, sizehint = 0):
142186 return lines
143187
144188 def truncate (self , size = None ):
189+ """Truncate the file's size.
190+
191+ If the optional size argument is present, the file is truncated to
192+ (at most) that size. The size defaults to the current position.
193+ The current file position is not changed unless the position
194+ is beyond the new file size.
195+
196+ If the specified size exceeds the file's current size, the
197+ file remains unchanged.
198+ """
145199 _complain_ifclosed (self .closed )
146200 if size is None :
147201 size = self .pos
@@ -152,6 +206,10 @@ def truncate(self, size=None):
152206 self .buf = self .getvalue ()[:size ]
153207
154208 def write (self , s ):
209+ """Write a string to the file.
210+
211+ There is no return value.
212+ """
155213 _complain_ifclosed (self .closed )
156214 if not s : return
157215 # Force s to be a string or unicode
@@ -179,11 +237,20 @@ def write(self, s):
179237 self .pos = newpos
180238
181239 def writelines (self , iterable ):
240+ """Write a sequence of strings to the file. The sequence can be any
241+ iterable object producing strings, typically a list of strings. There
242+ is no return value.
243+
244+ (The name is intended to match readlines(); writelines() does not add
245+ line separators.)
246+ """
182247 write = self .write
183248 for line in iterable :
184249 write (line )
185250
186251 def flush (self ):
252+ """Flush the internal buffer
253+ """
187254 _complain_ifclosed (self .closed )
188255
189256 def getvalue (self ):
0 commit comments