def test_load(self): # __doc__ (as of 2008-07-13) for pygame.mixer_music.load: # pygame.mixer.music.load(filename): return None # Load a music file for playback data_fname = example_path('data') pygame.mixer.init() # The mp3 test file can crash smpeg on some systems. ## formats = ['mp3', 'ogg', 'wav'] formats = ['ogg', 'wav'] for f in formats: path = os.path.join(data_fname, 'house_lo.%s' % f) if os.sep == '\\': path = path.replace('\\', '\\\\') umusfn = as_unicode(path) bmusfn = filesystem_encode(umusfn) pygame.mixer.music.load(umusfn) pygame.mixer.music.load(bmusfn) #NOTE: TODO: loading from filelikes are disabled... # because as of writing it only works in SDL_mixer svn. #pygame.mixer.music.load(open(musfn)) #musf = open(musfn) #pygame.mixer.music.load(musf) pygame.mixer.quit()
def test_load(self): # __doc__ (as of 2008-07-13) for pygame.mixer_music.load: # pygame.mixer.music.load(filename): return None # Load a music file for playback data_fname = example_path('data') pygame.mixer.init() # The mp3 test file can crash smpeg on some systems. ## formats = ['mp3', 'ogg', 'wav'] formats = ['ogg', 'wav'] for f in formats: path = os.path.join(data_fname, 'house_lo.%s' % f) if os.sep == '\\': path = path.replace('\\', '\\\\') umusfn = path if isinstance(umusfn, bytes_): umusfn = path.decode('ascii') bmusfn = filesystem_encode(umusfn) pygame.mixer.music.load(umusfn) pygame.mixer.music.load(bmusfn) # Test loading from filelikes objects pygame.mixer.music.load(open(bmusfn)) musf = open(bmusfn) pygame.mixer.music.load(musf) pygame.mixer.quit()
def testLoadPNG(self): """ see if we can load a png. """ f = example_path('data/alien1.png') # normalized # f = os.path.join("examples", "data", "alien1.png") surf = pygame.image.load(f) f = open(f, 'rb') # f = open(os.path.join("examples", "data", "alien1.png"), "rb") surf = pygame.image.load(f)
def testLoadJPG(self): """ see if we can load a jpg. """ f = example_path('data/alien1.jpg') # normalized pygame.image.load(f) f = open(f, "rb") pygame.image.load(f)
def test_load(self): # __doc__ (as of 2008-07-13) for pygame.mixer_music.load: # pygame.mixer.music.load(filename): return None # Load a music file for playback data_fname = example_path('data') pygame.mixer.init() formats = ['mp3', 'ogg', 'wav'] for f in formats: musfn = os.path.join(data_fname, 'house_lo.%s' % f) pygame.mixer.music.load(musfn) #NOTE: TODO: loading from filelikes are disabled... # because as of writing it only works in SDL_mixer svn. #pygame.mixer.music.load(open(musfn)) #musf = open(musfn) #pygame.mixer.music.load(musf) pygame.mixer.quit()
def test_load_bmp_threads(self): self.threads_load(glob.glob(example_path("data/*.bmp")))
def test_load_unicode_path(self): u = unicode_(example_path("data/alien1.png")) im = load_extended(u)
def test_sound_args(self): def get_bytes(snd): return snd.get_buffer().raw mixer.init() try: sample = as_bytes('\x00\xff') * 24 wave_path = example_path(os.path.join('data', 'house_lo.wav')) uwave_path = unicode_(wave_path) bwave_path = uwave_path.encode(sys.getfilesystemencoding()) snd = mixer.Sound(file=wave_path) self.assert_(snd.get_length() > 0.5) snd_bytes = get_bytes(snd) self.assert_(len(snd_bytes) > 1000) self.assert_(get_bytes(mixer.Sound(wave_path)) == snd_bytes) self.assert_(get_bytes(mixer.Sound(file=uwave_path)) == snd_bytes) self.assert_(get_bytes(mixer.Sound(uwave_path)) == snd_bytes) arg_emsg = 'Sound takes either 1 positional or 1 keyword argument' try: mixer.Sound() except TypeError: self.assertEqual(str(geterror()), arg_emsg) else: self.fail("no exception") try: mixer.Sound(wave_path, buffer=sample) except TypeError: self.assertEqual(str(geterror()), arg_emsg) else: self.fail("no exception") try: mixer.Sound(sample, file=wave_path) except TypeError: self.assertEqual(str(geterror()), arg_emsg) else: self.fail("no exception") try: mixer.Sound(buffer=sample, file=wave_path) except TypeError: self.assertEqual(str(geterror()), arg_emsg) else: self.fail("no exception") try: mixer.Sound(foobar=sample) except TypeError: emsg = "Unrecognized keyword argument 'foobar'" self.assertEqual(str(geterror()), emsg) else: self.fail("no exception") snd = mixer.Sound(wave_path, **{}) self.assertEqual(get_bytes(snd), snd_bytes) snd = mixer.Sound(*[], **{'file': wave_path}) try: snd = mixer.Sound([]) except TypeError: emsg = 'Unrecognized argument (type list)' self.assertEqual(str(geterror()), emsg) else: self.fail("no exception") try: snd = mixer.Sound(buffer=[]) except TypeError: emsg = 'Expected object with buffer interface: got a list' self.assertEqual(str(geterror()), emsg) else: self.fail("no exception") ufake_path = unicode_('12345678') self.assertRaises(pygame.error, mixer.Sound, ufake_path) try: mixer.Sound(buffer=unicode_('something')) except TypeError: emsg = 'Unicode object not allowed as buffer object' self.assertEqual(str(geterror()), emsg) else: self.fail("no exception") self.assertEqual(get_bytes(mixer.Sound(buffer=sample)), sample) self.assertEqual(get_bytes(mixer.Sound(sample)), sample) self.assertEqual(get_bytes(mixer.Sound(file=bwave_path)), snd_bytes) self.assertEqual(get_bytes(mixer.Sound(bwave_path)), snd_bytes) snd = mixer.Sound(wave_path) try: mixer.Sound(wave_path, array=snd) except TypeError: self.assertEqual(str(geterror()), arg_emsg) else: self.fail("no exception") try: mixer.Sound(buffer=sample, array=snd) except TypeError: self.assertEqual(str(geterror()), arg_emsg) else: self.fail("no exception") snd2 = mixer.Sound(array=snd) self.assertEqual(snd.get_buffer().raw, snd2.get_buffer().raw) finally: mixer.quit()
def test_load_pathlike(self): import pathlib pygame.image.load(pathlib.Path(example_path("data/chimp.bmp")))
def test_sound_args(self): def get_bytes(snd): return snd.get_raw() mixer.init() try: sample = as_bytes('\x00\xff') * 24 wave_path = example_path(os.path.join('data', 'house_lo.wav')) uwave_path = unicode_(wave_path) bwave_path = uwave_path.encode(sys.getfilesystemencoding()) snd = mixer.Sound(file=wave_path) self.assertTrue(snd.get_length() > 0.5) snd_bytes = get_bytes(snd) self.assertTrue(len(snd_bytes) > 1000) self.assertEqual(get_bytes(mixer.Sound(wave_path)), snd_bytes) self.assertEqual(get_bytes(mixer.Sound(file=uwave_path)), snd_bytes) self.assertEqual(get_bytes(mixer.Sound(uwave_path)), snd_bytes) arg_emsg = 'Sound takes either 1 positional or 1 keyword argument' with self.assertRaises(TypeError) as cm: mixer.Sound() self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(wave_path, buffer=sample) self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(sample, file=wave_path) self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(buffer=sample, file=wave_path) self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(foobar=sample) self.assertEqual(str(cm.exception), "Unrecognized keyword argument 'foobar'") snd = mixer.Sound(wave_path, **{}) self.assertEqual(get_bytes(snd), snd_bytes) snd = mixer.Sound(*[], **{'file': wave_path}) with self.assertRaises(TypeError) as cm: mixer.Sound([]) self.assertEqual(str(cm.exception), 'Unrecognized argument (type list)') with self.assertRaises(TypeError) as cm: snd = mixer.Sound(buffer=[]) emsg = 'Expected object with buffer interface: got a list' self.assertEqual(str(cm.exception), emsg) ufake_path = unicode_('12345678') self.assertRaises(pygame.error, mixer.Sound, ufake_path) with self.assertRaises(TypeError) as cm: mixer.Sound(buffer=unicode_('something')) emsg = 'Unicode object not allowed as buffer object' self.assertEqual(str(cm.exception), emsg) self.assertEqual(get_bytes(mixer.Sound(buffer=sample)), sample) self.assertEqual(get_bytes(mixer.Sound(sample)), sample) self.assertEqual(get_bytes(mixer.Sound(file=bwave_path)), snd_bytes) self.assertEqual(get_bytes(mixer.Sound(bwave_path)), snd_bytes) snd = mixer.Sound(wave_path) with self.assertRaises(TypeError) as cm: mixer.Sound(wave_path, array=snd) self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(buffer=sample, array=snd) self.assertEqual(str(cm.exception), arg_emsg) snd2 = mixer.Sound(array=snd) self.assertEqual(snd.get_raw(), snd2.get_raw()) finally: mixer.quit()
def test_sound_args(self): def get_bytes(snd): return snd.get_buffer().raw mixer.init() try: sample = as_bytes('\x00\xff') * 24 wave_path = example_path(os.path.join('data', 'house_lo.wav')) uwave_path = unicode_(wave_path) bwave_path = uwave_path.encode(sys.getfilesystemencoding()) snd = mixer.Sound(file=wave_path) self.assert_(snd.get_length() > 0.5) snd_bytes = get_bytes(snd) self.assert_(len(snd_bytes) > 1000) self.assert_(get_bytes(mixer.Sound(wave_path)) == snd_bytes) self.assert_(get_bytes(mixer.Sound(file=uwave_path)) == snd_bytes) self.assert_(get_bytes(mixer.Sound(uwave_path)) == snd_bytes) arg_emsg = 'Sound takes either 1 positional or 1 keyword argument' try: mixer.Sound() except TypeError: self.assert_(str(geterror()) == arg_emsg) else: self.fail("no exception") try: mixer.Sound(wave_path, buffer=sample) except TypeError: self.assert_(str(geterror()) == arg_emsg) else: self.fail("no exception") try: mixer.Sound(sample, file=wave_path) except TypeError: self.assert_(str(geterror()) == arg_emsg) else: self.fail("no exception") try: mixer.Sound(buffer=sample, file=wave_path) except TypeError: self.assert_(str(geterror()) == arg_emsg) else: self.fail("no exception") try: mixer.Sound(foobar=sample) except TypeError: emsg = "Unrecognized keyword argument 'foobar'" self.assert_(str(geterror()) == emsg) else: self.fail("no exception") snd = mixer.Sound(wave_path, **{}) self.assert_(get_bytes(snd) == snd_bytes) snd = mixer.Sound(*[], **{'file': wave_path}) try: snd = mixer.Sound([]) except TypeError: emsg = 'Unrecognized argument (type list)' self.assert_(str(geterror()) == emsg) else: self.fail("no exception") try: snd = mixer.Sound(buffer=[]) except TypeError: emsg = 'Expected object with buffer interface: got a list' self.assert_(str(geterror()) == emsg) else: self.fail("no exception") ufake_path = unicode_('12345678') self.assertRaises(pygame.error, mixer.Sound, ufake_path) try: mixer.Sound(buffer=unicode_('something')) except TypeError: emsg = 'Unicode object not allowed as buffer object' self.assert_(str(geterror()) == emsg) else: self.fail("no exception") self.assert_(get_bytes(mixer.Sound(buffer=sample)) == sample) self.assert_(get_bytes(mixer.Sound(sample)) == sample) self.assert_(get_bytes(mixer.Sound(file=bwave_path)) == snd_bytes) self.assert_(get_bytes(mixer.Sound(bwave_path)) == snd_bytes) finally: mixer.quit()
def test_sound_args(self): def get_bytes(snd): return snd.get_raw() mixer.init() sample = as_bytes("\x00\xff") * 24 wave_path = example_path(os.path.join("data", "house_lo.wav")) uwave_path = unicode_(wave_path) bwave_path = uwave_path.encode(sys.getfilesystemencoding()) snd = mixer.Sound(file=wave_path) self.assertTrue(snd.get_length() > 0.5) snd_bytes = get_bytes(snd) self.assertTrue(len(snd_bytes) > 1000) self.assertEqual(get_bytes(mixer.Sound(wave_path)), snd_bytes) self.assertEqual(get_bytes(mixer.Sound(file=uwave_path)), snd_bytes) self.assertEqual(get_bytes(mixer.Sound(uwave_path)), snd_bytes) arg_emsg = "Sound takes either 1 positional or 1 keyword argument" with self.assertRaises(TypeError) as cm: mixer.Sound() self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(wave_path, buffer=sample) self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(sample, file=wave_path) self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(buffer=sample, file=wave_path) self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(foobar=sample) self.assertEqual(str(cm.exception), "Unrecognized keyword argument 'foobar'") snd = mixer.Sound(wave_path, **{}) self.assertEqual(get_bytes(snd), snd_bytes) snd = mixer.Sound(*[], **{"file": wave_path}) with self.assertRaises(TypeError) as cm: mixer.Sound([]) self.assertEqual(str(cm.exception), "Unrecognized argument (type list)") with self.assertRaises(TypeError) as cm: snd = mixer.Sound(buffer=[]) emsg = "Expected object with buffer interface: got a list" self.assertEqual(str(cm.exception), emsg) ufake_path = unicode_("12345678") self.assertRaises(IOError, mixer.Sound, ufake_path) self.assertRaises(IOError, mixer.Sound, "12345678") with self.assertRaises(TypeError) as cm: mixer.Sound(buffer=unicode_("something")) emsg = "Unicode object not allowed as buffer object" self.assertEqual(str(cm.exception), emsg) self.assertEqual(get_bytes(mixer.Sound(buffer=sample)), sample) if type(sample) != str: somebytes = get_bytes(mixer.Sound(sample)) # on python 2 we do not allow using string except as file name. self.assertEqual(somebytes, sample) self.assertEqual(get_bytes(mixer.Sound(file=bwave_path)), snd_bytes) self.assertEqual(get_bytes(mixer.Sound(bwave_path)), snd_bytes) snd = mixer.Sound(wave_path) with self.assertRaises(TypeError) as cm: mixer.Sound(wave_path, array=snd) self.assertEqual(str(cm.exception), arg_emsg) with self.assertRaises(TypeError) as cm: mixer.Sound(buffer=sample, array=snd) self.assertEqual(str(cm.exception), arg_emsg) snd2 = mixer.Sound(array=snd) self.assertEqual(snd.get_raw(), snd2.get_raw())
def test_load_gif_threads(self): self.threads_load(glob.glob(example_path("data/*.gif")))
def test_load_jpg_threads(self): self.threads_load(glob.glob(example_path("data/*.jpg")))
def test_load_pathlib(self): """ works loading using a Path argument. """ path = pathlib.Path(example_path("data/asprite.bmp")) surf = pygame.image.load_extended(path) self.assertEqual(surf.get_at((0, 0)), (255, 255, 255, 255))
def test_queue__invalid_sound_type(self): """Ensures queue() correctly handles invalid file types.""" not_a_sound_file = example_path(os.path.join("data", "city.png")) with self.assertRaises(pygame.error): pygame.mixer.music.queue(not_a_sound_file)
def test_load_unicode_path(self): u = unicode_(example_path("data/alien1.png")) im = imageext.load_extended(u)