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
28f96b5b
Commit
28f96b5b
authored
Oct 13, 2010
by
Brian Curtin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement #7944. Use `with` throughout the test suite.
parent
01e39797
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
104 additions
and
117 deletions
+104
-117
Lib/test/test_gzip.py
Lib/test/test_gzip.py
+104
-117
No files found.
Lib/test/test_gzip.py
View file @
28f96b5b
...
...
@@ -44,7 +44,8 @@ class TestGzip(unittest.TestCase):
def
test_write
(
self
):
f
=
gzip
.
GzipFile
(
self
.
filename
,
'wb'
)
;
f
.
write
(
data1
*
50
)
with
gzip
.
GzipFile
(
self
.
filename
,
'wb'
)
as
f
:
f
.
write
(
data1
*
50
)
# Try flush and fileno.
f
.
flush
()
...
...
@@ -59,7 +60,8 @@ class TestGzip(unittest.TestCase):
def
test_read
(
self
):
self
.
test_write
()
# Try reading.
f
=
gzip
.
GzipFile
(
self
.
filename
,
'r'
)
;
d
=
f
.
read
()
;
f
.
close
()
with
gzip
.
GzipFile
(
self
.
filename
,
'r'
)
as
f
:
d
=
f
.
read
()
self
.
assertEqual
(
d
,
data1
*
50
)
def
test_io_on_closed_object
(
self
):
...
...
@@ -87,31 +89,30 @@ class TestGzip(unittest.TestCase):
def
test_append
(
self
):
self
.
test_write
()
# Append to the previous file
f
=
gzip
.
GzipFile
(
self
.
filename
,
'ab'
)
;
f
.
write
(
data2
*
15
)
;
f
.
close
()
with
gzip
.
GzipFile
(
self
.
filename
,
'ab'
)
as
f
:
f
.
write
(
data2
*
15
)
f
=
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
;
d
=
f
.
read
()
;
f
.
close
()
with
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
as
f
:
d
=
f
.
read
()
self
.
assertEqual
(
d
,
(
data1
*
50
)
+
(
data2
*
15
))
def
test_many_append
(
self
):
# Bug #1074261 was triggered when reading a file that contained
# many, many members. Create such a file and verify that reading it
# works.
f
=
gzip
.
open
(
self
.
filename
,
'wb'
,
9
)
with
gzip
.
open
(
self
.
filename
,
'wb'
,
9
)
as
f
:
f
.
write
(
b'a'
)
f
.
close
()
for
i
in
range
(
0
,
200
):
f
=
gzip
.
open
(
self
.
filename
,
"ab"
,
9
)
# append
with
gzip
.
open
(
self
.
filename
,
"ab"
,
9
)
as
f
:
# append
f
.
write
(
b'a'
)
f
.
close
()
# Try reading the file
zgfile
=
gzip
.
open
(
self
.
filename
,
"rb"
)
with
gzip
.
open
(
self
.
filename
,
"rb"
)
as
zgfile
:
contents
=
b""
while
1
:
ztxt
=
zgfile
.
read
(
8192
)
contents
+=
ztxt
if
not
ztxt
:
break
zgfile
.
close
()
self
.
assertEquals
(
contents
,
b'a'
*
201
)
def
test_buffered_reader
(
self
):
...
...
@@ -119,7 +120,7 @@ class TestGzip(unittest.TestCase):
# performance.
self
.
test_write
()
f
=
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
with
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
as
f
:
with
io
.
BufferedReader
(
f
)
as
r
:
lines
=
[
line
for
line
in
r
]
...
...
@@ -129,34 +130,31 @@ class TestGzip(unittest.TestCase):
self
.
test_write
()
# Try .readline() with varying line lengths
f
=
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
with
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
as
f
:
line_length
=
0
while
1
:
L
=
f
.
readline
(
line_length
)
if
not
L
and
line_length
!=
0
:
break
self
.
assertTrue
(
len
(
L
)
<=
line_length
)
line_length
=
(
line_length
+
1
)
%
50
f
.
close
()
def
test_readlines
(
self
):
self
.
test_write
()
# Try .readlines()
f
=
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
with
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
as
f
:
L
=
f
.
readlines
()
f
.
close
()
f
=
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
with
gzip
.
GzipFile
(
self
.
filename
,
'rb'
)
as
f
:
while
1
:
L
=
f
.
readlines
(
150
)
if
L
==
[]:
break
f
.
close
()
def
test_seek_read
(
self
):
self
.
test_write
()
# Try seek, read test
f
=
gzip
.
GzipFile
(
self
.
filename
)
with
gzip
.
GzipFile
(
self
.
filename
)
as
f
:
while
1
:
oldpos
=
f
.
tell
()
line1
=
f
.
readline
()
...
...
@@ -170,61 +168,52 @@ class TestGzip(unittest.TestCase):
line2
=
f
.
read
(
amount
)
self
.
assertEqual
(
line1
[:
amount
],
line2
)
f
.
seek
(
newpos
)
# positive seek
f
.
close
()
def
test_seek_whence
(
self
):
self
.
test_write
()
# Try seek(whence=1), read test
f
=
gzip
.
GzipFile
(
self
.
filename
)
with
gzip
.
GzipFile
(
self
.
filename
)
as
f
:
f
.
read
(
10
)
f
.
seek
(
10
,
whence
=
1
)
y
=
f
.
read
(
10
)
f
.
close
()
self
.
assertEquals
(
y
,
data1
[
20
:
30
])
def
test_seek_write
(
self
):
# Try seek, write test
f
=
gzip
.
GzipFile
(
self
.
filename
,
'w'
)
with
gzip
.
GzipFile
(
self
.
filename
,
'w'
)
as
f
:
for
pos
in
range
(
0
,
256
,
16
):
f
.
seek
(
pos
)
f
.
write
(
b'GZ
\
n
'
)
f
.
close
()
def
test_mode
(
self
):
self
.
test_write
()
f
=
gzip
.
GzipFile
(
self
.
filename
,
'r'
)
with
gzip
.
GzipFile
(
self
.
filename
,
'r'
)
as
f
:
self
.
assertEqual
(
f
.
myfileobj
.
mode
,
'rb'
)
f
.
close
()
def
test_1647484
(
self
):
for
mode
in
(
'wb'
,
'rb'
):
f
=
gzip
.
GzipFile
(
self
.
filename
,
mode
)
with
gzip
.
GzipFile
(
self
.
filename
,
mode
)
as
f
:
self
.
assertTrue
(
hasattr
(
f
,
"name"
))
self
.
assertEqual
(
f
.
name
,
self
.
filename
)
f
.
close
()
def
test_mtime
(
self
):
mtime
=
123456789
fWrite
=
gzip
.
GzipFile
(
self
.
filename
,
'w'
,
mtime
=
mtime
)
with
gzip
.
GzipFile
(
self
.
filename
,
'w'
,
mtime
=
mtime
)
as
fWrite
:
fWrite
.
write
(
data1
)
fWrite
.
close
()
fRead
=
gzip
.
GzipFile
(
self
.
filename
)
with
gzip
.
GzipFile
(
self
.
filename
)
as
fRead
:
dataRead
=
fRead
.
read
()
self
.
assertEqual
(
dataRead
,
data1
)
self
.
assertTrue
(
hasattr
(
fRead
,
'mtime'
))
self
.
assertEqual
(
fRead
.
mtime
,
mtime
)
fRead
.
close
()
def
test_metadata
(
self
):
mtime
=
123456789
fWrite
=
gzip
.
GzipFile
(
self
.
filename
,
'w'
,
mtime
=
mtime
)
with
gzip
.
GzipFile
(
self
.
filename
,
'w'
,
mtime
=
mtime
)
as
fWrite
:
fWrite
.
write
(
data1
)
fWrite
.
close
()
fRead
=
open
(
self
.
filename
,
'rb'
)
with
open
(
self
.
filename
,
'rb'
)
as
fRead
:
# see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
idBytes
=
fRead
.
read
(
2
)
...
...
@@ -263,8 +252,6 @@ class TestGzip(unittest.TestCase):
isizeBytes
=
fRead
.
read
(
4
)
self
.
assertEqual
(
isizeBytes
,
struct
.
pack
(
'<i'
,
len
(
data1
)))
fRead
.
close
()
def
test_with_open
(
self
):
# GzipFile supports the context management protocol
with
gzip
.
GzipFile
(
self
.
filename
,
"wb"
)
as
f
:
...
...
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