]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #5016: FileIO.seekable() could return False if the file position
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 13 Mar 2009 23:42:55 +0000 (23:42 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 13 Mar 2009 23:42:55 +0000 (23:42 +0000)
was negative when truncated to a C int. Patch by Victor Stinner.

Lib/test/test_largefile.py
Misc/NEWS
Modules/_fileio.c

index 584a20623fd51543f6a81d6f747334503d034928..d9326593a3f8ea3ce59361d895fe0ca5b25c9f5e 100644 (file)
@@ -133,6 +133,15 @@ class LargeFileTest(unittest.TestCase):
             f.seek(0)
             self.assertEqual(len(f.read()), 1)  # else wasn't truncated
 
+    def test_seekable(self):
+        # Issue #5016; seekable() can return False when the current position
+        # is negative when truncated to an int.
+        for pos in (2**31-1, 2**31, 2**31+1):
+            with self.open(TESTFN, 'rb') as f:
+                f.seek(pos)
+                self.assert_(f.seekable())
+
+
 def test_main():
     # On Windows and Mac OSX this test comsumes large resources; It
     # takes a long time to build the >2GB file and takes >2GB of disk
@@ -172,6 +181,7 @@ def test_main():
         with _open(TESTFN, 'wb') as f:
             if hasattr(f, 'truncate'):
                 suite.addTest(TestCase('test_truncate'))
+        suite.addTest(TestCase('test_seekable'))
         unlink(TESTFN)
     try:
         run_unittest(suite)
index 21b4a9b993f904e059d20d1061bf56faac981825..d1b1463aa2295f96a8e38cd54c8a867de5cf6c96 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@ Library
 - The error detection code in FileIO.close() could fail to reflect the `errno`
   value, and report it as -1 instead.
 
+- Issue #5016: FileIO.seekable() could return False if the file position
+  was negative when truncated to a C int. Patch by Victor Stinner.
+
 
 What's New in Python 3.1 alpha 1
 ================================
index 32f679007aae3b998b8185b54e2526510950c3b7..88ee54c5f809b7c2b5a507083cb0730beffd69f5 100644 (file)
@@ -66,6 +66,8 @@ _PyFileIO_closed(PyObject *self)
 static PyObject *
 portable_lseek(int fd, PyObject *posobj, int whence);
 
+static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
+
 /* Returns 0 on success, -1 with exception set on failure. */
 static int
 internal_close(PyFileIOObject *self)
@@ -441,14 +443,14 @@ fileio_seekable(PyFileIOObject *self)
        if (self->fd < 0)
                return err_closed();
        if (self->seekable < 0) {
-               int ret;
-               Py_BEGIN_ALLOW_THREADS
-               ret = lseek(self->fd, 0, SEEK_CUR);
-               Py_END_ALLOW_THREADS
-               if (ret < 0)
+               PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
+               if (pos == NULL) {
+                       PyErr_Clear();
                        self->seekable = 0;
-               else
+               } else {
+                       Py_DECREF(pos);
                        self->seekable = 1;
+               }
        }
        return PyBool_FromLong((long) self->seekable);
 }