mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
Fix GzipFile's handling of filenames given as bytes objects.
Add relevant tests for GzipFile, and also for BZ2File and LZMAFile.
This commit is contained in:
commit
10c8791978
5 changed files with 59 additions and 4 deletions
|
@ -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]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue