From 7714ebbe0e7556bbfa6b768e434042b55474939c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 16 Nov 2013 13:04:00 +0200 Subject: Issue #5202: Added support for unseekable files in the wave module. --- Lib/test/audiotests.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'Lib/test/audiotests.py') diff --git a/Lib/test/audiotests.py b/Lib/test/audiotests.py index 7b39269ec9..c22f0a10a9 100644 --- a/Lib/test/audiotests.py +++ b/Lib/test/audiotests.py @@ -21,6 +21,13 @@ def byteswap4(data): a.byteswap() return a.tobytes() +class UnseekableIO(io.FileIO): + def tell(self): + raise io.UnsupportedOperation + + def seek(self, *args, **kwargs): + raise io.UnsupportedOperation + class AudioTests: close_fd = False @@ -177,6 +184,59 @@ class AudioWriteTests(AudioTests): self.assertEqual(testfile.read(13), b'ababagalamaga') self.check_file(testfile, self.nframes, self.frames) + def test_unseekable_read(self): + with self.create_file(TESTFN) as f: + f.setnframes(self.nframes) + f.writeframes(self.frames) + + with UnseekableIO(TESTFN, 'rb') as testfile: + self.check_file(testfile, self.nframes, self.frames) + + def test_unseekable_write(self): + with UnseekableIO(TESTFN, 'wb') as testfile: + with self.create_file(testfile) as f: + f.setnframes(self.nframes) + f.writeframes(self.frames) + + self.check_file(TESTFN, self.nframes, self.frames) + + def test_unseekable_incompleted_write(self): + with UnseekableIO(TESTFN, 'wb') as testfile: + testfile.write(b'ababagalamaga') + f = self.create_file(testfile) + f.setnframes(self.nframes + 1) + try: + f.writeframes(self.frames) + except OSError: + pass + try: + f.close() + except OSError: + pass + + with open(TESTFN, 'rb') as testfile: + self.assertEqual(testfile.read(13), b'ababagalamaga') + self.check_file(testfile, self.nframes + 1, self.frames) + + def test_unseekable_overflowed_write(self): + with UnseekableIO(TESTFN, 'wb') as testfile: + testfile.write(b'ababagalamaga') + f = self.create_file(testfile) + f.setnframes(self.nframes - 1) + try: + f.writeframes(self.frames) + except OSError: + pass + try: + f.close() + except OSError: + pass + + with open(TESTFN, 'rb') as testfile: + self.assertEqual(testfile.read(13), b'ababagalamaga') + framesize = self.nchannels * self.sampwidth + self.check_file(testfile, self.nframes - 1, self.frames[:-framesize]) + class AudioTestsWithSourceFile(AudioTests): -- cgit v1.2.1