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
6cbb20cd
Commit
6cbb20cd
authored
Jun 04, 2012
by
Nadeem Vawda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow LZMAFile to accept modes with a "b" suffix.
parent
33c34da5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
9 deletions
+30
-9
Doc/library/lzma.rst
Doc/library/lzma.rst
+5
-3
Lib/lzma.py
Lib/lzma.py
+6
-4
Lib/test/test_lzma.py
Lib/test/test_lzma.py
+17
-2
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/lzma.rst
View file @
6cbb20cd
...
...
@@ -40,9 +40,11 @@ Reading and writing compressed files
file will not be closed when the :class:`LZMAFile` is closed.
The *mode* argument can be either ``"r"`` for reading (default), ``"w"`` for
overwriting, or ``"a"`` for appending. If *filename* is an existing file
object, a mode of ``"w"`` does not truncate the file, and is instead
equivalent to ``"a"``.
overwriting, or ``"a"`` for appending. These can equivalently be given as
``"rb"``, ``"wb"``, and ``"ab"`` respectively.
If *filename* is a file object (rather than an actual file name), a mode of
``"w"`` does not truncate the file, and is instead equivalent to ``"a"``.
When opening a file for reading, the input file may be the concatenation of
multiple separate compressed streams. These are transparently decoded as a
...
...
Lib/lzma.py
View file @
6cbb20cd
...
...
@@ -54,7 +54,8 @@ class LZMAFile(io.BufferedIOBase):
be an existing file object to read from or write to.
mode can be "r" for reading (default), "w" for (over)writing, or
"a" for appending.
"a" for appending. These can equivalently be given as "rb", "wb",
and "ab" respectively.
format specifies the container format to use for the file.
If mode is "r", this defaults to FORMAT_AUTO. Otherwise, the
...
...
@@ -93,7 +94,7 @@ class LZMAFile(io.BufferedIOBase):
self
.
_pos
=
0
self
.
_size
=
-
1
if
mode
==
"r"
:
if
mode
in
(
"r"
,
"rb"
)
:
if
check
!=
-
1
:
raise
ValueError
(
"Cannot specify an integrity check "
"when opening a file for reading"
)
...
...
@@ -109,7 +110,7 @@ class LZMAFile(io.BufferedIOBase):
self
.
_init_args
=
{
"format"
:
format
,
"filters"
:
filters
}
self
.
_decompressor
=
LZMADecompressor
(
**
self
.
_init_args
)
self
.
_buffer
=
None
elif
mode
in
(
"w"
,
"
a
"
):
elif
mode
in
(
"w"
,
"
wb"
,
"a"
,
"ab
"
):
if
format
is
None
:
format
=
FORMAT_XZ
mode_code
=
_MODE_WRITE
...
...
@@ -119,7 +120,8 @@ class LZMAFile(io.BufferedIOBase):
raise
ValueError
(
"Invalid mode: {!r}"
.
format
(
mode
))
if
isinstance
(
filename
,
(
str
,
bytes
)):
mode
+=
"b"
if
"b"
not
in
mode
:
mode
+=
"b"
self
.
_fp
=
open
(
filename
,
mode
)
self
.
_closefp
=
True
self
.
_mode
=
mode_code
...
...
Lib/test/test_lzma.py
View file @
6cbb20cd
...
...
@@ -374,6 +374,21 @@ class FileTestCase(unittest.TestCase):
with
LZMAFile
(
TESTFN
,
"a"
)
as
f
:
pass
def
test_init_mode
(
self
):
with
TempFile
(
TESTFN
):
with
LZMAFile
(
TESTFN
,
"r"
):
pass
with
LZMAFile
(
TESTFN
,
"rb"
):
pass
with
LZMAFile
(
TESTFN
,
"w"
):
pass
with
LZMAFile
(
TESTFN
,
"wb"
):
pass
with
LZMAFile
(
TESTFN
,
"a"
):
pass
with
LZMAFile
(
TESTFN
,
"ab"
):
pass
def
test_init_bad_mode
(
self
):
with
self
.
assertRaises
(
ValueError
):
LZMAFile
(
BytesIO
(
COMPRESSED_XZ
),
(
3
,
"x"
))
...
...
@@ -382,11 +397,11 @@ class FileTestCase(unittest.TestCase):
with
self
.
assertRaises
(
ValueError
):
LZMAFile
(
BytesIO
(
COMPRESSED_XZ
),
"x"
)
with
self
.
assertRaises
(
ValueError
):
LZMAFile
(
BytesIO
(
COMPRESSED_XZ
),
"r
b
"
)
LZMAFile
(
BytesIO
(
COMPRESSED_XZ
),
"r
t
"
)
with
self
.
assertRaises
(
ValueError
):
LZMAFile
(
BytesIO
(
COMPRESSED_XZ
),
"r+"
)
with
self
.
assertRaises
(
ValueError
):
LZMAFile
(
BytesIO
(
COMPRESSED_XZ
),
"w
b
"
)
LZMAFile
(
BytesIO
(
COMPRESSED_XZ
),
"w
t
"
)
with
self
.
assertRaises
(
ValueError
):
LZMAFile
(
BytesIO
(
COMPRESSED_XZ
),
"w+"
)
with
self
.
assertRaises
(
ValueError
):
...
...
Misc/NEWS
View file @
6cbb20cd
...
...
@@ -15,6 +15,8 @@ Core and Builtins
Library
-------
-
LZMAFile
now
accepts
the
modes
"rb"
/
"wb"
/
"ab"
as
synonyms
of
"r"
/
"w"
/
"a"
.
-
The
bz2
module
now
contains
an
open
()
function
,
allowing
compressed
files
to
conveniently
be
opened
in
text
mode
as
well
as
binary
mode
.
...
...
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