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
af518c19
Commit
af518c19
authored
Jun 04, 2012
by
Nadeem Vawda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a function bz2.open(), to match gzip.open().
parent
50cb936b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
184 additions
and
16 deletions
+184
-16
Doc/library/bz2.rst
Doc/library/bz2.rst
+31
-2
Lib/bz2.py
Lib/bz2.py
+44
-3
Lib/test/test_bz2.py
Lib/test/test_bz2.py
+106
-11
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/bz2.rst
View file @
af518c19
...
...
@@ -14,7 +14,8 @@ decompressing data using the bzip2 compression algorithm.
The :mod:`bz2` module contains:
* The :class:`BZ2File` class for reading and writing compressed files.
* The :func:`.open` function and :class:`BZ2File` class for reading and
writing compressed files.
* The :class:`BZ2Compressor` and :class:`BZ2Decompressor` classes for
incremental (de)compression.
* The :func:`compress` and :func:`decompress` functions for one-shot
...
...
@@ -26,9 +27,37 @@ All of the classes in this module may safely be accessed from multiple threads.
(De)compression of files
------------------------
.. function:: open(filename, mode='r', compresslevel=9, encoding=None, errors=None, newline=None)
Open a bzip2-compressed file in binary or text mode, returning a :term:`file
object`.
As with the constructor for :class:`BZ2File`, the *filename* argument can be
an actual filename (a :class:`str` or :class:`bytes` object), or an existing
file object to read from or write to.
The *mode* argument can be any of ``'r'``, ``'rb'``, ``'w'``, ``'wb'``,
``'a'``, or ``'ab'`` for binary mode, or ``'rt'``, ``'wt'``, or ``'at'`` for
text mode. The default is ``'rb'``.
The *compresslevel* argument is an integer from 1 to 9, as for the
:class:`BZ2File` constructor.
For binary mode, this function is equivalent to the :class:`BZ2File`
constructor: ``BZ2File(filename, mode, compresslevel=compresslevel)``. In
this case, the *encoding*, *errors* and *newline* arguments must not be
provided.
For text mode, a :class:`BZ2File` object is created, and wrapped in an
:class:`io.TextIOWrapper` instance with the specified encoding, error
handling behavior, and line ending(s).
.. versionadded:: 3.3
.. class:: BZ2File(filename, mode='r', buffering=None, compresslevel=9)
Open a bzip2-compressed file.
Open a bzip2-compressed file
in binary mode
.
If *filename* is a :class:`str` or :class:`bytes` object, open the named file
directly. Otherwise, *filename* should be a :term:`file object`, which will
...
...
Lib/bz2.py
View file @
af518c19
...
...
@@ -4,11 +4,12 @@ This module provides a file interface, classes for incremental
(de)compression, and functions for one-shot (de)compression.
"""
__all__
=
[
"BZ2File"
,
"BZ2Compressor"
,
"BZ2Decompressor"
,
"compress"
,
"decompress"
]
__all__
=
[
"BZ2File"
,
"BZ2Compressor"
,
"BZ2Decompressor"
,
"
open"
,
"compress"
,
"
decompress"
]
__author__
=
"Nadeem Vawda <nadeem.vawda@gmail.com>"
import
builtins
import
io
import
warnings
...
...
@@ -91,7 +92,7 @@ class BZ2File(io.BufferedIOBase):
raise
ValueError
(
"Invalid mode: {!r}"
.
format
(
mode
))
if
isinstance
(
filename
,
(
str
,
bytes
)):
self
.
_fp
=
open
(
filename
,
mode
)
self
.
_fp
=
builtins
.
open
(
filename
,
mode
)
self
.
_closefp
=
True
self
.
_mode
=
mode_code
elif
hasattr
(
filename
,
"read"
)
or
hasattr
(
filename
,
"write"
):
...
...
@@ -391,6 +392,46 @@ class BZ2File(io.BufferedIOBase):
return
self
.
_pos
def
open
(
filename
,
mode
=
"rb"
,
compresslevel
=
9
,
encoding
=
None
,
errors
=
None
,
newline
=
None
):
"""Open a bzip2-compressed file in binary or text mode.
The filename argument can be an actual filename (a str or bytes object), or
an existing file object to read from or write to.
The mode argument can be "r", "rb", "w", "wb", "a" or "ab" for binary mode,
or "rt", "wt" or "at" for text mode. The default mode is "rb", and the
default compresslevel is 9.
For binary mode, this function is equivalent to the BZ2File constructor:
BZ2File(filename, mode, compresslevel). In this case, the encoding, errors
and newline arguments must not be provided.
For text mode, a BZ2File object is created, and wrapped in an
io.TextIOWrapper instance with the specified encoding, error handling
behavior, and line ending(s).
"""
if
"t"
in
mode
:
if
"b"
in
mode
:
raise
ValueError
(
"Invalid mode: %r"
%
(
mode
,))
else
:
if
encoding
is
not
None
:
raise
ValueError
(
"Argument 'encoding' not supported in binary mode"
)
if
errors
is
not
None
:
raise
ValueError
(
"Argument 'errors' not supported in binary mode"
)
if
newline
is
not
None
:
raise
ValueError
(
"Argument 'newline' not supported in binary mode"
)
bz_mode
=
mode
.
replace
(
"t"
,
""
)
binary_file
=
BZ2File
(
filename
,
bz_mode
,
compresslevel
=
compresslevel
)
if
"t"
in
mode
:
return
io
.
TextIOWrapper
(
binary_file
,
encoding
,
errors
,
newline
)
else
:
return
binary_file
def
compress
(
data
,
compresslevel
=
9
):
"""Compress a block of data.
...
...
Lib/test/test_bz2.py
View file @
af518c19
...
...
@@ -48,6 +48,13 @@ class BaseTest(unittest.TestCase):
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
\
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d8
3]
\
xa9
\
xc6
\
x08
\
x82
f
\
xf6
\
xb9
"6l$
\
xb8
j@
\
xc0
\
x8a
\
xb0
l1..
\
xba
k
\
x83
ls
\
x15
\
xbc
\
xf4
\
xc1
\
x13
\
xbe
\
xf8
E
\
xb8
\
x9d
\
r
\
xa8
\
x9d
k
\
x84
\
xd3
n
\
xfa
\
xac
Q
\
x07
\
xb1
%y
\
xaa
v
\
xb4
\
x08
\
xe0
z
\
x1b
\
x16
\
xf5
\
x04
\
xe9
\
xcc
\
xb9
\
x08
z
\
x1e
n7.G
\
xfc
]
\
xc9
\
x14
\
xe1
B@
\
xbb
!8`'
def
setUp
(
self
):
self
.
filename
=
TESTFN
def
tearDown
(
self
):
if
os
.
path
.
isfile
(
self
.
filename
):
os
.
unlink
(
self
.
filename
)
if
has_cmdline_bunzip2
:
def
decompress
(
self
,
data
):
pop
=
subprocess
.
Popen
(
"bunzip2"
,
shell
=
True
,
...
...
@@ -70,13 +77,6 @@ class BaseTest(unittest.TestCase):
class
BZ2FileTest
(
BaseTest
):
"Test BZ2File type miscellaneous methods."
def
setUp
(
self
):
self
.
filename
=
TESTFN
def
tearDown
(
self
):
if
os
.
path
.
isfile
(
self
.
filename
):
os
.
unlink
(
self
.
filename
)
def
createTempFile
(
self
,
streams
=
1
):
with
open
(
self
.
filename
,
"wb"
)
as
f
:
f
.
write
(
self
.
DATA
*
streams
)
...
...
@@ -650,9 +650,7 @@ class BZ2DecompressorTest(BaseTest):
decompressed
=
None
class
FuncTest
(
BaseTest
):
"Test module functions"
class
CompressDecompressTest
(
BaseTest
):
def
testCompress
(
self
):
data
=
bz2
.
compress
(
self
.
TEXT
)
self
.
assertEqual
(
self
.
decompress
(
data
),
self
.
TEXT
)
...
...
@@ -672,12 +670,109 @@ class FuncTest(BaseTest):
text
=
bz2
.
decompress
(
self
.
DATA
*
5
)
self
.
assertEqual
(
text
,
self
.
TEXT
*
5
)
class
OpenTest
(
BaseTest
):
def
test_binary_modes
(
self
):
with
bz2
.
open
(
self
.
filename
,
"wb"
)
as
f
:
f
.
write
(
self
.
TEXT
)
with
open
(
self
.
filename
,
"rb"
)
as
f
:
file_data
=
bz2
.
decompress
(
f
.
read
())
self
.
assertEqual
(
file_data
,
self
.
TEXT
)
with
bz2
.
open
(
self
.
filename
,
"rb"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
self
.
TEXT
)
with
bz2
.
open
(
self
.
filename
,
"ab"
)
as
f
:
f
.
write
(
self
.
TEXT
)
with
open
(
self
.
filename
,
"rb"
)
as
f
:
file_data
=
bz2
.
decompress
(
f
.
read
())
self
.
assertEqual
(
file_data
,
self
.
TEXT
*
2
)
def
test_implicit_binary_modes
(
self
):
# Test implicit binary modes (no "b" or "t" in mode string).
with
bz2
.
open
(
self
.
filename
,
"w"
)
as
f
:
f
.
write
(
self
.
TEXT
)
with
open
(
self
.
filename
,
"rb"
)
as
f
:
file_data
=
bz2
.
decompress
(
f
.
read
())
self
.
assertEqual
(
file_data
,
self
.
TEXT
)
with
bz2
.
open
(
self
.
filename
,
"r"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
self
.
TEXT
)
with
bz2
.
open
(
self
.
filename
,
"a"
)
as
f
:
f
.
write
(
self
.
TEXT
)
with
open
(
self
.
filename
,
"rb"
)
as
f
:
file_data
=
bz2
.
decompress
(
f
.
read
())
self
.
assertEqual
(
file_data
,
self
.
TEXT
*
2
)
def
test_text_modes
(
self
):
text
=
self
.
TEXT
.
decode
(
"ascii"
)
text_native_eol
=
text
.
replace
(
"
\
n
"
,
os
.
linesep
)
with
bz2
.
open
(
self
.
filename
,
"wt"
)
as
f
:
f
.
write
(
text
)
with
open
(
self
.
filename
,
"rb"
)
as
f
:
file_data
=
bz2
.
decompress
(
f
.
read
()).
decode
(
"ascii"
)
self
.
assertEqual
(
file_data
,
text_native_eol
)
with
bz2
.
open
(
self
.
filename
,
"rt"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
text
)
with
bz2
.
open
(
self
.
filename
,
"at"
)
as
f
:
f
.
write
(
text
)
with
open
(
self
.
filename
,
"rb"
)
as
f
:
file_data
=
bz2
.
decompress
(
f
.
read
()).
decode
(
"ascii"
)
self
.
assertEqual
(
file_data
,
text_native_eol
*
2
)
def
test_fileobj
(
self
):
with
bz2
.
open
(
BytesIO
(
self
.
DATA
),
"r"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
self
.
TEXT
)
with
bz2
.
open
(
BytesIO
(
self
.
DATA
),
"rb"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
self
.
TEXT
)
text
=
self
.
TEXT
.
decode
(
"ascii"
)
with
bz2
.
open
(
BytesIO
(
self
.
DATA
),
"rt"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
text
)
def
test_bad_params
(
self
):
# Test invalid parameter combinations.
with
self
.
assertRaises
(
ValueError
):
bz2
.
open
(
self
.
filename
,
"wbt"
)
with
self
.
assertRaises
(
ValueError
):
bz2
.
open
(
self
.
filename
,
"rb"
,
encoding
=
"utf-8"
)
with
self
.
assertRaises
(
ValueError
):
bz2
.
open
(
self
.
filename
,
"rb"
,
errors
=
"ignore"
)
with
self
.
assertRaises
(
ValueError
):
bz2
.
open
(
self
.
filename
,
"rb"
,
newline
=
"
\
n
"
)
def
test_encoding
(
self
):
# Test non-default encoding.
text
=
self
.
TEXT
.
decode
(
"ascii"
)
text_native_eol
=
text
.
replace
(
"
\
n
"
,
os
.
linesep
)
with
bz2
.
open
(
self
.
filename
,
"wt"
,
encoding
=
"utf-16-le"
)
as
f
:
f
.
write
(
text
)
with
open
(
self
.
filename
,
"rb"
)
as
f
:
file_data
=
bz2
.
decompress
(
f
.
read
()).
decode
(
"utf-16-le"
)
self
.
assertEqual
(
file_data
,
text_native_eol
)
with
bz2
.
open
(
self
.
filename
,
"rt"
,
encoding
=
"utf-16-le"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
text
)
def
test_encoding_error_handler
(
self
):
# Test with non-default encoding error handler.
with
bz2
.
open
(
self
.
filename
,
"wb"
)
as
f
:
f
.
write
(
b"foo
\
xff
bar"
)
with
bz2
.
open
(
self
.
filename
,
"rt"
,
encoding
=
"ascii"
,
errors
=
"ignore"
)
\
as
f
:
self
.
assertEqual
(
f
.
read
(),
"foobar"
)
def
test_newline
(
self
):
# Test with explicit newline (universal newline mode disabled).
text
=
self
.
TEXT
.
decode
(
"ascii"
)
with
bz2
.
open
(
self
.
filename
,
"wt"
,
newline
=
"
\
n
"
)
as
f
:
f
.
write
(
text
)
with
bz2
.
open
(
self
.
filename
,
"rt"
,
newline
=
"
\
r
"
)
as
f
:
self
.
assertEqual
(
f
.
readlines
(),
[
text
])
def
test_main
():
support
.
run_unittest
(
BZ2FileTest
,
BZ2CompressorTest
,
BZ2DecompressorTest
,
FuncTest
CompressDecompressTest
,
OpenTest
,
)
support
.
reap_children
()
...
...
Misc/NEWS
View file @
af518c19
...
...
@@ -15,6 +15,9 @@ Core and Builtins
Library
-------
-
The
bz2
module
now
contains
an
open
()
function
,
allowing
compressed
files
to
conveniently
be
opened
in
text
mode
as
well
as
binary
mode
.
-
BZ2File
.
__init__
()
now
accepts
a
file
object
as
its
first
argument
,
rather
than
requiring
a
separate
"fileobj"
argument
.
...
...
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