summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2012-06-19 23:48:50 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2012-06-19 23:48:50 (GMT)
commit10c8791978203be95af2c4c1d7ce33496fac880c (patch)
treed30969af462c1c847aebb202b93e3b5a7cd43250 /Lib/test
parente67f48ce5e7ad122b17e23b2705bf66cff76d42b (diff)
parent103e8113e4bb4ad3687d641f660481c72904d571 (diff)
downloadcpython-10c8791978203be95af2c4c1d7ce33496fac880c.zip
cpython-10c8791978203be95af2c4c1d7ce33496fac880c.tar.gz
cpython-10c8791978203be95af2c4c1d7ce33496fac880c.tar.bz2
Fix GzipFile's handling of filenames given as bytes objects.
Add relevant tests for GzipFile, and also for BZ2File and LZMAFile.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_bz2.py15
-rw-r--r--Lib/test/test_gzip.py14
-rw-r--r--Lib/test/test_lzma.py24
3 files changed, 53 insertions, 0 deletions
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
index 6b1a93c..257b144 100644
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -522,6 +522,21 @@ class BZ2FileTest(BaseTest):
with BZ2File(self.filename) as bz2f:
self.assertEqual(bz2f.read(), data1 + data2)
+ def testOpenBytesFilename(self):
+ str_filename = self.filename
+ try:
+ bytes_filename = str_filename.encode("ascii")
+ except UnicodeEncodeError:
+ self.skipTest("Temporary file name needs to be ASCII")
+ with BZ2File(bytes_filename, "wb") as f:
+ f.write(self.DATA)
+ with BZ2File(bytes_filename, "rb") as f:
+ self.assertEqual(f.read(), self.DATA)
+ # Sanity check that we are actually operating on the right file.
+ with BZ2File(str_filename, "rb") as f:
+ self.assertEqual(f.read(), self.DATA)
+
+
# Tests for a BZ2File wrapping another file object:
def testReadBytesIO(self):
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index bb97097..af73953 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -355,6 +355,20 @@ class TestGzip(BaseTest):
with gzip.GzipFile(fileobj=f, mode="w") as g:
pass
+ def test_bytes_filename(self):
+ str_filename = self.filename
+ try:
+ bytes_filename = str_filename.encode("ascii")
+ except UnicodeEncodeError:
+ self.skipTest("Temporary file name needs to be ASCII")
+ with gzip.GzipFile(bytes_filename, "wb") as f:
+ f.write(data1 * 50)
+ with gzip.GzipFile(bytes_filename, "rb") as f:
+ self.assertEqual(f.read(), data1 * 50)
+ # Sanity check that we are actually operating on the right file.
+ with gzip.GzipFile(str_filename, "rb") as f:
+ self.assertEqual(f.read(), data1 * 50)
+
# Testing compress/decompress shortcut functions
def test_compress(self):
diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py
index 22f2f47..a086586 100644
--- a/Lib/test/test_lzma.py
+++ b/Lib/test/test_lzma.py
@@ -655,6 +655,16 @@ class FileTestCase(unittest.TestCase):
self.assertEqual(f.read(), INPUT)
self.assertEqual(f.read(), b"")
+ def test_read_from_file_with_bytes_filename(self):
+ try:
+ bytes_filename = TESTFN.encode("ascii")
+ except UnicodeEncodeError:
+ self.skipTest("Temporary file name needs to be ASCII")
+ with TempFile(TESTFN, COMPRESSED_XZ):
+ with LZMAFile(bytes_filename) as f:
+ self.assertEqual(f.read(), INPUT)
+ self.assertEqual(f.read(), b"")
+
def test_read_incomplete(self):
with LZMAFile(BytesIO(COMPRESSED_XZ[:128])) as f:
self.assertRaises(EOFError, f.read)
@@ -814,6 +824,20 @@ class FileTestCase(unittest.TestCase):
finally:
unlink(TESTFN)
+ def test_write_to_file_with_bytes_filename(self):
+ try:
+ bytes_filename = TESTFN.encode("ascii")
+ except UnicodeEncodeError:
+ self.skipTest("Temporary file name needs to be ASCII")
+ try:
+ with LZMAFile(bytes_filename, "w") as f:
+ f.write(INPUT)
+ expected = lzma.compress(INPUT)
+ with open(TESTFN, "rb") as f:
+ self.assertEqual(f.read(), expected)
+ finally:
+ unlink(TESTFN)
+
def test_write_append_to_file(self):
part1 = INPUT[:1024]
part2 = INPUT[1024:1536]