@@ -162,6 +162,12 @@ def _read_short(file):
162162 except struct .error :
163163 raise EOFError
164164
165+ def _read_ushort (file ):
166+ try :
167+ return struct .unpack ('>H' , file .read (2 ))[0 ]
168+ except struct .error :
169+ raise EOFError
170+
165171def _read_string (file ):
166172 length = ord (file .read (1 ))
167173 if length == 0 :
@@ -194,13 +200,19 @@ def _read_float(f): # 10 bytes
194200def _write_short (f , x ):
195201 f .write (struct .pack ('>h' , x ))
196202
203+ def _write_ushort (f , x ):
204+ f .write (struct .pack ('>H' , x ))
205+
197206def _write_long (f , x ):
207+ f .write (struct .pack ('>l' , x ))
208+
209+ def _write_ulong (f , x ):
198210 f .write (struct .pack ('>L' , x ))
199211
200212def _write_string (f , s ):
201213 if len (s ) > 255 :
202214 raise ValueError ("string exceeds maximum pstring length" )
203- f .write (struct .pack ('b ' , len (s )))
215+ f .write (struct .pack ('B ' , len (s )))
204216 f .write (s )
205217 if len (s ) & 1 == 0 :
206218 f .write (b'\x00 ' )
@@ -218,7 +230,7 @@ def _write_float(f, x):
218230 lomant = 0
219231 else :
220232 fmant , expon = math .frexp (x )
221- if expon > 16384 or fmant >= 1 : # Infinity or NaN
233+ if expon > 16384 or fmant >= 1 or fmant != fmant : # Infinity or NaN
222234 expon = sign | 0x7FFF
223235 himant = 0
224236 lomant = 0
@@ -234,9 +246,9 @@ def _write_float(f, x):
234246 fmant = math .ldexp (fmant - fsmant , 32 )
235247 fsmant = math .floor (fmant )
236248 lomant = int (fsmant )
237- _write_short (f , expon )
238- _write_long (f , himant )
239- _write_long (f , lomant )
249+ _write_ushort (f , expon )
250+ _write_ulong (f , himant )
251+ _write_ulong (f , lomant )
240252
241253from chunk import Chunk
242254
@@ -762,25 +774,25 @@ def _write_header(self, initlength):
762774 if self ._aifc :
763775 self ._file .write (b'AIFC' )
764776 self ._file .write (b'FVER' )
765- _write_long (self ._file , 4 )
766- _write_long (self ._file , self ._version )
777+ _write_ulong (self ._file , 4 )
778+ _write_ulong (self ._file , self ._version )
767779 else :
768780 self ._file .write (b'AIFF' )
769781 self ._file .write (b'COMM' )
770- _write_long (self ._file , commlength )
782+ _write_ulong (self ._file , commlength )
771783 _write_short (self ._file , self ._nchannels )
772784 self ._nframes_pos = self ._file .tell ()
773- _write_long (self ._file , self ._nframes )
785+ _write_ulong (self ._file , self ._nframes )
774786 _write_short (self ._file , self ._sampwidth * 8 )
775787 _write_float (self ._file , self ._framerate )
776788 if self ._aifc :
777789 self ._file .write (self ._comptype )
778790 _write_string (self ._file , self ._compname )
779791 self ._file .write (b'SSND' )
780792 self ._ssnd_length_pos = self ._file .tell ()
781- _write_long (self ._file , self ._datalength + 8 )
782- _write_long (self ._file , 0 )
783- _write_long (self ._file , 0 )
793+ _write_ulong (self ._file , self ._datalength + 8 )
794+ _write_ulong (self ._file , 0 )
795+ _write_ulong (self ._file , 0 )
784796
785797 def _write_form_length (self , datalength ):
786798 if self ._aifc :
@@ -791,8 +803,8 @@ def _write_form_length(self, datalength):
791803 else :
792804 commlength = 18
793805 verslength = 0
794- _write_long (self ._file , 4 + verslength + self ._marklength + \
795- 8 + commlength + 16 + datalength )
806+ _write_ulong (self ._file , 4 + verslength + self ._marklength + \
807+ 8 + commlength + 16 + datalength )
796808 return commlength
797809
798810 def _patchheader (self ):
@@ -810,9 +822,9 @@ def _patchheader(self):
810822 self ._file .seek (self ._form_length_pos , 0 )
811823 dummy = self ._write_form_length (datalength )
812824 self ._file .seek (self ._nframes_pos , 0 )
813- _write_long (self ._file , self ._nframeswritten )
825+ _write_ulong (self ._file , self ._nframeswritten )
814826 self ._file .seek (self ._ssnd_length_pos , 0 )
815- _write_long (self ._file , datalength + 8 )
827+ _write_ulong (self ._file , datalength + 8 )
816828 self ._file .seek (curpos , 0 )
817829 self ._nframes = self ._nframeswritten
818830 self ._datalength = datalength
@@ -827,13 +839,13 @@ def _writemarkers(self):
827839 length = length + len (name ) + 1 + 6
828840 if len (name ) & 1 == 0 :
829841 length = length + 1
830- _write_long (self ._file , length )
842+ _write_ulong (self ._file , length )
831843 self ._marklength = length + 8
832844 _write_short (self ._file , len (self ._markers ))
833845 for marker in self ._markers :
834846 id , pos , name = marker
835847 _write_short (self ._file , id )
836- _write_long (self ._file , pos )
848+ _write_ulong (self ._file , pos )
837849 _write_string (self ._file , name )
838850
839851def open (f , mode = None ):
0 commit comments