mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Issue #27029: Removed deprecated support of universal newlines mode from ZipFile.open().
This commit is contained in:
parent
e6dae877dc
commit
e670be2273
5 changed files with 24 additions and 222 deletions
|
@ -38,10 +38,6 @@ def get_files(test):
|
|||
yield f
|
||||
test.assertFalse(f.closed)
|
||||
|
||||
def openU(zipfp, fn):
|
||||
with check_warnings(('', DeprecationWarning)):
|
||||
return zipfp.open(fn, 'rU')
|
||||
|
||||
class AbstractTestsWithSourceFile:
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
|
@ -1035,32 +1031,6 @@ class OtherTests(unittest.TestCase):
|
|||
data += zipfp.read(info)
|
||||
self.assertIn(data, {b"foobar", b"barfoo"})
|
||||
|
||||
def test_universal_deprecation(self):
|
||||
f = io.BytesIO()
|
||||
with zipfile.ZipFile(f, "w") as zipfp:
|
||||
zipfp.writestr('spam.txt', b'ababagalamaga')
|
||||
|
||||
with zipfile.ZipFile(f, "r") as zipfp:
|
||||
for mode in 'U', 'rU':
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
zipopen = zipfp.open('spam.txt', mode)
|
||||
zipopen.close()
|
||||
|
||||
def test_universal_readaheads(self):
|
||||
f = io.BytesIO()
|
||||
|
||||
data = b'a\r\n' * 16 * 1024
|
||||
with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as zipfp:
|
||||
zipfp.writestr(TESTFN, data)
|
||||
|
||||
data2 = b''
|
||||
with zipfile.ZipFile(f, 'r') as zipfp, \
|
||||
openU(zipfp, TESTFN) as zipopen:
|
||||
for line in zipopen:
|
||||
data2 += line
|
||||
|
||||
self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
|
||||
|
||||
def test_writestr_extended_local_header_issue1202(self):
|
||||
with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
|
||||
for data in 'abcdefghijklmnop':
|
||||
|
@ -1268,9 +1238,12 @@ class OtherTests(unittest.TestCase):
|
|||
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
|
||||
|
||||
with zipfile.ZipFile(TESTFN, mode="r") as zipf:
|
||||
# read the data to make sure the file is there
|
||||
# read the data to make sure the file is there
|
||||
zipf.read("foo.txt")
|
||||
self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
|
||||
# universal newlines support is removed
|
||||
self.assertRaises(RuntimeError, zipf.open, "foo.txt", "U")
|
||||
self.assertRaises(RuntimeError, zipf.open, "foo.txt", "rU")
|
||||
|
||||
def test_read0(self):
|
||||
"""Check that calling read(0) on a ZipExtFile object returns an empty
|
||||
|
@ -2011,138 +1984,6 @@ class TestWithDirectory(unittest.TestCase):
|
|||
unlink(TESTFN)
|
||||
|
||||
|
||||
class AbstractUniversalNewlineTests:
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
|
||||
for i in range(FIXEDTEST_SIZE)]
|
||||
cls.seps = (b'\r', b'\r\n', b'\n')
|
||||
cls.arcdata = {}
|
||||
for n, s in enumerate(cls.seps):
|
||||
cls.arcdata[s] = s.join(cls.line_gen) + s
|
||||
|
||||
def setUp(self):
|
||||
self.arcfiles = {}
|
||||
for n, s in enumerate(self.seps):
|
||||
self.arcfiles[s] = '%s-%d' % (TESTFN, n)
|
||||
with open(self.arcfiles[s], "wb") as f:
|
||||
f.write(self.arcdata[s])
|
||||
|
||||
def make_test_archive(self, f, compression):
|
||||
# Create the ZIP archive
|
||||
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
||||
for fn in self.arcfiles.values():
|
||||
zipfp.write(fn, fn)
|
||||
|
||||
def read_test(self, f, compression):
|
||||
self.make_test_archive(f, compression)
|
||||
|
||||
# Read the ZIP archive
|
||||
with zipfile.ZipFile(f, "r") as zipfp:
|
||||
for sep, fn in self.arcfiles.items():
|
||||
with openU(zipfp, fn) as fp:
|
||||
zipdata = fp.read()
|
||||
self.assertEqual(self.arcdata[sep], zipdata)
|
||||
|
||||
def test_read(self):
|
||||
for f in get_files(self):
|
||||
self.read_test(f, self.compression)
|
||||
|
||||
def readline_read_test(self, f, compression):
|
||||
self.make_test_archive(f, compression)
|
||||
|
||||
# Read the ZIP archive
|
||||
with zipfile.ZipFile(f, "r") as zipfp:
|
||||
for sep, fn in self.arcfiles.items():
|
||||
with openU(zipfp, fn) as zipopen:
|
||||
data = b''
|
||||
while True:
|
||||
read = zipopen.readline()
|
||||
if not read:
|
||||
break
|
||||
data += read
|
||||
|
||||
read = zipopen.read(5)
|
||||
if not read:
|
||||
break
|
||||
data += read
|
||||
|
||||
self.assertEqual(data, self.arcdata[b'\n'])
|
||||
|
||||
def test_readline_read(self):
|
||||
for f in get_files(self):
|
||||
self.readline_read_test(f, self.compression)
|
||||
|
||||
def readline_test(self, f, compression):
|
||||
self.make_test_archive(f, compression)
|
||||
|
||||
# Read the ZIP archive
|
||||
with zipfile.ZipFile(f, "r") as zipfp:
|
||||
for sep, fn in self.arcfiles.items():
|
||||
with openU(zipfp, fn) as zipopen:
|
||||
for line in self.line_gen:
|
||||
linedata = zipopen.readline()
|
||||
self.assertEqual(linedata, line + b'\n')
|
||||
|
||||
def test_readline(self):
|
||||
for f in get_files(self):
|
||||
self.readline_test(f, self.compression)
|
||||
|
||||
def readlines_test(self, f, compression):
|
||||
self.make_test_archive(f, compression)
|
||||
|
||||
# Read the ZIP archive
|
||||
with zipfile.ZipFile(f, "r") as zipfp:
|
||||
for sep, fn in self.arcfiles.items():
|
||||
with openU(zipfp, fn) as fp:
|
||||
ziplines = fp.readlines()
|
||||
for line, zipline in zip(self.line_gen, ziplines):
|
||||
self.assertEqual(zipline, line + b'\n')
|
||||
|
||||
def test_readlines(self):
|
||||
for f in get_files(self):
|
||||
self.readlines_test(f, self.compression)
|
||||
|
||||
def iterlines_test(self, f, compression):
|
||||
self.make_test_archive(f, compression)
|
||||
|
||||
# Read the ZIP archive
|
||||
with zipfile.ZipFile(f, "r") as zipfp:
|
||||
for sep, fn in self.arcfiles.items():
|
||||
with openU(zipfp, fn) as fp:
|
||||
for line, zipline in zip(self.line_gen, fp):
|
||||
self.assertEqual(zipline, line + b'\n')
|
||||
|
||||
def test_iterlines(self):
|
||||
for f in get_files(self):
|
||||
self.iterlines_test(f, self.compression)
|
||||
|
||||
def tearDown(self):
|
||||
for sep, fn in self.arcfiles.items():
|
||||
unlink(fn)
|
||||
unlink(TESTFN)
|
||||
unlink(TESTFN2)
|
||||
|
||||
|
||||
class StoredUniversalNewlineTests(AbstractUniversalNewlineTests,
|
||||
unittest.TestCase):
|
||||
compression = zipfile.ZIP_STORED
|
||||
|
||||
@requires_zlib
|
||||
class DeflateUniversalNewlineTests(AbstractUniversalNewlineTests,
|
||||
unittest.TestCase):
|
||||
compression = zipfile.ZIP_DEFLATED
|
||||
|
||||
@requires_bz2
|
||||
class Bzip2UniversalNewlineTests(AbstractUniversalNewlineTests,
|
||||
unittest.TestCase):
|
||||
compression = zipfile.ZIP_BZIP2
|
||||
|
||||
@requires_lzma
|
||||
class LzmaUniversalNewlineTests(AbstractUniversalNewlineTests,
|
||||
unittest.TestCase):
|
||||
compression = zipfile.ZIP_LZMA
|
||||
|
||||
class ZipInfoTests(unittest.TestCase):
|
||||
def test_from_file(self):
|
||||
zi = zipfile.ZipInfo.from_file(__file__)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue