[Python-checkins] r57263 - python/branches/alex-py3k/Lib/io.py

alexandre.vassalotti python-checkins at python.org
Tue Aug 21 20:21:55 CEST 2007


Author: alexandre.vassalotti
Date: Tue Aug 21 20:21:54 2007
New Revision: 57263

Modified:
   python/branches/alex-py3k/Lib/io.py
Log:
Change truncate() behaviour to imply a seek to the indicated position
if an argument is present.
Convert str to bytes in ByteIO.__init__().
Make the close() method of BytesIO and StringIO do nothing.
Make write() do nothing if the argument length is zero.
Raise a ValueError if a negative argument is used for an absolute
seek.
Raise a ValueError if a negative argument is given to truncate().
Raise a ValueError instead of an IOError if whence is invalid, in
seek().
In readlines() and readline(), consider a negative argument to as None.
In TextIOWrapper, get the value for writeable(), readable() and
seekable() from the underlying buffer.


Modified: python/branches/alex-py3k/Lib/io.py
==============================================================================
--- python/branches/alex-py3k/Lib/io.py	(original)
+++ python/branches/alex-py3k/Lib/io.py	Tue Aug 21 20:21:54 2007
@@ -364,7 +364,7 @@
         return line
 
     def readlines(self, hint=None):
-        if hint is None:
+        if hint is None or hint <= 0:
             return list(self)
         n = 0
         lines = []
@@ -452,6 +452,11 @@
         _fileio._FileIO.close(self)
         RawIOBase.close(self)
 
+    def truncate(self, pos=None):
+        if pos is not None:
+            _fileio._FileIO.seek(self, pos)
+        return _fileio._FileIO.truncate(self)
+
     @property
     def name(self):
         return self._name
@@ -604,11 +609,16 @@
 
     def __init__(self, initial_bytes=None):
         buffer = b""
-        if initial_bytes is not None:
+        if isinstance(initial_bytes, str):
+            buffer += bytes(initial_bytes)
+        elif initial_bytes is not None:
             buffer += initial_bytes
         self._buffer = buffer
         self._pos = 0
 
+    def close(self):
+        pass
+
     def getvalue(self):
         return self._buffer
 
@@ -617,6 +627,8 @@
             n = -1
         if n < 0:
             n = len(self._buffer)
+        if len(self._buffer) <= self._pos:
+            return self._buffer[:0]
         newpos = min(len(self._buffer), self._pos + n)
         b = self._buffer[self._pos : newpos]
         self._pos = newpos
@@ -626,9 +638,9 @@
         return self.read(n)
 
     def write(self, b):
-        if self.closed:
-            raise ValueError("write to closed file")
         n = len(b)
+        if n == 0:
+            return 0
         newpos = self._pos + n
         if newpos > len(self._buffer):
             # Inserts null bytes between the current end of the file
@@ -641,13 +653,16 @@
 
     def seek(self, pos, whence=0):
         if whence == 0:
-            self._pos = max(0, pos)
+            if pos < 0:
+                raise ValueError("Negative seek position %r" % (pos,))
+            self._pos = pos
         elif whence == 1:
             self._pos = max(0, self._pos + pos)
         elif whence == 2:
             self._pos = max(0, len(self._buffer) + pos)
         else:
-            raise IOError("invalid whence value")
+            raise ValueError("Invalid whence (%r, should be 0, 1 or 2)" %
+                             (whence,))
         return self._pos
 
     def tell(self):
@@ -656,8 +671,10 @@
     def truncate(self, pos=None):
         if pos is None:
             pos = self._pos
+        elif pos < 0:
+            raise ValueError("invalid position value")
         del self._buffer[pos:]
-        return pos
+        return self.seek(pos)
 
     def readable(self):
         return True
@@ -1066,6 +1083,15 @@
     def isatty(self):
         return self.buffer.isatty()
 
+    def writable(self):
+        return self.buffer.writable()
+
+    def readable(self):
+        return self.buffer.readable()
+
+    def seekable(self):
+        return self.buffer.seekable()
+
     def write(self, s: str):
         if self.closed:
             raise ValueError("write to closed file")
@@ -1221,7 +1247,7 @@
         if limit is not None:
             # XXX Hack to support limit argument, for backwards compatibility
             line = self.readline()
-            if len(line) <= limit:
+            if len(line) <= limit or limit < 0:
                 return line
             line, self._pending = line[:limit], line[limit:] + self._pending
             return line


More information about the Python-checkins mailing list