Actually, requiring isinstance(pos, int) is too rigid. You should use __index__() instead, which allows other int-like numbers but not float numbers.
On Nov 8, 2007 10:04 AM, christian.heimes <[email protected]> wrote: > Author: christian.heimes > Date: Thu Nov 8 19:04:45 2007 > New Revision: 58914 > > Modified: > python/branches/py3k/Lib/io.py > python/branches/py3k/Lib/test/test_io.py > python/branches/py3k/Modules/_fileio.c > Log: > Fixed bug #1081: file.seek allows float arguments > > Modified: python/branches/py3k/Lib/io.py > ============================================================================== > --- python/branches/py3k/Lib/io.py (original) > +++ python/branches/py3k/Lib/io.py Thu Nov 8 19:04:45 2007 > @@ -694,6 +694,8 @@ > 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: > > Modified: python/branches/py3k/Lib/test/test_io.py > ============================================================================== > --- python/branches/py3k/Lib/test/test_io.py (original) > +++ python/branches/py3k/Lib/test/test_io.py Thu Nov 8 19:04:45 2007 > @@ -95,6 +95,7 @@ > 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 @@ > 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 @@ > > 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 @@ > 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): > > Modified: python/branches/py3k/Modules/_fileio.c > ============================================================================== > --- python/branches/py3k/Modules/_fileio.c (original) > +++ python/branches/py3k/Modules/_fileio.c Thu Nov 8 19:04:45 2007 > @@ -556,6 +556,10 @@ > 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 > _______________________________________________ > Python-3000-checkins mailing list > [email protected] > http://mail.python.org/mailman/listinfo/python-3000-checkins > -- --Guido van Rossum (home page: http://www.python.org/~guido/) _______________________________________________ Python-3000-checkins mailing list [email protected] http://mail.python.org/mailman/listinfo/python-3000-checkins
