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
1de19ac7
Commit
1de19ac7
authored
Dec 04, 2013
by
Nadeem Vawda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#19839: Fix regression in bz2 module's handling of non-bzip2 data at EOF.
parent
a5cc9d68
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
11 deletions
+51
-11
Lib/bz2.py
Lib/bz2.py
+19
-10
Lib/test/test_bz2.py
Lib/test/test_bz2.py
+29
-1
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/bz2.py
View file @
1de19ac7
...
@@ -199,7 +199,14 @@ class BZ2File(io.BufferedIOBase):
...
@@ -199,7 +199,14 @@ class BZ2File(io.BufferedIOBase):
# Continue to next stream.
# Continue to next stream.
if
self
.
_decompressor
.
eof
:
if
self
.
_decompressor
.
eof
:
self
.
_decompressor
=
BZ2Decompressor
()
self
.
_decompressor
=
BZ2Decompressor
()
try
:
self
.
_buffer
=
self
.
_decompressor
.
decompress
(
rawblock
)
except
OSError
:
# Trailing data isn't a valid bzip2 stream. We're done here.
self
.
_mode
=
_MODE_READ_EOF
self
.
_size
=
self
.
_pos
return
False
else
:
self
.
_buffer
=
self
.
_decompressor
.
decompress
(
rawblock
)
self
.
_buffer
=
self
.
_decompressor
.
decompress
(
rawblock
)
self
.
_buffer_offset
=
0
self
.
_buffer_offset
=
0
return
True
return
True
...
@@ -488,17 +495,19 @@ def decompress(data):
...
@@ -488,17 +495,19 @@ def decompress(data):
For incremental decompression, use a BZ2Decompressor object instead.
For incremental decompression, use a BZ2Decompressor object instead.
"""
"""
if
len
(
data
)
==
0
:
return
b""
results
=
[]
results
=
[]
while
True
:
while
data
:
decomp
=
BZ2Decompressor
()
decomp
=
BZ2Decompressor
()
results
.
append
(
decomp
.
decompress
(
data
))
try
:
res
=
decomp
.
decompress
(
data
)
except
OSError
:
if
results
:
break
# Leftover data is not a valid bzip2 stream; ignore it.
else
:
raise
# Error on the first iteration; bail out.
results
.
append
(
res
)
if
not
decomp
.
eof
:
if
not
decomp
.
eof
:
raise
ValueError
(
"Compressed data ended before the "
raise
ValueError
(
"Compressed data ended before the "
"end-of-stream marker was reached"
)
"end-of-stream marker was reached"
)
if
not
decomp
.
unused_data
:
return
b""
.
join
(
results
)
# There is unused data left over. Proceed to next stream.
data
=
decomp
.
unused_data
data
=
decomp
.
unused_data
return
b""
.
join
(
results
)
Lib/test/test_bz2.py
View file @
1de19ac7
...
@@ -49,6 +49,7 @@ class BaseTest(unittest.TestCase):
...
@@ -49,6 +49,7 @@ class BaseTest(unittest.TestCase):
TEXT
=
b''
.
join
(
TEXT_LINES
)
TEXT
=
b''
.
join
(
TEXT_LINES
)
DATA
=
b'BZh91AY&SY.
\
xc8
N
\
x18
\
x00
\
x01
>_
\
x80
\
x00
\
x10
@
\
x02
\
xff
\
xf0
\
x01
\
x07
n
\
x00
?
\
xe7
\
xff
\
xe0
0
\
x01
\
x99
\
xaa
\
x00
\
xc0
\
x03
F
\
x86
\
x8c
#&
\
x83
F
\
x9a
\
x03
\
x06
\
xa6
\
xd0
\
xa6
\
x93
M
\
x0f
Q
\
xa7
\
xa8
\
x06
\
x80
4hh
\
x12
$
\
x11
\
xa4
i4
\
xf1
4S
\
xd2
<Q
\
xb5
\
x0f
H
\
xd3
\
xd4
\
xdd
\
xd5
\
x87
\
xbb
\
xf8
\
x94
\
r
\
x8f
\
xaf
I
\
x12
\
xe1
\
xc9
\
xf8
/E
\
x00
pu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
x90
\
x00
\
x00
\
x00
\
x00
'
BAD_DATA
=
b'this is not a valid bzip2 file'
def
setUp
(
self
):
def
setUp
(
self
):
self
.
filename
=
TESTFN
self
.
filename
=
TESTFN
...
@@ -79,9 +80,10 @@ class BaseTest(unittest.TestCase):
...
@@ -79,9 +80,10 @@ class BaseTest(unittest.TestCase):
class
BZ2FileTest
(
BaseTest
):
class
BZ2FileTest
(
BaseTest
):
"Test BZ2File type miscellaneous methods."
"Test BZ2File type miscellaneous methods."
def
createTempFile
(
self
,
streams
=
1
):
def
createTempFile
(
self
,
streams
=
1
,
suffix
=
b""
):
with
open
(
self
.
filename
,
"wb"
)
as
f
:
with
open
(
self
.
filename
,
"wb"
)
as
f
:
f
.
write
(
self
.
DATA
*
streams
)
f
.
write
(
self
.
DATA
*
streams
)
f
.
write
(
suffix
)
def
testBadArgs
(
self
):
def
testBadArgs
(
self
):
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
...
@@ -103,6 +105,11 @@ class BZ2FileTest(BaseTest):
...
@@ -103,6 +105,11 @@ class BZ2FileTest(BaseTest):
self
.
assertRaises
(
TypeError
,
bz2f
.
read
,
None
)
self
.
assertRaises
(
TypeError
,
bz2f
.
read
,
None
)
self
.
assertEqual
(
bz2f
.
read
(),
self
.
TEXT
)
self
.
assertEqual
(
bz2f
.
read
(),
self
.
TEXT
)
def
testReadBadFile
(
self
):
self
.
createTempFile
(
streams
=
0
,
suffix
=
self
.
BAD_DATA
)
with
BZ2File
(
self
.
filename
)
as
bz2f
:
self
.
assertRaises
(
OSError
,
bz2f
.
read
)
def
testReadMultiStream
(
self
):
def
testReadMultiStream
(
self
):
self
.
createTempFile
(
streams
=
5
)
self
.
createTempFile
(
streams
=
5
)
with
BZ2File
(
self
.
filename
)
as
bz2f
:
with
BZ2File
(
self
.
filename
)
as
bz2f
:
...
@@ -122,6 +129,16 @@ class BZ2FileTest(BaseTest):
...
@@ -122,6 +129,16 @@ class BZ2FileTest(BaseTest):
finally
:
finally
:
bz2
.
_BUFFER_SIZE
=
buffer_size
bz2
.
_BUFFER_SIZE
=
buffer_size
def
testReadTrailingJunk
(
self
):
self
.
createTempFile
(
suffix
=
self
.
BAD_DATA
)
with
BZ2File
(
self
.
filename
)
as
bz2f
:
self
.
assertEqual
(
bz2f
.
read
(),
self
.
TEXT
)
def
testReadMultiStreamTrailingJunk
(
self
):
self
.
createTempFile
(
streams
=
5
,
suffix
=
self
.
BAD_DATA
)
with
BZ2File
(
self
.
filename
)
as
bz2f
:
self
.
assertEqual
(
bz2f
.
read
(),
self
.
TEXT
*
5
)
def
testRead0
(
self
):
def
testRead0
(
self
):
self
.
createTempFile
()
self
.
createTempFile
()
with
BZ2File
(
self
.
filename
)
as
bz2f
:
with
BZ2File
(
self
.
filename
)
as
bz2f
:
...
@@ -707,10 +724,21 @@ class CompressDecompressTest(BaseTest):
...
@@ -707,10 +724,21 @@ class CompressDecompressTest(BaseTest):
def
testDecompressIncomplete
(
self
):
def
testDecompressIncomplete
(
self
):
self
.
assertRaises
(
ValueError
,
bz2
.
decompress
,
self
.
DATA
[:
-
10
])
self
.
assertRaises
(
ValueError
,
bz2
.
decompress
,
self
.
DATA
[:
-
10
])
def
testDecompressBadData
(
self
):
self
.
assertRaises
(
OSError
,
bz2
.
decompress
,
self
.
BAD_DATA
)
def
testDecompressMultiStream
(
self
):
def
testDecompressMultiStream
(
self
):
text
=
bz2
.
decompress
(
self
.
DATA
*
5
)
text
=
bz2
.
decompress
(
self
.
DATA
*
5
)
self
.
assertEqual
(
text
,
self
.
TEXT
*
5
)
self
.
assertEqual
(
text
,
self
.
TEXT
*
5
)
def
testDecompressTrailingJunk
(
self
):
text
=
bz2
.
decompress
(
self
.
DATA
+
self
.
BAD_DATA
)
self
.
assertEqual
(
text
,
self
.
TEXT
)
def
testDecompressMultiStreamTrailingJunk
(
self
):
text
=
bz2
.
decompress
(
self
.
DATA
*
5
+
self
.
BAD_DATA
)
self
.
assertEqual
(
text
,
self
.
TEXT
*
5
)
class
OpenTest
(
BaseTest
):
class
OpenTest
(
BaseTest
):
def
test_binary_modes
(
self
):
def
test_binary_modes
(
self
):
...
...
Misc/NEWS
View file @
1de19ac7
...
@@ -18,6 +18,9 @@ Core and Builtins
...
@@ -18,6 +18,9 @@ Core and Builtins
Library
Library
-------
-------
- Issue #19839: Fix regression in bz2 module'
s
handling
of
non
-
bzip2
data
at
EOF
.
-
Issue
#
19138
:
doctest
's IGNORE_EXCEPTION_DETAIL now allows a match when
-
Issue
#
19138
:
doctest
's IGNORE_EXCEPTION_DETAIL now allows a match when
no exception detail exists (no colon following the exception'
s
name
,
or
no exception detail exists (no colon following the exception'
s
name
,
or
a
colon
does
follow
but
no
text
follows
the
colon
).
a
colon
does
follow
but
no
text
follows
the
colon
).
...
...
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