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
f77dd81b
Commit
f77dd81b
authored
Aug 17, 2010
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #3488: Provide convenient shorthand functions `gzip.compress`
and `gzip.decompress`. Original patch by Anand B. Pillai.
parent
bbfd4624
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
58 additions
and
1 deletion
+58
-1
Doc/library/gzip.rst
Doc/library/gzip.rst
+16
-0
Lib/gzip.py
Lib/gzip.py
+18
-1
Lib/test/test_gzip.py
Lib/test/test_gzip.py
+20
-0
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/gzip.rst
View file @
f77dd81b
...
@@ -82,6 +82,17 @@ The module defines the following items:
...
@@ -82,6 +82,17 @@ The module defines the following items:
The *filename* argument is required; *mode* defaults to ``'rb'`` and
The *filename* argument is required; *mode* defaults to ``'rb'`` and
*compresslevel* defaults to ``9``.
*compresslevel* defaults to ``9``.
.. function:: compress(data, compresslevel=9)
Compress the *data*, returning a :class:`bytes` object containing
the compressed data. *compresslevel* has the same meaning as in
the :class:`GzipFile` constructor above.
.. function:: decompress(data)
Decompress the *data*, returning a :class:`bytes` object containing the
uncompressed data.
.. _gzip-usage-examples:
.. _gzip-usage-examples:
...
@@ -112,6 +123,11 @@ Example of how to GZIP compress an existing file::
...
@@ -112,6 +123,11 @@ Example of how to GZIP compress an existing file::
f_out.close()
f_out.close()
f_in.close()
f_in.close()
Example of how to GZIP compress a binary string::
import gzip
s_in = b"Lots of content here"
s_out = gzip.compress(s_in)
.. seealso::
.. seealso::
...
...
Lib/gzip.py
View file @
f77dd81b
...
@@ -10,7 +10,7 @@ import zlib
...
@@ -10,7 +10,7 @@ import zlib
import
builtins
import
builtins
import
io
import
io
__all__
=
[
"GzipFile"
,
"open
"
]
__all__
=
[
"GzipFile"
,
"open"
,
"compress"
,
"decompress
"
]
FTEXT
,
FHCRC
,
FEXTRA
,
FNAME
,
FCOMMENT
=
1
,
2
,
4
,
8
,
16
FTEXT
,
FHCRC
,
FEXTRA
,
FNAME
,
FCOMMENT
=
1
,
2
,
4
,
8
,
16
...
@@ -476,6 +476,23 @@ class GzipFile(io.BufferedIOBase):
...
@@ -476,6 +476,23 @@ class GzipFile(io.BufferedIOBase):
return
b''
.
join
(
bufs
)
# Return resulting line
return
b''
.
join
(
bufs
)
# Return resulting line
def
compress
(
data
,
compresslevel
=
9
):
"""Compress data in one shot and return the compressed string.
Optional argument is the compression level, in range of 1-9.
"""
buf
=
io
.
BytesIO
()
with
GzipFile
(
fileobj
=
buf
,
mode
=
'wb'
,
compresslevel
=
compresslevel
)
as
f
:
f
.
write
(
data
)
return
buf
.
getvalue
()
def
decompress
(
data
):
"""Decompress a gzip compressed string in one shot.
Return the decompressed string.
"""
with
GzipFile
(
fileobj
=
io
.
BytesIO
(
data
))
as
f
:
return
f
.
read
()
def
_test
():
def
_test
():
# Act like gzip; with -d, act like gunzip.
# Act like gzip; with -d, act like gunzip.
# The input file is not deleted, however, nor are any other gzip
# The input file is not deleted, however, nor are any other gzip
...
...
Lib/test/test_gzip.py
View file @
f77dd81b
...
@@ -265,6 +265,26 @@ class TestGzip(unittest.TestCase):
...
@@ -265,6 +265,26 @@ class TestGzip(unittest.TestCase):
d
=
f
.
read
()
d
=
f
.
read
()
self
.
assertEqual
(
d
,
data1
*
50
,
"Incorrect data in file"
)
self
.
assertEqual
(
d
,
data1
*
50
,
"Incorrect data in file"
)
# Testing compress/decompress shortcut functions
def
test_compress
(
self
):
for
data
in
[
data1
,
data2
]:
for
args
in
[(),
(
1
,),
(
6
,),
(
9
,)]:
datac
=
gzip
.
compress
(
data
,
*
args
)
self
.
assertEqual
(
type
(
datac
),
bytes
)
with
gzip
.
GzipFile
(
fileobj
=
io
.
BytesIO
(
datac
),
mode
=
"rb"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
data
)
def
test_decompress
(
self
):
for
data
in
(
data1
,
data2
):
buf
=
io
.
BytesIO
()
with
gzip
.
GzipFile
(
fileobj
=
buf
,
mode
=
"wb"
)
as
f
:
f
.
write
(
data
)
self
.
assertEqual
(
gzip
.
decompress
(
buf
.
getvalue
()),
data
)
# Roundtrip with compress
datac
=
gzip
.
compress
(
data
)
self
.
assertEqual
(
gzip
.
decompress
(
datac
),
data
)
def
test_main
(
verbose
=
None
):
def
test_main
(
verbose
=
None
):
support
.
run_unittest
(
TestGzip
)
support
.
run_unittest
(
TestGzip
)
...
...
Misc/ACKS
View file @
f77dd81b
...
@@ -634,6 +634,7 @@ Neale Pickett
...
@@ -634,6 +634,7 @@ Neale Pickett
Jim St. Pierre
Jim St. Pierre
Dan Pierson
Dan Pierson
Martijn Pieters
Martijn Pieters
Anand B. Pillai
François Pinard
François Pinard
Zach Pincus
Zach Pincus
Michael Piotrowski
Michael Piotrowski
...
...
Misc/NEWS
View file @
f77dd81b
...
@@ -95,6 +95,9 @@ Extensions
...
@@ -95,6 +95,9 @@ Extensions
Library
Library
-------
-------
- Issue #3488: Provide convenient shorthand functions ``gzip.compress``
and ``gzip.decompress``. Original patch by Anand B. Pillai.
- Issue #8807: poplib.POP3_SSL class now accepts a context parameter, which is a
- Issue #8807: poplib.POP3_SSL class now accepts a context parameter, which is a
ssl.SSLContext object allowing bundling SSL configuration options,
ssl.SSLContext object allowing bundling SSL configuration options,
certificates and private keys into a single (potentially long-lived)
certificates and private keys into a single (potentially long-lived)
...
...
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