]> granicus.if.org Git - python/commitdiff
Closes #10142: Support for SEEK_HOLE/SEEK_DATA
authorJesus Cea <jcea@jcea.es>
Fri, 22 Jun 2012 16:32:07 +0000 (18:32 +0200)
committerJesus Cea <jcea@jcea.es>
Fri, 22 Jun 2012 16:32:07 +0000 (18:32 +0200)
Doc/library/io.rst
Doc/library/os.rst
Lib/_pyio.py
Lib/os.py
Lib/test/test_io.py
Lib/test/test_posix.py
Misc/NEWS
Modules/_io/bufferedio.c
Modules/posixmodule.c

index e30a016d9605ef1c457fb75363500a7dfbabbcb4..1d3d7028343c63ebbcee3018823425e0a4095bc6 100644 (file)
@@ -291,6 +291,11 @@ I/O Base Classes
       .. versionadded:: 3.1
          The ``SEEK_*`` constants.
 
+      .. versionadded:: 3.3
+         Some operating systems could support additional values, like
+         :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`. The valid values
+         for a file could depend on it being open in text or binary mode.
+
    .. method:: seekable()
 
       Return ``True`` if the stream supports random access.  If ``False``,
index 28cf4b4ac0369f27474910a404b8b7a9a263bfd5..0be5595cd8b1da72ebd3e5491eff0fbb4c3313c5 100644 (file)
@@ -995,6 +995,10 @@ as internal buffering of data.
    Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
    respectively. Availability: Windows, Unix.
 
+   .. versionadded:: 3.3
+      Some operating systems could support additional values, like
+      :data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`.
+
 
 .. function:: mkdirat(dirfd, path, mode=0o777)
 
index b684a9f7cb159c919a3fa24f1791cfb4bb4c1d14..c06f4b877b6fffc2c487270d425c0603e95d69ef 100644 (file)
@@ -16,6 +16,11 @@ except ImportError:
 import io
 from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
 
+valid_seek_flags = {0, 1, 2}  # Hardwired values
+if hasattr(os, 'SEEK_HOLE') :
+    valid_seek_flags.add(os.SEEK_HOLE)
+    valid_seek_flags.add(os.SEEK_DATA)
+
 # open() uses st_blksize whenever we can
 DEFAULT_BUFFER_SIZE = 8 * 1024  # bytes
 
@@ -306,6 +311,7 @@ class IOBase(metaclass=abc.ABCMeta):
         * 0 -- start of stream (the default); offset should be zero or positive
         * 1 -- current stream position; offset may be negative
         * 2 -- end of stream; offset is usually negative
+        Some operating systems / file systems could provide additional values.
 
         Return an int indicating the new absolute position.
         """
@@ -866,7 +872,7 @@ class BytesIO(BufferedIOBase):
         elif whence == 2:
             self._pos = max(0, len(self._buffer) + pos)
         else:
-            raise ValueError("invalid whence value")
+            raise ValueError("unsupported whence value")
         return self._pos
 
     def tell(self):
@@ -1041,7 +1047,7 @@ class BufferedReader(_BufferedIOMixin):
         return _BufferedIOMixin.tell(self) - len(self._read_buf) + self._read_pos
 
     def seek(self, pos, whence=0):
-        if not (0 <= whence <= 2):
+        if whence not in valid_seek_flags:
             raise ValueError("invalid whence value")
         with self._read_lock:
             if whence == 1:
@@ -1138,8 +1144,8 @@ class BufferedWriter(_BufferedIOMixin):
         return _BufferedIOMixin.tell(self) + len(self._write_buf)
 
     def seek(self, pos, whence=0):
-        if not (0 <= whence <= 2):
-            raise ValueError("invalid whence")
+        if whence not in valid_seek_flags:
+            raise ValueError("invalid whence value")
         with self._write_lock:
             self._flush_unlocked()
             return _BufferedIOMixin.seek(self, pos, whence)
@@ -1235,8 +1241,8 @@ class BufferedRandom(BufferedWriter, BufferedReader):
         BufferedWriter.__init__(self, raw, buffer_size, max_buffer_size)
 
     def seek(self, pos, whence=0):
-        if not (0 <= whence <= 2):
-            raise ValueError("invalid whence")
+        if whence not in valid_seek_flags:
+            raise ValueError("invalid whence value")
         self.flush()
         if self._read_buf:
             # Undo read ahead.
@@ -1852,8 +1858,7 @@ class TextIOWrapper(TextIOBase):
                 self._decoder.reset()
             return position
         if whence != 0:
-            raise ValueError("invalid whence (%r, should be 0, 1 or 2)" %
-                             (whence,))
+            raise ValueError("unsupported whence (%r)" % (whence,))
         if cookie < 0:
             raise ValueError("negative seek position %r" % (cookie,))
         self.flush()
index 11e4d60d15ec9844f6f5a0e425d8851498fbe2f0..261a39e64b32be60a2a647148821e917ea8d08a1 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -121,6 +121,7 @@ del _names
 
 # Python uses fixed values for the SEEK_ constants; they are mapped
 # to native constants if necessary in posixmodule.c
+# Other possible SEEK values are directly imported from posixmodule.c
 SEEK_SET = 0
 SEEK_CUR = 1
 SEEK_END = 2
index 1951a06b9b6ec6aa4486e23f1831bf3a300fa606..54ba179f1d8e6fbeb62e884acc26279f39a14e59 100644 (file)
@@ -706,7 +706,7 @@ class CommonBufferedTests:
         bufio = self.tp(rawio)
         # Invalid whence
         self.assertRaises(ValueError, bufio.seek, 0, -1)
-        self.assertRaises(ValueError, bufio.seek, 0, 3)
+        self.assertRaises(ValueError, bufio.seek, 0, 9)
 
     def test_override_destructor(self):
         tp = self.tp
index 21473fc2eb2ab8eda6c1c102233dc10bd5a6e30d..ae46866272600534a83587ff5416d4543eb5c382 100644 (file)
@@ -1015,6 +1015,26 @@ class PosixTester(unittest.TestCase):
         posix.RTLD_GLOBAL
         posix.RTLD_LOCAL
 
+    @unittest.skipUnless(hasattr(os, 'SEEK_HOLE'),
+                         "test needs an OS that reports file holes")
+    def test_fs_holes(self) :
+        # Even if the filesystem doesn't report holes,
+        # if the OS supports it the SEEK_* constants
+        # will be defined and will have a consistent
+        # behaviour:
+        # os.SEEK_DATA = current position
+        # os.SEEK_HOLE = end of file position
+        with open(support.TESTFN, 'r+b') as fp :
+            fp.write(b"hello")
+            fp.flush()
+            size = fp.tell()
+            fno = fp.fileno()
+            for i in range(size) :
+                self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
+                self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
+            self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
+            self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
+
 class PosixGroupsTester(unittest.TestCase):
 
     def setUp(self):
index 8b7e3becd7406b797b6e9c640822061c782f1955..af1e26c3dbe47c317f87ba7150f3b472f94f5e83 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -935,6 +935,8 @@ Extension Modules
 - Issue #14259: The finditer() method of re objects did not take any
   keyword arguments, contrary to the documentation.
 
+- Issue #10142: Support for SEEK_HOLE/SEEK_DATA (for example, under ZFS).
+
 Tests
 -----
 
index 8a9ae4704e45539cfafab68aa7554c368ae0d7cd..dc723b1793673998df4cf6fcc4bd74c4d965339c 100644 (file)
@@ -1157,9 +1157,20 @@ buffered_seek(buffered *self, PyObject *args)
     if (!PyArg_ParseTuple(args, "O|i:seek", &targetobj, &whence)) {
         return NULL;
     }
-    if (whence < 0 || whence > 2) {
+
+    /* Do some error checking instead of trusting OS 'seek()'
+    ** error detection, just in case.
+    */
+    if ((whence < 0 || whence >2)
+#ifdef SEEK_HOLE
+        && (whence != SEEK_HOLE)
+#endif
+#ifdef SEEK_DATA
+        && (whence != SEEK_DATA)
+#endif
+        ) {
         PyErr_Format(PyExc_ValueError,
-                     "whence must be between 0 and 2, not %d", whence);
+                     "whence value %d unsupported", whence);
         return NULL;
     }
 
@@ -1172,7 +1183,11 @@ buffered_seek(buffered *self, PyObject *args)
     if (target == -1 && PyErr_Occurred())
         return NULL;
 
-    if (whence != 2 && self->readable) {
+    /* SEEK_SET and SEEK_CUR are special because we could seek inside the
+       buffer. Other whence values must be managed without this optimization.
+       Some Operating Systems can provide additional values, like
+       SEEK_HOLE/SEEK_DATA. */
+    if (((whence == 0) || (whence == 1)) && self->readable) {
         Py_off_t current, avail;
         /* Check if seeking leaves us inside the current buffer,
            so as to return quickly if possible. Also, we needn't take the
index 094108a29f84aaa79d714dc23c88165af6ddd06e..5418cb9a2ad3f2c5e9b600c72b5518a035d19e5f 100644 (file)
@@ -11292,6 +11292,13 @@ all_ins(PyObject *d)
 #endif
 
 
+#ifdef SEEK_HOLE
+    if (ins(d, "SEEK_HOLE", (long)SEEK_HOLE)) return -1;
+#endif
+#ifdef SEEK_DATA
+    if (ins(d, "SEEK_DATA", (long)SEEK_DATA)) return -1;
+#endif
+
 /* MS Windows */
 #ifdef O_NOINHERIT
     /* Don't inherit in child processes. */