diff -r 81b2a30da853 Doc/library/io.rst --- a/Doc/library/io.rst Sun Jan 20 16:35:09 2013 +0200 +++ b/Doc/library/io.rst Sun Jan 20 19:09:01 2013 +0200 @@ -281,10 +281,10 @@ Return ``True`` if the stream can be read from. If False, :meth:`read` will raise :exc:`OSError`. - .. method:: readline(limit=-1) + .. method:: readline(size=-1) - Read and return one line from the stream. If *limit* is specified, at - most *limit* bytes will be read. + Read and return one line from the stream. If *size* is specified, at + most *size* bytes will be read. The line terminator is always ``b'\n'`` for binary files; for text files, the *newlines* argument to :func:`open` can be used to select the line @@ -361,14 +361,14 @@ In addition to the attributes and methods from :class:`IOBase`, :class:`RawIOBase` provides the following methods: - .. method:: read(n=-1) + .. method:: read(size=-1) - Read up to *n* bytes from the object and return them. As a convenience, - if *n* is unspecified or -1, :meth:`readall` is called. Otherwise, - only one system call is ever made. Fewer than *n* bytes may be - returned if the operating system call returns fewer than *n* bytes. + Read up to *size* bytes from the object and return them. As a convenience, + if *size* is unspecified or -1, :meth:`readall` is called. Otherwise, + only one system call is ever made. Fewer than *size* bytes may be + returned if the operating system call returns fewer than *size* bytes. - If 0 bytes are returned, and *n* was not 0, this indicates end of file. + If 0 bytes are returned, and *size* was not 0, this indicates end of file. If the object is in non-blocking mode and no bytes are available, ``None`` is returned. @@ -437,10 +437,10 @@ .. versionadded:: 3.1 - .. method:: read(n=-1) + .. method:: read(size=-1) - Read and return up to *n* bytes. If the argument is omitted, ``None``, or - negative, data is read and returned until EOF is reached. An empty + Read and return up to *size* bytes. If the argument is omitted, ``None``, + or negative, data is read and returned until EOF is reached. An empty :class:`bytes` object is returned if the stream is already at EOF. If the argument is positive, and the underlying raw stream is not @@ -452,9 +452,9 @@ A :exc:`BlockingIOError` is raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment. - .. method:: read1(n=-1) + .. method:: read1(size=-1) - Read and return up to *n* bytes, with at most one call to the underlying + Read and return up to *size* bytes, with at most one call to the underlying raw stream's :meth:`~RawIOBase.read` method. This can be useful if you are implementing your own buffering on top of a :class:`BufferedIOBase` object. @@ -596,21 +596,21 @@ :class:`BufferedReader` provides or overrides these methods in addition to those from :class:`BufferedIOBase` and :class:`IOBase`: - .. method:: peek([n]) + .. method:: peek([size]) Return bytes from the stream without advancing the position. At most one single read on the raw stream is done to satisfy the call. The number of bytes returned may be less or more than requested. - .. method:: read([n]) + .. method:: read([size]) - Read and return *n* bytes, or if *n* is not given or negative, until EOF - or if the read call would block in non-blocking mode. + Read and return *size* bytes, or if *size* is not given or negative, until + EOF or if the read call would block in non-blocking mode. - .. method:: read1(n) + .. method:: read1(size) - Read and return up to *n* bytes with only one call on the raw stream. If - at least one byte is buffered, only buffered bytes are returned. + Read and return up to *size* bytes with only one call on the raw stream. + If at least one byte is buffered, only buffered bytes are returned. Otherwise, one raw stream read call is made. @@ -729,17 +729,17 @@ .. versionadded:: 3.1 - .. method:: read(n) + .. method:: read(size) - Read and return at most *n* characters from the stream as a single - :class:`str`. If *n* is negative or ``None``, reads until EOF. + Read and return at most *size* characters from the stream as a single + :class:`str`. If *size* is negative or ``None``, reads until EOF. - .. method:: readline(limit=-1) + .. method:: readline(size=-1) Read until newline or EOF and return a single ``str``. If the stream is already at EOF, an empty string is returned. - If *limit* is specified, at most *limit* characters will be read. + If *size* is specified, at most *size* characters will be read. .. method:: seek(offset, whence=SEEK_SET) diff -r 81b2a30da853 Lib/_pyio.py --- a/Lib/_pyio.py Sun Jan 20 16:35:09 2013 +0200 +++ b/Lib/_pyio.py Sun Jan 20 19:09:01 2013 +0200 @@ -320,7 +320,7 @@ """Return an int indicating the current stream position.""" return self.seek(0, 1) - def truncate(self, pos=None): + def truncate(self, size=None): """Truncate file to size bytes. Size defaults to the current IO position as reported by tell(). Return @@ -455,11 +455,11 @@ ### Readline[s] and writelines ### - def readline(self, limit=-1): + def readline(self, size=-1): r"""Read and return a line of bytes from the stream. - If limit is specified, at most limit bytes will be read. - Limit should be an int. + If size is specified, at most size bytes will be read. + Size should be an int. The line terminator is always b'\n' for binary files; for text files, the newlines argument to open can be used to select the line @@ -472,18 +472,18 @@ if not readahead: return 1 n = (readahead.find(b"\n") + 1) or len(readahead) - if limit >= 0: - n = min(n, limit) + if size >= 0: + n = min(n, size) return n else: def nreadahead(): return 1 - if limit is None: - limit = -1 - elif not isinstance(limit, int): - raise TypeError("limit must be an integer") + if size is None: + size = -1 + elif not isinstance(size, int): + raise TypeError("size must be an integer") res = bytearray() - while limit < 0 or len(res) < limit: + while size < 0 or len(res) < size: b = self.read(nreadahead()) if not b: break @@ -542,17 +542,17 @@ # primitive operation, but that would lead to nasty recursion in case # a subclass doesn't implement either.) - def read(self, n=-1): - """Read and return up to n bytes, where n is an int. + def read(self, size=-1): + """Read and return up to size bytes, where size is an int. Returns an empty bytes object on EOF, or None if the object is set not to block and has no data to read. """ - if n is None: - n = -1 - if n < 0: + if size is None: + size = -1 + if size < 0: return self.readall() - b = bytearray(n.__index__()) + b = bytearray(size.__index__()) n = self.readinto(b) if n is None: return None @@ -610,8 +610,8 @@ implementation, but wrap one. """ - def read(self, n=None): - """Read and return up to n bytes, where n is an int. + def read(self, size=None): + """Read and return up to size bytes, where size is an int. If the argument is omitted, None, or negative, reads and returns all data until EOF. @@ -630,9 +630,9 @@ """ self._unsupported("read") - def read1(self, n=None): - """Read up to n bytes with at most one read() system call, - where n is an int. + def read1(self, size=None): + """Read up to size bytes with at most one read() system call, + where size is an int. """ self._unsupported("read1") @@ -708,17 +708,17 @@ raise OSError("tell() returned an invalid position") return pos - def truncate(self, pos=None): + def truncate(self, size=None): # Flush the stream. We're mixing buffered I/O with lower-level I/O, # and a flush may be necessary to synch both views of the current # file state. self.flush() - if pos is None: - pos = self.tell() + if size is None: + size = self.tell() # XXX: Should seek() be used, instead of passing the position # XXX directly to truncate? - return self.raw.truncate(pos) + return self.raw.truncate(size) ### Flush and close ### @@ -820,24 +820,24 @@ """ return memoryview(self._buffer) - def read(self, n=None): + def read(self, size=None): if self.closed: raise ValueError("read from closed file") - if n is None: - n = -1 - if n < 0: - n = len(self._buffer) + if size is None: + size = -1 + if size < 0: + size = len(self._buffer) if len(self._buffer) <= self._pos: return b"" - newpos = min(len(self._buffer), self._pos + n) + newpos = min(len(self._buffer), self._pos + size) b = self._buffer[self._pos : newpos] self._pos = newpos return bytes(b) - def read1(self, n): + def read1(self, size): """This is the same as read. """ - return self.read(n) + return self.read(size) def write(self, b): if self.closed: @@ -881,20 +881,20 @@ raise ValueError("tell on closed file") return self._pos - def truncate(self, pos=None): + def truncate(self, size=None): if self.closed: raise ValueError("truncate on closed file") - if pos is None: - pos = self._pos + if size is None: + size = self._pos else: try: - pos.__index__ + size.__index__ except AttributeError as err: raise TypeError("an integer is required") from err - if pos < 0: - raise ValueError("negative truncate position %r" % (pos,)) - del self._buffer[pos:] - return pos + if size < 0: + raise ValueError("negative truncate position %r" % (size,)) + del self._buffer[size:] + return size def readable(self): if self.closed: @@ -940,18 +940,18 @@ self._read_buf = b"" self._read_pos = 0 - def read(self, n=None): - """Read n bytes. + def read(self, size=None): + """Read size bytes. - Returns exactly n bytes of data unless the underlying raw IO + Returns exactly size bytes of data unless the underlying raw IO stream reaches EOF or if the call would block in non-blocking - mode. If n is negative, read until EOF or until read() would + mode. If size is negative, read until EOF or until read() would block. """ - if n is not None and n < -1: + if size is not None and size < -1: raise ValueError("invalid number of bytes to read") with self._read_lock: - return self._read_unlocked(n) + return self._read_unlocked(size) def _read_unlocked(self, n=None): nodata_val = b"" @@ -1011,7 +1011,7 @@ self._read_pos = 0 return out[:n] if out else nodata_val - def peek(self, n=0): + def peek(self, size=0): """Returns buffered bytes without advancing the position. The argument indicates a desired minimal number of bytes; we @@ -1019,7 +1019,7 @@ than self.buffer_size. """ with self._read_lock: - return self._peek_unlocked(n) + return self._peek_unlocked(size) def _peek_unlocked(self, n=0): want = min(n, self.buffer_size) @@ -1037,18 +1037,18 @@ self._read_pos = 0 return self._read_buf[self._read_pos:] - def read1(self, n): - """Reads up to n bytes, with at most one read() system call.""" - # Returns up to n bytes. If at least one byte is buffered, we + def read1(self, size): + """Reads up to size bytes, with at most one read() system call.""" + # Returns up to size bytes. If at least one byte is buffered, we # only return buffered bytes. Otherwise, we do one raw read. - if n < 0: + if size < 0: raise ValueError("number of bytes to read must be positive") - if n == 0: + if size == 0: return b"" with self._read_lock: self._peek_unlocked(1) return self._read_unlocked( - min(n, len(self._read_buf) - self._read_pos)) + min(size, len(self._read_buf) - self._read_pos)) def tell(self): return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos @@ -1111,12 +1111,12 @@ raise BlockingIOError(e.errno, e.strerror, written) return written - def truncate(self, pos=None): + def truncate(self, size=None): with self._write_lock: self._flush_unlocked() - if pos is None: - pos = self.raw.tell() - return self.raw.truncate(pos) + if size is None: + size = self.raw.tell() + return self.raw.truncate(size) def flush(self): with self._write_lock: @@ -1182,10 +1182,10 @@ self.reader = BufferedReader(reader, buffer_size) self.writer = BufferedWriter(writer, buffer_size) - def read(self, n=None): - if n is None: - n = -1 - return self.reader.read(n) + def read(self, size=None): + if size is None: + size = -1 + return self.reader.read(size) def readinto(self, b): return self.reader.readinto(b) @@ -1193,11 +1193,11 @@ def write(self, b): return self.writer.write(b) - def peek(self, n=0): - return self.reader.peek(n) + def peek(self, size=0): + return self.reader.peek(size) - def read1(self, n): - return self.reader.read1(n) + def read1(self, size): + return self.reader.read1(size) def readable(self): return self.reader.readable() @@ -1257,29 +1257,29 @@ else: return BufferedReader.tell(self) - def truncate(self, pos=None): - if pos is None: - pos = self.tell() + def truncate(self, size=None): + if size is None: + size = self.tell() # Use seek to flush the read buffer. - return BufferedWriter.truncate(self, pos) + return BufferedWriter.truncate(self, size) - def read(self, n=None): - if n is None: - n = -1 + def read(self, size=None): + if size is None: + size = -1 self.flush() - return BufferedReader.read(self, n) + return BufferedReader.read(self, size) def readinto(self, b): self.flush() return BufferedReader.readinto(self, b) - def peek(self, n=0): + def peek(self, size=0): self.flush() - return BufferedReader.peek(self, n) + return BufferedReader.peek(self, size) - def read1(self, n): + def read1(self, size): self.flush() - return BufferedReader.read1(self, n) + return BufferedReader.read1(self, size) def write(self, b): if self._read_buf: @@ -1299,11 +1299,11 @@ are immutable. There is no public constructor. """ - def read(self, n=-1): - """Read at most n characters from stream, where n is an int. + def read(self, size=-1): + """Read at most size characters from stream, where size is an int. - Read from underlying buffer until we have n characters or we hit EOF. - If n is negative or omitted, read until EOF. + Read from underlying buffer until we have size characters or we hit EOF. + If size is negative or omitted, read until EOF. Returns a string. """ @@ -1313,8 +1313,8 @@ """Write string s to stream and returning an int.""" self._unsupported("write") - def truncate(self, pos=None): - """Truncate size to pos, where pos is an int.""" + def truncate(self, size=None): + """Truncate size to size, where size is an int.""" self._unsupported("truncate") def readline(self): @@ -1822,11 +1822,11 @@ finally: decoder.setstate(saved_state) - def truncate(self, pos=None): + def truncate(self, size=None): self.flush() - if pos is None: - pos = self.tell() - return self.buffer.truncate(pos) + if size is None: + size = self.tell() + return self.buffer.truncate(size) def detach(self): if self.buffer is None: @@ -1907,16 +1907,16 @@ encoder.reset() return cookie - def read(self, n=None): + def read(self, size=None): self._checkReadable() - if n is None: - n = -1 + if size is None: + size = -1 decoder = self._decoder or self._get_decoder() try: - n.__index__ + size.__index__ except AttributeError as err: raise TypeError("an integer is required") from err - if n < 0: + if size < 0: # Read everything. result = (self._get_decoded_chars() + decoder.decode(self.buffer.read(), final=True)) @@ -1924,12 +1924,12 @@ self._snapshot = None return result else: - # Keep reading chunks until we have n characters to return. + # Keep reading chunks until we have size characters to return. eof = False - result = self._get_decoded_chars(n) - while len(result) < n and not eof: + result = self._get_decoded_chars(size) + while len(result) < size and not eof: eof = not self._read_chunk() - result += self._get_decoded_chars(n - len(result)) + result += self._get_decoded_chars(size - len(result)) return result def __next__(self): @@ -1941,13 +1941,13 @@ raise StopIteration return line - def readline(self, limit=None): + def readline(self, size=None): if self.closed: raise ValueError("read from closed file") - if limit is None: - limit = -1 - elif not isinstance(limit, int): - raise TypeError("limit must be an integer") + if size is None: + size = -1 + elif not isinstance(size, int): + raise TypeError("size must be an integer") # Grab all the decoded text (we will rewind any extra bits later). line = self._get_decoded_chars() @@ -2006,8 +2006,8 @@ endpos = pos + len(self._readnl) break - if limit >= 0 and len(line) >= limit: - endpos = limit # reached length limit + if size >= 0 and len(line) >= size: + endpos = size # reached length size break # No line ending seen yet - get more data' @@ -2022,8 +2022,8 @@ self._snapshot = None return line - if limit >= 0 and endpos > limit: - endpos = limit # don't exceed limit + if size >= 0 and endpos > size: + endpos = size # don't exceed size # Rewind _decoded_chars to just after the line ending we found. self._rewind_decoded_chars(len(line) - endpos)