Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b275210

Browse files
Issue #25788: fileinput.hook_encoded() now supports an "errors" argument
for passing to open. Original patch by Joseph Hackman.
1 parent 258a5d4 commit b275210

6 files changed

Lines changed: 40 additions & 6 deletions

File tree

Doc/library/fileinput.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,14 @@ The two following opening hooks are provided by this module:
193193
Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)``
194194

195195

196-
.. function:: hook_encoded(encoding)
196+
.. function:: hook_encoded(encoding, errors=None)
197197

198198
Returns a hook which opens each file with :func:`open`, using the given
199-
*encoding* to read the file.
199+
*encoding* and *errors* to read the file.
200200

201201
Usage example: ``fi =
202-
fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))``
202+
fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8",
203+
"surrogateescape"))``
204+
205+
.. versionchanged:: 3.6
206+
Added the optional *errors* parameter.

Doc/whatsnew/3.6.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ The :func:`~zlib.compress` function now accepts keyword arguments.
358358
(Contributed by Aviv Palivoda in :issue:`26243`.)
359359

360360

361+
fileinput
362+
---------
363+
364+
:func:`~fileinput.hook_encoded` now supports the *errors* argument.
365+
(Contributed by Joseph Hackman in :issue:`25788`.)
366+
367+
361368
Optimizations
362369
=============
363370

Lib/fileinput.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,9 @@ def hook_compressed(filename, mode):
400400
return open(filename, mode)
401401

402402

403-
def hook_encoded(encoding):
403+
def hook_encoded(encoding, errors=None):
404404
def openhook(filename, mode):
405-
return open(filename, mode, encoding=encoding)
405+
return open(filename, mode, encoding=encoding, errors=errors)
406406
return openhook
407407

408408

Lib/test/test_fileinput.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ class Test_hook_encoded(unittest.TestCase):
945945

946946
def test(self):
947947
encoding = object()
948-
result = fileinput.hook_encoded(encoding)
948+
errors = object()
949+
result = fileinput.hook_encoded(encoding, errors=errors)
949950

950951
fake_open = InvocationRecorder()
951952
original_open = builtins.open
@@ -963,8 +964,26 @@ def test(self):
963964
self.assertIs(args[0], filename)
964965
self.assertIs(args[1], mode)
965966
self.assertIs(kwargs.pop('encoding'), encoding)
967+
self.assertIs(kwargs.pop('errors'), errors)
966968
self.assertFalse(kwargs)
967969

970+
def test_errors(self):
971+
with open(TESTFN, 'wb') as f:
972+
f.write(b'\x80abc')
973+
self.addCleanup(safe_unlink, TESTFN)
974+
975+
def check(errors, expected_lines):
976+
with FileInput(files=TESTFN, mode='r',
977+
openhook=hook_encoded('utf-8', errors=errors)) as fi:
978+
lines = list(fi)
979+
self.assertEqual(lines, expected_lines)
980+
981+
check('ignore', ['abc'])
982+
with self.assertRaises(UnicodeDecodeError):
983+
check('strict', ['abc'])
984+
check('replace', ['\ufffdabc'])
985+
check('backslashreplace', ['\\x80abc'])
986+
968987
def test_modes(self):
969988
with open(TESTFN, 'wb') as f:
970989
# UTF-7 is a convenient, seldom used encoding

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ Michael Guravage
538538
Lars Gustäbel
539539
Thomas Güttler
540540
Jonas H.
541+
Joseph Hackman
541542
Barry Haddow
542543
Philipp Hagemeister
543544
Paul ten Hagen

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ Core and Builtins
256256
Library
257257
-------
258258

259+
- Issue #25788: fileinput.hook_encoded() now supports an "errors" argument
260+
for passing to open. Original patch by Joseph Hackman.
261+
259262
- Issue #26634: recursive_repr() now sets __qualname__ of wrapper. Patch by
260263
Xiang Zhang.
261264

0 commit comments

Comments
 (0)