Commit 814661e0 authored by Guido van Rossum's avatar Guido van Rossum

Fix test_zipfile.py. (Why was it passing before?)

The usual str/bytes issues.

BTW, perhaps zipfp.open() should behave more like io.open()?
parent 697a84b1
...@@ -247,14 +247,14 @@ class TestsWithSourceFile(unittest.TestCase): ...@@ -247,14 +247,14 @@ class TestsWithSourceFile(unittest.TestCase):
# NOTE: this test fails if len(d) < 22 because of the first # NOTE: this test fails if len(d) < 22 because of the first
# line "fpin.seek(-22, 2)" in _EndRecData # line "fpin.seek(-22, 2)" in _EndRecData
d = 'I am not a ZipFile!'*10 d = 'I am not a ZipFile!'*10
f = file(TESTFN2, 'wb') f = open(TESTFN2, 'wb')
f.write(d) f.write(d)
f.close() f.close()
zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
zipfp.write(TESTFN, TESTFN) zipfp.write(TESTFN, TESTFN)
zipfp.close() zipfp.close()
f = file(TESTFN2, 'rb') f = open(TESTFN2, 'rb')
f.seek(len(d)) f.seek(len(d))
zipfp = zipfile.ZipFile(f, "r") zipfp = zipfile.ZipFile(f, "r")
self.assertEqual(zipfp.namelist(), [TESTFN]) self.assertEqual(zipfp.namelist(), [TESTFN])
...@@ -265,7 +265,7 @@ class TestsWithSourceFile(unittest.TestCase): ...@@ -265,7 +265,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Check that calling ZipFile.write without arcname specified produces the expected result # Check that calling ZipFile.write without arcname specified produces the expected result
zipfp = zipfile.ZipFile(TESTFN2, "w") zipfp = zipfile.ZipFile(TESTFN2, "w")
zipfp.write(TESTFN) zipfp.write(TESTFN)
self.assertEqual(zipfp.read(TESTFN), file(TESTFN).read()) self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
zipfp.close() zipfp.close()
def test_PerFileCompression(self): def test_PerFileCompression(self):
...@@ -478,7 +478,7 @@ class PyZipFileTests(unittest.TestCase): ...@@ -478,7 +478,7 @@ class PyZipFileTests(unittest.TestCase):
def testWriteNonPyfile(self): def testWriteNonPyfile(self):
zipfp = zipfile.PyZipFile(TemporaryFile(), "w") zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
file(TESTFN, 'w').write('most definitely not a python file') open(TESTFN, 'w').write('most definitely not a python file')
self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
os.remove(TESTFN) os.remove(TESTFN)
...@@ -566,7 +566,7 @@ class OtherTests(unittest.TestCase): ...@@ -566,7 +566,7 @@ class OtherTests(unittest.TestCase):
self.assertRaises(RuntimeError, zipf.open, "foo.txt") self.assertRaises(RuntimeError, zipf.open, "foo.txt")
self.assertRaises(RuntimeError, zipf.testzip) self.assertRaises(RuntimeError, zipf.testzip)
self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
file(TESTFN, 'w').write('zipfile test data') open(TESTFN, 'w').write('zipfile test data')
self.assertRaises(RuntimeError, zipf.write, TESTFN) self.assertRaises(RuntimeError, zipf.write, TESTFN)
def test_BadConstructorMode(self): def test_BadConstructorMode(self):
...@@ -592,9 +592,9 @@ class OtherTests(unittest.TestCase): ...@@ -592,9 +592,9 @@ class OtherTests(unittest.TestCase):
# read the data to make sure the file is there # read the data to make sure the file is there
f = zipf.open("foo.txt") f = zipf.open("foo.txt")
for i in range(FIXEDTEST_SIZE): for i in range(FIXEDTEST_SIZE):
self.assertEqual(f.read(0), '') self.assertEqual(f.read(0), b'')
self.assertEqual(f.read(), "O, for a Muse of Fire!") self.assertEqual(f.read(), b"O, for a Muse of Fire!")
zipf.close() zipf.close()
def test_OpenNonexistentItem(self): def test_OpenNonexistentItem(self):
...@@ -610,7 +610,7 @@ class OtherTests(unittest.TestCase): ...@@ -610,7 +610,7 @@ class OtherTests(unittest.TestCase):
def test_NullByteInFilename(self): def test_NullByteInFilename(self):
# Check that a filename containing a null byte is properly terminated # Check that a filename containing a null byte is properly terminated
zipf = zipfile.ZipFile(TESTFN, mode="w") zipf = zipfile.ZipFile(TESTFN, mode="w")
zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!") zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
self.assertEqual(zipf.namelist(), ['foo.txt']) self.assertEqual(zipf.namelist(), ['foo.txt'])
def tearDown(self): def tearDown(self):
......
...@@ -412,7 +412,7 @@ class ZipExtFile: ...@@ -412,7 +412,7 @@ class ZipExtFile:
# ugly check for cases where half of an \r\n pair was # ugly check for cases where half of an \r\n pair was
# read on the last pass, and the \r was discarded. In this # read on the last pass, and the \r was discarded. In this
# case we just throw away the \n at the start of the buffer. # case we just throw away the \n at the start of the buffer.
if (self.lastdiscard, self.linebuffer[0]) == (b'\r', b'\n'): if (self.lastdiscard, self.linebuffer[:1]) == (b'\r', b'\n'):
self.linebuffer = self.linebuffer[1:] self.linebuffer = self.linebuffer[1:]
for sep in self.nlSeps: for sep in self.nlSeps:
...@@ -479,9 +479,9 @@ class ZipExtFile: ...@@ -479,9 +479,9 @@ class ZipExtFile:
return result return result
def read(self, size = None): def read(self, size = None):
# act like file() obj and return empty string if size is 0 # act like file obj and return empty string if size is 0
if size == 0: if size == 0:
return '' return b''
# determine read size # determine read size
bytesToRead = self.compress_size - self.bytes_read bytesToRead = self.compress_size - self.bytes_read
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment