summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJesus Cea <jcea@jcea.es>2012-04-26 14:39:35 (GMT)
committerJesus Cea <jcea@jcea.es>2012-04-26 14:39:35 (GMT)
commit2b47f0a23ffcb0cd86dc9eacf379fbd329b895c7 (patch)
treee7a04c686ef2b72e9d4495b88e8f87c58cfd5e11 /Lib
parent790a9b4c193a9f3323138a8c1c1d074a6b6289f0 (diff)
downloadcpython-2b47f0a23ffcb0cd86dc9eacf379fbd329b895c7.zip
cpython-2b47f0a23ffcb0cd86dc9eacf379fbd329b895c7.tar.gz
cpython-2b47f0a23ffcb0cd86dc9eacf379fbd329b895c7.tar.bz2
Close #10142: Support for SEEK_HOLE/SEEK_DATA
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_pyio.py12
-rw-r--r--Lib/os.py1
-rw-r--r--Lib/test/test_posix.py20
3 files changed, 24 insertions, 9 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index f66290f..41402b1 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -306,6 +306,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 +867,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,8 +1042,6 @@ 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):
- raise ValueError("invalid whence value")
with self._read_lock:
if whence == 1:
pos -= len(self._read_buf) - self._read_pos
@@ -1138,8 +1137,6 @@ 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")
with self._write_lock:
self._flush_unlocked()
return _BufferedIOMixin.seek(self, pos, whence)
@@ -1235,8 +1232,6 @@ 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")
self.flush()
if self._read_buf:
# Undo read ahead.
@@ -1852,8 +1847,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()
diff --git a/Lib/os.py b/Lib/os.py
index 2c7d954..2403f30 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -116,6 +116,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
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 142dddd..c8798a9 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -1009,6 +1009,26 @@ class PosixTester(unittest.TestCase):
posix.RTLD_GLOBAL
posix.RTLD_LOCAL
+ @unittest.skipUnless('PC_MIN_HOLE_SIZE' in os.pathconf_names,
+ "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):