diff options
author | Guido van Rossum <guido@python.org> | 1999-08-26 15:50:43 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1999-08-26 15:50:43 (GMT) |
commit | 3601e88cb3a41f805216e9b9feda591f678f4014 (patch) | |
tree | 74f25f71e81582d0e051aa8bbd87902afe2957c6 /Lib/chunk.py | |
parent | 2900ff93821743eef54d46e65b84c1afa497ef2b (diff) | |
download | cpython-3601e88cb3a41f805216e9b9feda591f678f4014.zip cpython-3601e88cb3a41f805216e9b9feda591f678f4014.tar.gz cpython-3601e88cb3a41f805216e9b9feda591f678f4014.tar.bz2 |
Sjoerd Mullender writes:
"""
Extended chunk so that it can also handle formats that are almost
according to EA IFF 85. In particular, added options to handle
little-endian and to handle formats that include the header size in
the chunk size value.
Fixed a bug where the header size was included in the chunk size, which
it isn't according to EA IFF 85.
Added a new method getsize() to get the size of the chunk (excluding
header).
Fixed chunk documentation (TIFF doesn't look like it uses chunks).
Converted wave to use chunk. Wave uses EA IFF 85 chunks except that
it uses little-endian encoding of integer data.
Removed __del__ methods from aifc and wave since I got an
AttributeError there upon exit.
"""
Diffstat (limited to 'Lib/chunk.py')
-rw-r--r-- | Lib/chunk.py | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/Lib/chunk.py b/Lib/chunk.py index 231a59c..fbbb1c1 100644 --- a/Lib/chunk.py +++ b/Lib/chunk.py @@ -49,19 +49,24 @@ default is 1, i.e. aligned. """ class Chunk: - def __init__(self, file, align = 1): + def __init__(self, file, align = 1, bigendian = 1, inclheader = 0): import struct self.closed = 0 self.align = align # whether to align to word (2-byte) boundaries + if bigendian: + strflag = '>' + else: + strflag = '<' self.file = file self.chunkname = file.read(4) if len(self.chunkname) < 4: raise EOFError try: - self.chunksize = struct.unpack('>l', file.read(4))[0] + self.chunksize = struct.unpack(strflag+'l', file.read(4))[0] except struct.error: raise EOFError - self.chunksize = self.chunksize - 8 # subtract header + if inclheader: + self.chunksize = self.chunksize - 8 # subtract header self.size_read = 0 try: self.offset = self.file.tell() @@ -74,6 +79,10 @@ class Chunk: """Return the name (ID) of the current chunk.""" return self.chunkname + def getsize(self): + """Return the size of the current chunk.""" + return self.chunksize + def close(self): if not self.closed: self.skip() |