]> granicus.if.org Git - python/commitdiff
Make a few more tests pass with the new I/O library.
authorGuido van Rossum <guido@python.org>
Thu, 12 Apr 2007 22:55:07 +0000 (22:55 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 12 Apr 2007 22:55:07 +0000 (22:55 +0000)
Fix the truncate() semantics -- it should not affect the current position.
Switch wave.py/chunk.py to struct.unpack_from() to support bytes.
Don't use writelines() on binary files (test_fileinput.py).

Lib/chunk.py
Lib/io.py
Lib/test/test_fileinput.py
Lib/test/test_io.py
Lib/wave.py
Modules/_fileio.c

index a8fbc1051f3e44794427408fa0c4dbc89b3c9103..32aada8aef4f2c2679143d2e90043aec3efb407e 100644 (file)
@@ -62,7 +62,7 @@ class Chunk:
         if len(self.chunkname) < 4:
             raise EOFError
         try:
-            self.chunksize = struct.unpack(strflag+'L', file.read(4))[0]
+            self.chunksize = struct.unpack_from(strflag+'L', file.read(4))[0]
         except struct.error:
             raise EOFError
         if inclheader:
index 4465e9e9e55cdf27c4290f433c9a1caf9a397bef..8b5c958691ed4b08f45d57a53d8c2cbe34c176af 100644 (file)
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -551,8 +551,6 @@ class _MemoryIOMixin(BufferedIOBase):
     def truncate(self, pos=None):
         if pos is None:
             pos = self._pos
-        else:
-            self._pos = max(0, pos)
         del self._buffer[pos:]
         return pos
 
index 2b7b255dae9a447f5eb6668535785adc03590ab2..b7f84c686af29a598e1b2d91fa8420d6a5c3d996 100644 (file)
@@ -18,7 +18,8 @@ from fileinput import FileInput, hook_encoded
 def writeTmp(i, lines, mode='w'):  # opening in text mode is the default
     name = TESTFN + str(i)
     f = open(name, mode)
-    f.writelines(lines)
+    for line in lines:
+        f.write(line)
     f.close()
     return name
 
index 737dfab36bd734b2f2a9367fb295ffaa550cb80b..1f6be02ca5f775d9798eb3402790a816a6efbd9f 100644 (file)
@@ -93,7 +93,7 @@ class IOTest(unittest.TestCase):
         self.assertEqual(f.seek(-1, 2), 13)
         self.assertEqual(f.tell(), 13)
         self.assertEqual(f.truncate(12), 12)
-        self.assertEqual(f.tell(), 12)
+        self.assertEqual(f.tell(), 13)
 
     def read_ops(self, f, buffered=False):
         data = f.read(5)
@@ -135,7 +135,7 @@ class IOTest(unittest.TestCase):
         self.assertEqual(f.tell(), self.LARGE + 2)
         self.assertEqual(f.seek(0, 2), self.LARGE + 2)
         self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
-        self.assertEqual(f.tell(), self.LARGE + 1)
+        self.assertEqual(f.tell(), self.LARGE + 2)
         self.assertEqual(f.seek(0, 2), self.LARGE + 1)
         self.assertEqual(f.seek(-1, 2), self.LARGE)
         self.assertEqual(f.read(2), b"x")
index 08c51ba90c163cb1237ae51b9babca2f539337ff..dd5a47a6eaf5c5997b09c9d5943ba68e9d17841c 100644 (file)
@@ -256,9 +256,9 @@ class Wave_read:
     #
 
     def _read_fmt_chunk(self, chunk):
-        wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<hhllh', chunk.read(14))
+        wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<hhllh', chunk.read(14))
         if wFormatTag == WAVE_FORMAT_PCM:
-            sampwidth = struct.unpack('<h', chunk.read(2))[0]
+            sampwidth = struct.unpack_from('<h', chunk.read(2))[0]
             self._sampwidth = (sampwidth + 7) // 8
         else:
             raise Error, 'unknown format: %r' % (wFormatTag,)
index 97b219904b2f1b51a1aa2c02e7bde770deb3e51f..d524aa7ae8ab508d74badc5375bae593c6ef6978 100644 (file)
@@ -519,7 +519,7 @@ fileio_truncate(PyFileIOObject *self, PyObject *args)
 {
        PyObject *posobj = NULL;
        Py_off_t pos;
-       int fd, whence;
+       int fd;
 
        fd = self->fd;
        if (fd < 0)
@@ -530,17 +530,14 @@ fileio_truncate(PyFileIOObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "|O", &posobj))
                return NULL;
 
-       if (posobj == Py_None)
-               posobj = NULL;
-
-       if (posobj == NULL)
-               whence = 1;
-       else
-               whence = 0;
-
-       posobj = portable_lseek(fd, posobj, whence);
-       if (posobj == NULL)
-               return NULL;
+       if (posobj == Py_None || posobj == NULL) {
+                posobj = portable_lseek(fd, NULL, 1);
+                if (posobj == NULL)
+                  return NULL;
+        }
+        else {
+               Py_INCREF(posobj);
+        }
 
 #if !defined(HAVE_LARGEFILE_SUPPORT)
        pos = PyInt_AsLong(posobj);