<s> from test . support import findfile , TESTFN , unlink <EOL> import unittest <EOL> from test import audiotests <EOL> import os <EOL> import io <EOL> import sys <EOL> import struct <EOL> import aifc <EOL> class AifcPCM8Test ( audiotests . AudioWriteTests , <EOL> audiotests . AudioTestsWithSourceFile , <EOL> unittest . TestCase ) : <EOL> module = aifc <EOL> sndfilename = '<STR_LIT>' <EOL> sndfilenframes = <NUM_LIT> <EOL> nchannels = <NUM_LIT:2> <EOL> sampwidth = <NUM_LIT:1> <EOL> framerate = <NUM_LIT> <EOL> nframes = <NUM_LIT> <EOL> comptype = b'<STR_LIT>' <EOL> compname = b'<STR_LIT>' <EOL> frames = bytes . fromhex ( """<STR_LIT>""" ) <EOL> close_fd = True <EOL> class AifcPCM16Test ( audiotests . AudioWriteTests , <EOL> audiotests . AudioTestsWithSourceFile , <EOL> unittest . TestCase ) : <EOL> module = aifc <EOL> sndfilename = '<STR_LIT>' <EOL> sndfilenframes = <NUM_LIT> <EOL> nchannels = <NUM_LIT:2> <EOL> sampwidth = <NUM_LIT:2> <EOL> framerate = <NUM_LIT> <EOL> nframes = <NUM_LIT> <EOL> comptype = b'<STR_LIT>' <EOL> compname = b'<STR_LIT>' <EOL> frames = bytes . fromhex ( """<STR_LIT>""" ) <EOL> close_fd = True <EOL> class AifcPCM24Test ( audiotests . AudioWriteTests , <EOL> audiotests . AudioTestsWithSourceFile , <EOL> unittest . TestCase ) : <EOL> module = aifc <EOL> sndfilename = '<STR_LIT>' <EOL> sndfilenframes = <NUM_LIT> <EOL> nchannels = <NUM_LIT:2> <EOL> sampwidth = <NUM_LIT:3> <EOL> framerate = <NUM_LIT> <EOL> nframes = <NUM_LIT> <EOL> comptype = b'<STR_LIT>' <EOL> compname = b'<STR_LIT>' <EOL> frames = bytes . fromhex ( """<STR_LIT>""" ) <EOL> close_fd = True <EOL> class AifcPCM32Test ( audiotests . AudioWriteTests , <EOL> audiotests . AudioTestsWithSourceFile , <EOL> unittest . TestCase ) : <EOL> module = aifc <EOL> sndfilename = '<STR_LIT>' <EOL> sndfilenframes = <NUM_LIT> <EOL> nchannels = <NUM_LIT:2> <EOL> sampwidth = <NUM_LIT:4> <EOL> framerate = <NUM_LIT> <EOL> nframes = <NUM_LIT> <EOL> comptype = b'<STR_LIT>' <EOL> compname = b'<STR_LIT>' <EOL> frames = bytes . fromhex ( """<STR_LIT>""" ) <EOL> close_fd = True <EOL> class AifcULAWTest ( audiotests . AudioWriteTests , <EOL> audiotests . AudioTestsWithSourceFile , <EOL> unittest . TestCase ) : <EOL> module = aifc <EOL> sndfilename = '<STR_LIT>' <EOL> sndfilenframes = <NUM_LIT> <EOL> nchannels = <NUM_LIT:2> <EOL> sampwidth = <NUM_LIT:2> <EOL> framerate = <NUM_LIT> <EOL> nframes = <NUM_LIT> <EOL> comptype = b'<STR_LIT>' <EOL> compname = b'<STR_LIT>' <EOL> frames = bytes . fromhex ( """<STR_LIT>""" ) <EOL> if sys . byteorder != '<STR_LIT>' : <EOL> frames = audiotests . byteswap2 ( frames ) <EOL> close_fd = True <EOL> class AifcALAWTest ( audiotests . AudioWriteTests , <EOL> audiotests . AudioTestsWithSourceFile , <EOL> unittest . TestCase ) : <EOL> module = aifc <EOL> sndfilename = '<STR_LIT>' <EOL> sndfilenframes = <NUM_LIT> <EOL> nchannels = <NUM_LIT:2> <EOL> sampwidth = <NUM_LIT:2> <EOL> framerate = <NUM_LIT> <EOL> nframes = <NUM_LIT> <EOL> comptype = b'<STR_LIT>' <EOL> compname = b'<STR_LIT>' <EOL> frames = bytes . fromhex ( """<STR_LIT>""" ) <EOL> if sys . byteorder != '<STR_LIT>' : <EOL> frames = audiotests . byteswap2 ( frames ) <EOL> close_fd = True <EOL> class AifcMiscTest ( audiotests . AudioTests , unittest . TestCase ) : <EOL> def test_skipunknown ( self ) : <EOL> self . f = aifc . open ( findfile ( '<STR_LIT>' ) ) <EOL> def test_write_header_comptype_sampwidth ( self ) : <EOL> for comptype in ( b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ) : <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> fout . setnchannels ( <NUM_LIT:1> ) <EOL> fout . setframerate ( <NUM_LIT:1> ) <EOL> fout . setcomptype ( comptype , b'<STR_LIT>' ) <EOL> fout . close ( ) <EOL> self . assertEqual ( fout . getsampwidth ( ) , <NUM_LIT:2> ) <EOL> fout . initfp ( None ) <EOL> def test_write_markers_values ( self ) : <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> self . assertEqual ( fout . getmarkers ( ) , None ) <EOL> fout . setmark ( <NUM_LIT:1> , <NUM_LIT:0> , b'<STR_LIT>' ) <EOL> fout . setmark ( <NUM_LIT:1> , <NUM_LIT:1> , b'<STR_LIT>' ) <EOL> self . assertEqual ( fout . getmark ( <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:1> , b'<STR_LIT>' ) ) <EOL> self . assertEqual ( fout . getmarkers ( ) , [ ( <NUM_LIT:1> , <NUM_LIT:1> , b'<STR_LIT>' ) ] ) <EOL> fout . initfp ( None ) <EOL> def test_read_markers ( self ) : <EOL> fout = self . fout = aifc . open ( TESTFN , '<STR_LIT:wb>' ) <EOL> fout . aiff ( ) <EOL> fout . setparams ( ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , b'<STR_LIT>' , b'<STR_LIT>' ) ) <EOL> fout . setmark ( <NUM_LIT:1> , <NUM_LIT:0> , b'<STR_LIT>' ) <EOL> fout . setmark ( <NUM_LIT:2> , <NUM_LIT:0> , b'<STR_LIT>' ) <EOL> fout . writeframes ( b'<STR_LIT:\x00>' ) <EOL> fout . close ( ) <EOL> f = self . f = aifc . open ( TESTFN , '<STR_LIT:rb>' ) <EOL> self . assertEqual ( f . getmarkers ( ) , [ ( <NUM_LIT:1> , <NUM_LIT:0> , b'<STR_LIT>' ) , ( <NUM_LIT:2> , <NUM_LIT:0> , b'<STR_LIT>' ) ] ) <EOL> self . assertEqual ( f . getmark ( <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:0> , b'<STR_LIT>' ) ) <EOL> self . assertEqual ( f . getmark ( <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:0> , b'<STR_LIT>' ) ) <EOL> self . assertRaises ( aifc . Error , f . getmark , <NUM_LIT:3> ) <EOL> class AIFCLowLevelTest ( unittest . TestCase ) : <EOL> def test_read_written ( self ) : <EOL> def read_written ( self , what ) : <EOL> f = io . BytesIO ( ) <EOL> getattr ( aifc , '<STR_LIT>' + what ) ( f , x ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> return getattr ( aifc , '<STR_LIT>' + what ) ( f ) <EOL> for x in ( - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0.1> , <NUM_LIT:1> ) : <EOL> self . assertEqual ( read_written ( x , '<STR_LIT:float>' ) , x ) <EOL> for x in ( float ( '<STR_LIT>' ) , float ( '<STR_LIT>' ) ) : <EOL> self . assertEqual ( read_written ( x , '<STR_LIT:float>' ) , aifc . _HUGE_VAL ) <EOL> for x in ( b'<STR_LIT>' , b'<STR_LIT:foo>' , b'<STR_LIT:a>' * <NUM_LIT:255> ) : <EOL> self . assertEqual ( read_written ( x , '<STR_LIT:string>' ) , x ) <EOL> for x in ( - <NUM_LIT> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> ) : <EOL> self . assertEqual ( read_written ( x , '<STR_LIT>' ) , x ) <EOL> for x in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> ) : <EOL> self . assertEqual ( read_written ( x , '<STR_LIT>' ) , x ) <EOL> for x in ( - <NUM_LIT> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> ) : <EOL> self . assertEqual ( read_written ( x , '<STR_LIT>' ) , x ) <EOL> for x in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> ) : <EOL> self . assertEqual ( read_written ( x , '<STR_LIT>' ) , x ) <EOL> def test_read_raises ( self ) : <EOL> f = io . BytesIO ( b'<STR_LIT:\x00>' ) <EOL> self . assertRaises ( EOFError , aifc . _read_ulong , f ) <EOL> self . assertRaises ( EOFError , aifc . _read_long , f ) <EOL> self . assertRaises ( EOFError , aifc . _read_ushort , f ) <EOL> self . assertRaises ( EOFError , aifc . _read_short , f ) <EOL> def test_write_long_string_raises ( self ) : <EOL> f = io . BytesIO ( ) <EOL> with self . assertRaises ( ValueError ) : <EOL> aifc . _write_string ( f , b'<STR_LIT>' * <NUM_LIT:255> ) <EOL> def test_wrong_open_mode ( self ) : <EOL> with self . assertRaises ( aifc . Error ) : <EOL> aifc . open ( TESTFN , '<STR_LIT>' ) <EOL> def test_read_wrong_form ( self ) : <EOL> b1 = io . BytesIO ( b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> b2 = io . BytesIO ( b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:4> ) + b'<STR_LIT>' ) <EOL> self . assertRaises ( aifc . Error , aifc . open , b1 ) <EOL> self . assertRaises ( aifc . Error , aifc . open , b2 ) <EOL> def test_read_no_comm_chunk ( self ) : <EOL> b = io . BytesIO ( b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:4> ) + b'<STR_LIT>' ) <EOL> self . assertRaises ( aifc . Error , aifc . open , b ) <EOL> def test_read_wrong_compression_type ( self ) : <EOL> b = b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:4> ) + b'<STR_LIT>' <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT:B>' , <NUM_LIT:0> ) <EOL> self . assertRaises ( aifc . Error , aifc . open , io . BytesIO ( b ) ) <EOL> def test_read_wrong_marks ( self ) : <EOL> b = b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:4> ) + b'<STR_LIT>' <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:8> ) + b'<STR_LIT:\x00>' * <NUM_LIT:8> <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> with self . assertWarns ( UserWarning ) as cm : <EOL> f = aifc . open ( io . BytesIO ( b ) ) <EOL> self . assertEqual ( str ( cm . warning ) , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( f . getmarkers ( ) , None ) <EOL> def test_read_comm_kludge_compname_even ( self ) : <EOL> b = b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:4> ) + b'<STR_LIT>' <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT:B>' , <NUM_LIT:4> ) + b'<STR_LIT>' + b'<STR_LIT:\x00>' <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:8> ) + b'<STR_LIT:\x00>' * <NUM_LIT:8> <EOL> with self . assertWarns ( UserWarning ) as cm : <EOL> f = aifc . open ( io . BytesIO ( b ) ) <EOL> self . assertEqual ( str ( cm . warning ) , '<STR_LIT>' ) <EOL> self . assertEqual ( f . getcompname ( ) , b'<STR_LIT>' ) <EOL> def test_read_comm_kludge_compname_odd ( self ) : <EOL> b = b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:4> ) + b'<STR_LIT>' <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT:B>' , <NUM_LIT:3> ) + b'<STR_LIT>' <EOL> b += b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:8> ) + b'<STR_LIT:\x00>' * <NUM_LIT:8> <EOL> with self . assertWarns ( UserWarning ) as cm : <EOL> f = aifc . open ( io . BytesIO ( b ) ) <EOL> self . assertEqual ( str ( cm . warning ) , '<STR_LIT>' ) <EOL> self . assertEqual ( f . getcompname ( ) , b'<STR_LIT>' ) <EOL> def test_write_params_raises ( self ) : <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> wrong_params = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , b'<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRaises ( aifc . Error , fout . setparams , wrong_params ) <EOL> self . assertRaises ( aifc . Error , fout . getparams ) <EOL> self . assertRaises ( aifc . Error , fout . setnchannels , <NUM_LIT:0> ) <EOL> self . assertRaises ( aifc . Error , fout . getnchannels ) <EOL> self . assertRaises ( aifc . Error , fout . setsampwidth , <NUM_LIT:0> ) <EOL> self . assertRaises ( aifc . Error , fout . getsampwidth ) <EOL> self . assertRaises ( aifc . Error , fout . setframerate , <NUM_LIT:0> ) <EOL> self . assertRaises ( aifc . Error , fout . getframerate ) <EOL> self . assertRaises ( aifc . Error , fout . setcomptype , b'<STR_LIT>' , '<STR_LIT>' ) <EOL> fout . aiff ( ) <EOL> fout . setnchannels ( <NUM_LIT:1> ) <EOL> fout . setsampwidth ( <NUM_LIT:1> ) <EOL> fout . setframerate ( <NUM_LIT:1> ) <EOL> fout . setnframes ( <NUM_LIT:1> ) <EOL> fout . writeframes ( b'<STR_LIT:\x00>' ) <EOL> self . assertRaises ( aifc . Error , fout . setparams , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . assertRaises ( aifc . Error , fout . setnchannels , <NUM_LIT:1> ) <EOL> self . assertRaises ( aifc . Error , fout . setsampwidth , <NUM_LIT:1> ) <EOL> self . assertRaises ( aifc . Error , fout . setframerate , <NUM_LIT:1> ) <EOL> self . assertRaises ( aifc . Error , fout . setnframes , <NUM_LIT:1> ) <EOL> self . assertRaises ( aifc . Error , fout . setcomptype , b'<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRaises ( aifc . Error , fout . aiff ) <EOL> self . assertRaises ( aifc . Error , fout . aifc ) <EOL> def test_write_params_singles ( self ) : <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> fout . aifc ( ) <EOL> fout . setnchannels ( <NUM_LIT:1> ) <EOL> fout . setsampwidth ( <NUM_LIT:2> ) <EOL> fout . setframerate ( <NUM_LIT:3> ) <EOL> fout . setnframes ( <NUM_LIT:4> ) <EOL> fout . setcomptype ( b'<STR_LIT>' , b'<STR_LIT:name>' ) <EOL> self . assertEqual ( fout . getnchannels ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( fout . getsampwidth ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( fout . getframerate ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( fout . getnframes ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( fout . tell ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( fout . getcomptype ( ) , b'<STR_LIT>' ) <EOL> self . assertEqual ( fout . getcompname ( ) , b'<STR_LIT:name>' ) <EOL> fout . writeframes ( b'<STR_LIT:\x00>' * <NUM_LIT:4> * fout . getsampwidth ( ) * fout . getnchannels ( ) ) <EOL> self . assertEqual ( fout . getnframes ( ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( fout . tell ( ) , <NUM_LIT:4> ) <EOL> def test_write_params_bunch ( self ) : <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> fout . aifc ( ) <EOL> p = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , b'<STR_LIT>' , b'<STR_LIT:name>' ) <EOL> fout . setparams ( p ) <EOL> self . assertEqual ( fout . getparams ( ) , p ) <EOL> fout . initfp ( None ) <EOL> def test_write_header_raises ( self ) : <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> self . assertRaises ( aifc . Error , fout . close ) <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> fout . setnchannels ( <NUM_LIT:1> ) <EOL> self . assertRaises ( aifc . Error , fout . close ) <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> fout . setnchannels ( <NUM_LIT:1> ) <EOL> fout . setsampwidth ( <NUM_LIT:1> ) <EOL> self . assertRaises ( aifc . Error , fout . close ) <EOL> def test_write_header_comptype_raises ( self ) : <EOL> for comptype in ( b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ) : <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> fout . setsampwidth ( <NUM_LIT:1> ) <EOL> fout . setcomptype ( comptype , b'<STR_LIT>' ) <EOL> self . assertRaises ( aifc . Error , fout . close ) <EOL> fout . initfp ( None ) <EOL> def test_write_markers_raises ( self ) : <EOL> fout = aifc . open ( io . BytesIO ( ) , '<STR_LIT:wb>' ) <EOL> self . assertRaises ( aifc . Error , fout . setmark , <NUM_LIT:0> , <NUM_LIT:0> , b'<STR_LIT>' ) <EOL> self . assertRaises ( aifc . Error , fout . setmark , <NUM_LIT:1> , - <NUM_LIT:1> , b'<STR_LIT>' ) <EOL> self . assertRaises ( aifc . Error , fout . setmark , <NUM_LIT:1> , <NUM_LIT:0> , None ) <EOL> self . assertRaises ( aifc . Error , fout . getmark , <NUM_LIT:1> ) <EOL> fout . initfp ( None ) <EOL> def test_write_aiff_by_extension ( self ) : <EOL> sampwidth = <NUM_LIT:2> <EOL> fout = self . fout = aifc . open ( TESTFN + '<STR_LIT>' , '<STR_LIT:wb>' ) <EOL> fout . setparams ( ( <NUM_LIT:1> , sampwidth , <NUM_LIT:1> , <NUM_LIT:1> , b'<STR_LIT>' , b'<STR_LIT>' ) ) <EOL> frames = b'<STR_LIT:\x00>' * fout . getnchannels ( ) * sampwidth <EOL> fout . writeframes ( frames ) <EOL> fout . close ( ) <EOL> f = self . f = aifc . open ( TESTFN + '<STR_LIT>' , '<STR_LIT:rb>' ) <EOL> self . assertEqual ( f . getcomptype ( ) , b'<STR_LIT>' ) <EOL> f . close ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
