mirror of https://github.com/python/cpython.git
Sjoerd Mullender:
Added support for unseekable files. (I use unqualified excepts since we don't know why the seek/tell might fail. In my case it was because of an AttributeError.)
This commit is contained in:
parent
f2f0594587
commit
7bb11d68bf
32
Lib/chunk.py
32
Lib/chunk.py
|
@ -63,7 +63,12 @@ def __init__(self, file, align = 1):
|
||||||
raise EOFError
|
raise EOFError
|
||||||
self.chunksize = self.chunksize - 8 # subtract header
|
self.chunksize = self.chunksize - 8 # subtract header
|
||||||
self.size_read = 0
|
self.size_read = 0
|
||||||
self.offset = self.file.tell()
|
try:
|
||||||
|
self.offset = self.file.tell()
|
||||||
|
except:
|
||||||
|
self.seekable = 0
|
||||||
|
else:
|
||||||
|
self.seekable = 1
|
||||||
|
|
||||||
def getname(self):
|
def getname(self):
|
||||||
"""Return the name (ID) of the current chunk."""
|
"""Return the name (ID) of the current chunk."""
|
||||||
|
@ -87,6 +92,8 @@ def seek(self, pos, mode = 0):
|
||||||
|
|
||||||
if self.closed:
|
if self.closed:
|
||||||
raise ValueError, "I/O operation on closed file"
|
raise ValueError, "I/O operation on closed file"
|
||||||
|
if not self.seekable:
|
||||||
|
raise IOError, "cannot seek"
|
||||||
if mode == 1:
|
if mode == 1:
|
||||||
pos = pos + self.size_read
|
pos = pos + self.size_read
|
||||||
elif mode == 2:
|
elif mode == 2:
|
||||||
|
@ -133,11 +140,20 @@ def skip(self):
|
||||||
|
|
||||||
if self.closed:
|
if self.closed:
|
||||||
raise ValueError, "I/O operation on closed file"
|
raise ValueError, "I/O operation on closed file"
|
||||||
try:
|
if self.seekable:
|
||||||
self.file.seek(self.chunksize - self.size_read, 1)
|
try:
|
||||||
except RuntimeError:
|
n = self.chunksize - self.size_read
|
||||||
while self.size_read < self.chunksize:
|
# maybe fix alignment
|
||||||
dummy = self.read(8192)
|
if self.align and (self.chunksize & 1):
|
||||||
if not dummy:
|
n = n + 1
|
||||||
raise EOFError
|
self.file.seek(n, 1)
|
||||||
|
self.size_read = self.size_read + n
|
||||||
|
return
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
while self.size_read < self.chunksize:
|
||||||
|
n = min(8192, self.chunksize - self.size_read)
|
||||||
|
dummy = self.read(n)
|
||||||
|
if not dummy:
|
||||||
|
raise EOFError
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue