mirror of
https://github.com/python/cpython.git
synced 2025-10-09 08:31:26 +00:00
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. """
This commit is contained in:
parent
2900ff9382
commit
3601e88cb3
3 changed files with 50 additions and 147 deletions
15
Lib/chunk.py
15
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue