]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 70352 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 13 Mar 2009 23:46:17 +0000 (23:46 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 13 Mar 2009 23:46:17 +0000 (23:46 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r70352 | antoine.pitrou | 2009-03-14 00:42:55 +0100 (sam., 14 mars 2009) | 4 lines

  Issue #5016: FileIO.seekable() could return False if the file position
  was negative when truncated to a C int. Patch by Victor Stinner.
........

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

index 8060ea04e85526ac4cfcf9fed10a74182a7ce01b..8cb4a176300b59fbe20350c669398830ff4e7c77 100644 (file)
@@ -131,6 +131,15 @@ class TestCase(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 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
@@ -165,6 +174,7 @@ def test_main():
     with open(TESTFN, 'w') as f:
         if hasattr(f, 'truncate'):
             suite.addTest(TestCase('test_truncate'))
+    suite.addTest(TestCase('test_seekable'))
     unlink(TESTFN)
     try:
         run_unittest(suite)
index 97b8fd1d9720b849f245956017b111369fcfa9d9..b0758046418f74bc1e65e306293cea208e10371f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5016: FileIO.seekable() could return False if the file position
+  was negative when truncated to a C int. Patch by Victor Stinner.
+
 - Issue #5179: Fixed subprocess handle leak on failure on windows.
 
 - Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
index 7fe37ec657c3d4d33556668924f5c48612a5619f..bee19fe511eed612c7955266fd87954f402b9c1c 100644 (file)
@@ -58,6 +58,8 @@ PyTypeObject PyFileIO_Type;
 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)
@@ -396,14 +398,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);
 }