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
b275210a
Commit
b275210a
authored
Apr 27, 2016
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #25788: fileinput.hook_encoded() now supports an "errors" argument
for passing to open. Original patch by Joseph Hackman.
parent
258a5d4d
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
40 additions
and
6 deletions
+40
-6
Doc/library/fileinput.rst
Doc/library/fileinput.rst
+7
-3
Doc/whatsnew/3.6.rst
Doc/whatsnew/3.6.rst
+7
-0
Lib/fileinput.py
Lib/fileinput.py
+2
-2
Lib/test/test_fileinput.py
Lib/test/test_fileinput.py
+20
-1
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/fileinput.rst
View file @
b275210a
...
@@ -193,10 +193,14 @@ The two following opening hooks are provided by this module:
...
@@ -193,10 +193,14 @@ The two following opening hooks are provided by this module:
Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)``
Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)``
.. function:: hook_encoded(encoding)
.. function:: hook_encoded(encoding
, errors=None
)
Returns a hook which opens each file with :func:`open`, using the given
Returns a hook which opens each file with :func:`open`, using the given
*encoding* to read the file.
*encoding*
and *errors*
to read the file.
Usage example: ``fi =
Usage example: ``fi =
fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))``
fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8",
"surrogateescape"))``
.. versionchanged:: 3.6
Added the optional *errors* parameter.
Doc/whatsnew/3.6.rst
View file @
b275210a
...
@@ -358,6 +358,13 @@ The :func:`~zlib.compress` function now accepts keyword arguments.
...
@@ -358,6 +358,13 @@ The :func:`~zlib.compress` function now accepts keyword arguments.
(Contributed by Aviv Palivoda in :issue:`26243`.)
(Contributed by Aviv Palivoda in :issue:`26243`.)
fileinput
---------
:func:`~fileinput.hook_encoded` now supports the *errors* argument.
(Contributed by Joseph Hackman in :issue:`25788`.)
Optimizations
Optimizations
=============
=============
...
...
Lib/fileinput.py
View file @
b275210a
...
@@ -400,9 +400,9 @@ def hook_compressed(filename, mode):
...
@@ -400,9 +400,9 @@ def hook_compressed(filename, mode):
return
open
(
filename
,
mode
)
return
open
(
filename
,
mode
)
def
hook_encoded
(
encoding
):
def
hook_encoded
(
encoding
,
errors
=
None
):
def
openhook
(
filename
,
mode
):
def
openhook
(
filename
,
mode
):
return
open
(
filename
,
mode
,
encoding
=
encoding
)
return
open
(
filename
,
mode
,
encoding
=
encoding
,
errors
=
errors
)
return
openhook
return
openhook
...
...
Lib/test/test_fileinput.py
View file @
b275210a
...
@@ -945,7 +945,8 @@ class Test_hook_encoded(unittest.TestCase):
...
@@ -945,7 +945,8 @@ class Test_hook_encoded(unittest.TestCase):
def
test
(
self
):
def
test
(
self
):
encoding
=
object
()
encoding
=
object
()
result
=
fileinput
.
hook_encoded
(
encoding
)
errors
=
object
()
result
=
fileinput
.
hook_encoded
(
encoding
,
errors
=
errors
)
fake_open
=
InvocationRecorder
()
fake_open
=
InvocationRecorder
()
original_open
=
builtins
.
open
original_open
=
builtins
.
open
...
@@ -963,8 +964,26 @@ class Test_hook_encoded(unittest.TestCase):
...
@@ -963,8 +964,26 @@ class Test_hook_encoded(unittest.TestCase):
self
.
assertIs
(
args
[
0
],
filename
)
self
.
assertIs
(
args
[
0
],
filename
)
self
.
assertIs
(
args
[
1
],
mode
)
self
.
assertIs
(
args
[
1
],
mode
)
self
.
assertIs
(
kwargs
.
pop
(
'encoding'
),
encoding
)
self
.
assertIs
(
kwargs
.
pop
(
'encoding'
),
encoding
)
self
.
assertIs
(
kwargs
.
pop
(
'errors'
),
errors
)
self
.
assertFalse
(
kwargs
)
self
.
assertFalse
(
kwargs
)
def
test_errors
(
self
):
with
open
(
TESTFN
,
'wb'
)
as
f
:
f
.
write
(
b'
\
x80
abc'
)
self
.
addCleanup
(
safe_unlink
,
TESTFN
)
def
check
(
errors
,
expected_lines
):
with
FileInput
(
files
=
TESTFN
,
mode
=
'r'
,
openhook
=
hook_encoded
(
'utf-8'
,
errors
=
errors
))
as
fi
:
lines
=
list
(
fi
)
self
.
assertEqual
(
lines
,
expected_lines
)
check
(
'ignore'
,
[
'abc'
])
with
self
.
assertRaises
(
UnicodeDecodeError
):
check
(
'strict'
,
[
'abc'
])
check
(
'replace'
,
[
'
\
ufffd
abc'
])
check
(
'backslashreplace'
,
[
'
\
\
x80abc'
])
def
test_modes
(
self
):
def
test_modes
(
self
):
with
open
(
TESTFN
,
'wb'
)
as
f
:
with
open
(
TESTFN
,
'wb'
)
as
f
:
# UTF-7 is a convenient, seldom used encoding
# UTF-7 is a convenient, seldom used encoding
...
...
Misc/ACKS
View file @
b275210a
...
@@ -538,6 +538,7 @@ Michael Guravage
...
@@ -538,6 +538,7 @@ Michael Guravage
Lars Gustäbel
Lars Gustäbel
Thomas Güttler
Thomas Güttler
Jonas H.
Jonas H.
Joseph Hackman
Barry Haddow
Barry Haddow
Philipp Hagemeister
Philipp Hagemeister
Paul ten Hagen
Paul ten Hagen
...
...
Misc/NEWS
View file @
b275210a
...
@@ -256,6 +256,9 @@ Core and Builtins
...
@@ -256,6 +256,9 @@ Core and Builtins
Library
Library
-------
-------
-
Issue
#
25788
:
fileinput
.
hook_encoded
()
now
supports
an
"errors"
argument
for
passing
to
open
.
Original
patch
by
Joseph
Hackman
.
-
Issue
#
26634
:
recursive_repr
()
now
sets
__qualname__
of
wrapper
.
Patch
by
-
Issue
#
26634
:
recursive_repr
()
now
sets
__qualname__
of
wrapper
.
Patch
by
Xiang
Zhang
.
Xiang
Zhang
.
...
...
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