Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
16e2c101
Commit
16e2c101
authored
Apr 19, 2011
by
Brian Curtin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix #8886. Use context managers throughout zipfile tests.
This was fixed in py3k SVN. Consider this a backport.
parent
2e9642d6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
110 additions
and
112 deletions
+110
-112
Lib/test/test_zipfile.py
Lib/test/test_zipfile.py
+110
-112
No files found.
Lib/test/test_zipfile.py
View file @
16e2c101
...
...
@@ -115,7 +115,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
,
compression
)
as
zipfp
:
zipdata1
=
[]
zipopen1
=
zipfp
.
open
(
TESTFN
)
with
zipfp
.
open
(
TESTFN
)
as
zipopen1
:
while
True
:
read_data
=
zipopen1
.
read
(
256
)
if
not
read_data
:
...
...
@@ -123,7 +123,7 @@ class TestsWithSourceFile(unittest.TestCase):
zipdata1
.
append
(
read_data
)
zipdata2
=
[]
zipopen2
=
zipfp
.
open
(
"another.name"
)
with
zipfp
.
open
(
"another.name"
)
as
zipopen2
:
while
True
:
read_data
=
zipopen2
.
read
(
256
)
if
not
read_data
:
...
...
@@ -147,7 +147,8 @@ class TestsWithSourceFile(unittest.TestCase):
infos
=
zipfp
.
infolist
()
data
=
""
for
info
in
infos
:
data
+=
zipfp
.
open
(
info
).
read
()
with
zipfp
.
open
(
info
)
as
f
:
data
+=
f
.
read
()
self
.
assertTrue
(
data
==
"foobar"
or
data
==
"barfoo"
)
data
=
""
for
info
in
infos
:
...
...
@@ -160,7 +161,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
,
compression
)
as
zipfp
:
zipdata1
=
[]
zipopen1
=
zipfp
.
open
(
TESTFN
)
with
zipfp
.
open
(
TESTFN
)
as
zipopen1
:
while
True
:
read_data
=
zipopen1
.
read
(
randint
(
1
,
1024
))
if
not
read_data
:
...
...
@@ -177,16 +178,14 @@ class TestsWithSourceFile(unittest.TestCase):
f
=
StringIO
()
data
=
'a
\
r
\
n
'
*
16
*
1024
zipfp
=
zipfile
.
ZipFile
(
f
,
'w'
,
zipfile
.
ZIP_STORED
)
with
zipfile
.
ZipFile
(
f
,
'w'
,
zipfile
.
ZIP_STORED
)
as
zipfp
:
zipfp
.
writestr
(
TESTFN
,
data
)
zipfp
.
close
()
data2
=
''
zipfp
=
zipfile
.
ZipFile
(
f
,
'r'
)
zipopen
=
zipfp
.
open
(
TESTFN
,
'rU'
)
with
zipfile
.
ZipFile
(
f
,
'r'
)
as
zipfp
:
with
zipfp
.
open
(
TESTFN
,
'rU'
)
as
zipopen
:
for
line
in
zipopen
:
data2
+=
line
zipfp
.
close
()
self
.
assertEqual
(
data
,
data2
.
replace
(
'
\
n
'
,
'
\
r
\
n
'
))
...
...
@@ -194,9 +193,8 @@ class TestsWithSourceFile(unittest.TestCase):
self
.
make_test_archive
(
f
,
compression
)
# Read the ZIP archive
zipfp
=
zipfile
.
ZipFile
(
f
,
"r"
)
zipopen
=
zipfp
.
open
(
TESTFN
)
with
zipfile
.
ZipFile
(
f
,
"r"
)
as
zipfp
:
with
zipfp
.
open
(
TESTFN
)
as
zipopen
:
data
=
''
while
True
:
read
=
zipopen
.
readline
()
...
...
@@ -210,14 +208,13 @@ class TestsWithSourceFile(unittest.TestCase):
data
+=
read
self
.
assertEqual
(
data
,
self
.
data
)
zipfp
.
close
()
def
zip_readline_test
(
self
,
f
,
compression
):
self
.
make_test_archive
(
f
,
compression
)
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
)
as
zipfp
:
zipopen
=
zipfp
.
open
(
TESTFN
)
with
zipfp
.
open
(
TESTFN
)
as
zipopen
:
for
line
in
self
.
line_gen
:
linedata
=
zipopen
.
readline
()
self
.
assertEqual
(
linedata
,
line
+
'
\
n
'
)
...
...
@@ -227,7 +224,8 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
)
as
zipfp
:
ziplines
=
zipfp
.
open
(
TESTFN
).
readlines
()
with
zipfp
.
open
(
TESTFN
)
as
zo
:
ziplines
=
zo
.
readlines
()
for
line
,
zipline
in
zip
(
self
.
line_gen
,
ziplines
):
self
.
assertEqual
(
zipline
,
line
+
'
\
n
'
)
...
...
@@ -301,7 +299,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Get an open object for strfile
with
zipfile
.
ZipFile
(
TESTFN2
,
"r"
,
zipfile
.
ZIP_DEFLATED
)
as
zipfp
:
openobj
=
zipfp
.
open
(
"strfile"
)
with
zipfp
.
open
(
"strfile"
)
as
openobj
:
self
.
assertEqual
(
openobj
.
read
(
1
),
'1'
)
self
.
assertEqual
(
openobj
.
read
(
1
),
'2'
)
...
...
@@ -775,8 +773,8 @@ class OtherTests(unittest.TestCase):
self
.
assertRaises
(
IOError
,
zipfile
.
ZipFile
,
TESTFN
)
def
test_empty_file_raises_BadZipFile
(
self
):
f
=
open
(
TESTFN
,
'w'
)
f
.
close
()
with
open
(
TESTFN
,
'w'
)
as
f
:
pass
self
.
assertRaises
(
zipfile
.
BadZipfile
,
zipfile
.
ZipFile
,
TESTFN
)
with
open
(
TESTFN
,
'w'
)
as
fp
:
...
...
@@ -820,7 +818,7 @@ class OtherTests(unittest.TestCase):
with
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"w"
)
as
zipf
:
zipf
.
writestr
(
"foo.txt"
,
"O, for a Muse of Fire!"
)
# read the data to make sure the file is there
f
=
zipf
.
open
(
"foo.txt"
)
with
zipf
.
open
(
"foo.txt"
)
as
f
:
for
i
in
xrange
(
FIXEDTEST_SIZE
):
self
.
assertEqual
(
f
.
read
(
0
),
''
)
...
...
@@ -952,15 +950,15 @@ class OtherTests(unittest.TestCase):
def
test_empty_zipfile
(
self
):
# Check that creating a file in 'w' or 'a' mode and closing without
# adding any files to the archives creates a valid empty ZIP file
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"w"
)
zipf
.
close
()
with
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"w"
)
as
zipf
:
pass
try
:
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"r"
)
except
zipfile
.
BadZipfile
:
self
.
fail
(
"Unable to create empty ZIP file in 'w' mode"
)
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"a"
)
zipf
.
close
()
with
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"a"
)
as
zipf
:
pass
try
:
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"r"
)
except
:
...
...
@@ -970,8 +968,8 @@ class OtherTests(unittest.TestCase):
# Issue 1710703: Check that opening a file with less than 22 bytes
# raises a BadZipfile exception (rather than the previously unhelpful
# IOError)
f
=
open
(
TESTFN
,
'w'
)
f
.
close
()
with
open
(
TESTFN
,
'w'
)
as
f
:
pass
self
.
assertRaises
(
zipfile
.
BadZipfile
,
zipfile
.
ZipFile
,
TESTFN
,
'r'
)
def
tearDown
(
self
):
...
...
@@ -1084,7 +1082,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
,
compression
)
as
zipfp
:
zipdata1
=
[]
zipopen1
=
zipfp
.
open
(
TESTFN
)
with
zipfp
.
open
(
TESTFN
)
as
zipopen1
:
while
True
:
read_data
=
zipopen1
.
read
(
256
)
if
not
read_data
:
...
...
@@ -1092,7 +1090,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
zipdata1
.
append
(
read_data
)
zipdata2
=
[]
zipopen2
=
zipfp
.
open
(
"another.name"
)
with
zipfp
.
open
(
"another.name"
)
as
zipopen2
:
while
True
:
read_data
=
zipopen2
.
read
(
256
)
if
not
read_data
:
...
...
@@ -1122,7 +1120,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
,
compression
)
as
zipfp
:
zipdata1
=
[]
zipopen1
=
zipfp
.
open
(
TESTFN
)
with
zipfp
.
open
(
TESTFN
)
as
zipopen1
:
while
True
:
read_data
=
zipopen1
.
read
(
randint
(
1
,
1024
))
if
not
read_data
:
...
...
@@ -1167,8 +1165,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
with
zipfile
.
ZipFile
(
TESTFN2
,
mode
=
"r"
)
as
zipf
:
zopen1
=
zipf
.
open
(
'ones'
)
zopen2
=
zipf
.
open
(
'twos'
)
with
zipf
.
open
(
'ones'
)
as
zopen1
,
zipf
.
open
(
'twos'
)
as
zopen2
:
data1
=
zopen1
.
read
(
500
)
data2
=
zopen2
.
read
(
500
)
data1
+=
zopen1
.
read
(
500
)
...
...
@@ -1180,9 +1177,8 @@ class TestsWithMultipleOpens(unittest.TestCase):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
with
zipfile
.
ZipFile
(
TESTFN2
,
mode
=
"r"
)
as
zipf
:
zopen1
=
zipf
.
open
(
'ones'
)
with
zipf
.
open
(
'ones'
)
as
zopen1
,
zipf
.
open
(
'twos'
)
as
zopen2
:
data1
=
zopen1
.
read
(
500
)
zopen2
=
zipf
.
open
(
'twos'
)
data2
=
zopen2
.
read
(
500
)
data1
+=
zopen1
.
read
(
500
)
data2
+=
zopen2
.
read
(
500
)
...
...
@@ -1244,7 +1240,8 @@ class UniversalNewlineTests(unittest.TestCase):
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
)
as
zipfp
:
for
sep
,
fn
in
self
.
arcfiles
.
items
():
zipdata
=
zipfp
.
open
(
fn
,
"rU"
).
read
()
with
zipfp
.
open
(
fn
,
"rU"
)
as
fp
:
zipdata
=
fp
.
read
()
self
.
assertEqual
(
self
.
arcdata
[
sep
],
zipdata
)
def
readline_read_test
(
self
,
f
,
compression
):
...
...
@@ -1253,7 +1250,7 @@ class UniversalNewlineTests(unittest.TestCase):
# Read the ZIP archive
zipfp
=
zipfile
.
ZipFile
(
f
,
"r"
)
for
sep
,
fn
in
self
.
arcfiles
.
items
():
zipopen
=
zipfp
.
open
(
fn
,
"rU"
)
with
zipfp
.
open
(
fn
,
"rU"
)
as
zipopen
:
data
=
''
while
True
:
read
=
zipopen
.
readline
()
...
...
@@ -1276,7 +1273,7 @@ class UniversalNewlineTests(unittest.TestCase):
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
)
as
zipfp
:
for
sep
,
fn
in
self
.
arcfiles
.
items
():
zipopen
=
zipfp
.
open
(
fn
,
"rU"
)
with
zipfp
.
open
(
fn
,
"rU"
)
as
zipopen
:
for
line
in
self
.
line_gen
:
linedata
=
zipopen
.
readline
()
self
.
assertEqual
(
linedata
,
line
+
'
\
n
'
)
...
...
@@ -1287,7 +1284,8 @@ class UniversalNewlineTests(unittest.TestCase):
# Read the ZIP archive
with
zipfile
.
ZipFile
(
f
,
"r"
)
as
zipfp
:
for
sep
,
fn
in
self
.
arcfiles
.
items
():
ziplines
=
zipfp
.
open
(
fn
,
"rU"
).
readlines
()
with
zipfp
.
open
(
fn
,
"rU"
)
as
fp
:
ziplines
=
fp
.
readlines
()
for
line
,
zipline
in
zip
(
self
.
line_gen
,
ziplines
):
self
.
assertEqual
(
zipline
,
line
+
'
\
n
'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment