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
8bdd4480
Commit
8bdd4480
authored
Oct 02, 2016
by
Berker Peksag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #28225: bz2 module now supports pathlib
Initial patch by Ethan Furman.
parent
03020cfa
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
7 deletions
+25
-7
Doc/library/bz2.rst
Doc/library/bz2.rst
+6
-0
Lib/bz2.py
Lib/bz2.py
+9
-7
Lib/test/test_bz2.py
Lib/test/test_bz2.py
+8
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/bz2.rst
View file @
8bdd4480
...
...
@@ -61,6 +61,9 @@ All of the classes in this module may safely be accessed from multiple threads.
.. versionchanged:: 3.4
The ``'x'`` (exclusive creation) mode was added.
.. versionchanged:: 3.6
Accepts a :term:`path-like object`.
.. class:: BZ2File(filename, mode='r', buffering=None, compresslevel=9)
...
...
@@ -128,6 +131,9 @@ All of the classes in this module may safely be accessed from multiple threads.
The :meth:`~io.BufferedIOBase.read` method now accepts an argument of
``None``.
.. versionchanged:: 3.6
Accepts a :term:`path-like object`.
Incremental (de)compression
---------------------------
...
...
Lib/bz2.py
View file @
8bdd4480
...
...
@@ -11,6 +11,7 @@ __author__ = "Nadeem Vawda <nadeem.vawda@gmail.com>"
from
builtins
import
open
as
_builtin_open
import
io
import
os
import
warnings
import
_compression
...
...
@@ -42,9 +43,9 @@ class BZ2File(_compression.BaseStream):
def
__init__
(
self
,
filename
,
mode
=
"r"
,
buffering
=
None
,
compresslevel
=
9
):
"""Open a bzip2-compressed file.
If filename is a str
or bytes object, it gives the nam
e
of the file to be opened. Otherwise, it should be a file object,
which will be used to read or write the compressed data.
If filename is a str
, bytes, or PathLike object, it gives th
e
name of the file to be opened. Otherwise, it should be a file
object,
which will be used to read or write the compressed data.
mode can be 'r' for reading (default), 'w' for (over)writing,
'x' for creating exclusively, or 'a' for appending. These can
...
...
@@ -91,7 +92,7 @@ class BZ2File(_compression.BaseStream):
else
:
raise
ValueError
(
"Invalid mode: %r"
%
(
mode
,))
if
isinstance
(
filename
,
(
str
,
bytes
)):
if
isinstance
(
filename
,
(
str
,
bytes
,
os
.
PathLike
)):
self
.
_fp
=
_builtin_open
(
filename
,
mode
)
self
.
_closefp
=
True
self
.
_mode
=
mode_code
...
...
@@ -99,7 +100,7 @@ class BZ2File(_compression.BaseStream):
self
.
_fp
=
filename
self
.
_mode
=
mode_code
else
:
raise
TypeError
(
"filename must be a str
or bytes object, or a file
"
)
raise
TypeError
(
"filename must be a str
, bytes, file or PathLike object
"
)
if
self
.
_mode
==
_MODE_READ
:
raw
=
_compression
.
DecompressReader
(
self
.
_fp
,
...
...
@@ -289,8 +290,9 @@ 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 filename argument can be an actual filename (a str, bytes, or
PathLike object), or an existing file object to read from or write
to.
The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or
"ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode.
...
...
Lib/test/test_bz2.py
View file @
8bdd4480
...
...
@@ -6,6 +6,7 @@ from io import BytesIO, DEFAULT_BUFFER_SIZE
import
os
import
pickle
import
glob
import
pathlib
import
random
import
subprocess
import
sys
...
...
@@ -560,6 +561,13 @@ class BZ2FileTest(BaseTest):
with
BZ2File
(
str_filename
,
"rb"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
self
.
DATA
)
def
testOpenPathLikeFilename
(
self
):
filename
=
pathlib
.
Path
(
self
.
filename
)
with
BZ2File
(
filename
,
"wb"
)
as
f
:
f
.
write
(
self
.
DATA
)
with
BZ2File
(
filename
,
"rb"
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
self
.
DATA
)
def
testDecompressLimited
(
self
):
"""Decompressed data buffering should be limited"""
bomb
=
bz2
.
compress
(
b'
\
0
'
*
int
(
2e6
),
compresslevel
=
9
)
...
...
Misc/NEWS
View file @
8bdd4480
...
...
@@ -46,6 +46,8 @@ Core and Builtins
Library
-------
- Issue #28225: bz2 module now supports pathlib. Initial patch by Ethan Furman.
- Issue #28227: gzip now supports pathlib. Patch by Ethan Furman.
- Issue #27358: Optimized merging var-keyword arguments and improved error
...
...
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