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
68721019
Commit
68721019
authored
Jun 04, 2012
by
Nadeem Vawda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add fileobj support to gzip.open().
parent
d7b7c747
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
10 deletions
+38
-10
Doc/library/gzip.rst
Doc/library/gzip.rst
+11
-9
Lib/gzip.py
Lib/gzip.py
+12
-1
Lib/test/test_gzip.py
Lib/test/test_gzip.py
+13
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/gzip.rst
View file @
68721019
...
...
@@ -14,10 +14,10 @@ like the GNU programs :program:`gzip` and :program:`gunzip` would.
The data compression is provided by the :mod:`zlib` module.
The :mod:`gzip` module provides the :class:`GzipFile` class, as well as the
:func:`
gzip.open`, :func:`compress` and :func:`decompress` convenience
functions. The :class:`GzipFile` class reads and writes :program:`gzip`\ -format
files, automatically compressing or decompressing the data so that it looks like
an
ordinary :term:`file object`.
:func:`
.open`, :func:`compress` and :func:`decompress` convenience functions.
The :class:`GzipFile` class reads and writes :program:`gzip`\ -format files,
automatically compressing or decompressing the data so that it looks like an
ordinary :term:`file object`.
Note that additional file formats which can be decompressed by the
:program:`gzip` and :program:`gunzip` programs, such as those produced by
...
...
@@ -28,9 +28,11 @@ The module defines the following items:
.. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
Open *filename* as a gzip-compressed file in binary or text mode.
Open a gzip-compressed file in binary or text mode, returning a :term:`file
object`.
Returns a :term:`file object`.
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'``, ``'a'``, ``'ab'``,
``'w'``, or ``'wb'`` for binary mode, or ``'rt'``, ``'at'``, or ``'wt'`` for
...
...
@@ -48,8 +50,8 @@ The module defines the following items:
handling behavior, and line ending(s).
.. versionchanged:: 3.3
Support for text mode was added, along with the *encoding*, *errors* and
*newline* arguments.
Added support for *filename* being a file object, support for text mode,
and the *encoding*, *errors* and
*newline* arguments.
.. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
...
...
@@ -75,7 +77,7 @@ The module defines the following items:
is the mode of *fileobj* if discernible; otherwise, the default is ``'rb'``.
Note that the file is always opened in binary mode. To open a compressed file
in text mode, use :func:`
gzip
.open` (or wrap your :class:`GzipFile` with an
in text mode, use :func:`.open` (or wrap your :class:`GzipFile` with an
:class:`io.TextIOWrapper`).
The *compresslevel* argument is an integer from ``1`` to ``9`` controlling the
...
...
Lib/gzip.py
View file @
68721019
...
...
@@ -20,6 +20,9 @@ def open(filename, mode="rb", compresslevel=9,
encoding
=
None
,
errors
=
None
,
newline
=
None
):
"""Open a gzip-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.
...
...
@@ -43,7 +46,15 @@ def open(filename, mode="rb", compresslevel=9,
raise
ValueError
(
"Argument 'errors' not supported in binary mode"
)
if
newline
is
not
None
:
raise
ValueError
(
"Argument 'newline' not supported in binary mode"
)
binary_file
=
GzipFile
(
filename
,
mode
.
replace
(
"t"
,
""
),
compresslevel
)
gz_mode
=
mode
.
replace
(
"t"
,
""
)
if
isinstance
(
filename
,
(
str
,
bytes
)):
binary_file
=
GzipFile
(
filename
,
gz_mode
,
compresslevel
)
elif
hasattr
(
filename
,
"read"
)
or
hasattr
(
filename
,
"write"
):
binary_file
=
GzipFile
(
None
,
gz_mode
,
compresslevel
,
filename
)
else
:
raise
TypeError
(
"filename must be a str or bytes object, or a file"
)
if
"t"
in
mode
:
return
io
.
TextIOWrapper
(
binary_file
,
encoding
,
errors
,
newline
)
else
:
...
...
Lib/test/test_gzip.py
View file @
68721019
...
...
@@ -424,8 +424,21 @@ class TestOpen(BaseTest):
file_data
=
gzip
.
decompress
(
f
.
read
()).
decode
(
"ascii"
)
self
.
assertEqual
(
file_data
,
uncompressed_raw
*
2
)
def
test_fileobj
(
self
):
uncompressed_bytes
=
data1
*
50
uncompressed_str
=
uncompressed_bytes
.
decode
(
"ascii"
)
compressed
=
gzip
.
compress
(
uncompressed_bytes
)
with
gzip
.
open
(
io
.
BytesIO
(
compressed
),
"r"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
uncompressed_bytes
)
with
gzip
.
open
(
io
.
BytesIO
(
compressed
),
"rb"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
uncompressed_bytes
)
with
gzip
.
open
(
io
.
BytesIO
(
compressed
),
"rt"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
uncompressed_str
)
def
test_bad_params
(
self
):
# Test invalid parameter combinations.
with
self
.
assertRaises
(
TypeError
):
gzip
.
open
(
123.456
)
with
self
.
assertRaises
(
ValueError
):
gzip
.
open
(
self
.
filename
,
"wbt"
)
with
self
.
assertRaises
(
ValueError
):
...
...
Misc/NEWS
View file @
68721019
...
...
@@ -15,6 +15,8 @@ Core and Builtins
Library
-------
-
gzip
.
open
()
now
accepts
file
objects
as
well
as
filenames
.
-
Issue
#
14992
:
os
.
makedirs
(
path
,
exist_ok
=
True
)
would
raise
an
OSError
when
the
path
existed
and
had
the
S_ISGID
mode
bit
set
when
it
was
not
explicitly
asked
for
.
This
is
no
longer
an
exception
as
mkdir
...
...
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