]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fixed bug #1081: file.seek allows float arguments
authorChristian Heimes <christian@cheimes.de>
Thu, 8 Nov 2007 18:04:45 +0000 (18:04 +0000)
committerChristian Heimes <christian@cheimes.de>
Thu, 8 Nov 2007 18:04:45 +0000 (18:04 +0000)
Lib/io.py
Lib/test/test_io.py
Modules/_fileio.c

index c2f5d3ebfaa4ed5e3750188f18064b6c8fbe9d6e..d9550ae54a8a57a45d56cb0258d2611f05831b99 100644 (file)
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -694,6 +694,8 @@ class BytesIO(BufferedIOBase):
         return n
 
     def seek(self, pos, whence=0):
+        if not isinstance(pos, int):
+            raise TypeError("an integer is required")
         if whence == 0:
             self._pos = max(0, pos)
         elif whence == 1:
index 6091e89497a8557d581bc1a17b2cacf9250d6d86..dace6427cdf620d5155f3b9861aa1ae4c4a96278 100644 (file)
@@ -95,6 +95,7 @@ class IOTest(unittest.TestCase):
         self.assertEqual(f.tell(), 13)
         self.assertEqual(f.truncate(12), 12)
         self.assertEqual(f.tell(), 13)
+        self.assertRaises(TypeError, f.seek, 0.0)
 
     def read_ops(self, f, buffered=False):
         data = f.read(5)
@@ -116,6 +117,7 @@ class IOTest(unittest.TestCase):
         self.assertEqual(f.seek(-6, 1), 5)
         self.assertEqual(f.read(5), b" worl")
         self.assertEqual(f.tell(), 10)
+        self.assertRaises(TypeError, f.seek, 0.0)
         if buffered:
             f.seek(0)
             self.assertEqual(f.read(), b"hello world\n")
@@ -296,6 +298,7 @@ class MemorySeekTestMixin:
 
         bytesIo.seek(3)
         self.assertEquals(buf[3:], bytesIo.read())
+        self.assertRaises(TypeError, bytesIo.seek, 0.0)
 
     def testTell(self):
         buf = self.buftype("1234567890")
@@ -481,6 +484,7 @@ class BufferedRandomTest(unittest.TestCase):
         rw.seek(2, 1)
         self.assertEquals(7, rw.tell())
         self.assertEquals(b"fl", rw.read(11))
+        self.assertRaises(TypeError, rw.seek, 0.0)
 
 
 class TextIOWrapperTest(unittest.TestCase):
index c357a73b3bfd371cf04cd8f28c662aec88176be0..0fd8b6685716a01382626c02d47356edfdaf2030 100644 (file)
@@ -556,6 +556,10 @@ portable_lseek(int fd, PyObject *posobj, int whence)
        if (posobj == NULL)
                pos = 0;
        else {
+               if(PyFloat_Check(posobj)) {
+                       PyErr_SetString(PyExc_TypeError, "an integer is required");
+                       return NULL;
+               }
 #if !defined(HAVE_LARGEFILE_SUPPORT)
                pos = PyInt_AsLong(posobj);
 #else