diff options
| author | Guido van Rossum <guido@python.org> | 1999-06-16 12:25:34 +0000 | 
|---|---|---|
| committer | Guido van Rossum <guido@python.org> | 1999-06-16 12:25:34 +0000 | 
| commit | 7bb11d68bf12010a9a6d5074cc0f05e46d1cf517 (patch) | |
| tree | e985092aaba07046c76690d82bd59deca7f3aa11 /Lib | |
| parent | f2f059458792a4ebf80f0adb7e97823e51e697a9 (diff) | |
| download | cpython-git-7bb11d68bf12010a9a6d5074cc0f05e46d1cf517.tar.gz | |
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.)
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/chunk.py | 32 | 
1 files changed, 24 insertions, 8 deletions
diff --git a/Lib/chunk.py b/Lib/chunk.py index 320339ef74..4105f319a8 100644 --- a/Lib/chunk.py +++ b/Lib/chunk.py @@ -63,7 +63,12 @@ class Chunk:              raise EOFError          self.chunksize = self.chunksize - 8 # subtract header          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):          """Return the name (ID) of the current chunk.""" @@ -87,6 +92,8 @@ class Chunk:          if self.closed:              raise ValueError, "I/O operation on closed file" +        if not self.seekable: +            raise IOError, "cannot seek"          if mode == 1:              pos = pos + self.size_read          elif mode == 2: @@ -133,11 +140,20 @@ class Chunk:          if self.closed:              raise ValueError, "I/O operation on closed file" -        try: -            self.file.seek(self.chunksize - self.size_read, 1) -        except RuntimeError: -            while self.size_read < self.chunksize: -                dummy = self.read(8192) -                if not dummy: -                    raise EOFError +        if self.seekable: +            try: +                n = self.chunksize - self.size_read +                # maybe fix alignment +                if self.align and (self.chunksize & 1): +                    n = n + 1 +                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  | 
