Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10945)

Side by Side Diff: Lib/test/test_fileinput.py

Issue 25788: fileinput.hook_encoded has no way to pass arguments to codecs
Patch Set: Created 3 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« Lib/fileinput.py ('K') | « Lib/fileinput.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 ''' 1 '''
2 Tests for fileinput module. 2 Tests for fileinput module.
3 Nick Mathewson 3 Nick Mathewson
4 ''' 4 '''
5 import os 5 import os
6 import sys 6 import sys
7 import re 7 import re
8 import fileinput 8 import fileinput
9 import collections 9 import collections
10 import builtins 10 import builtins
(...skipping 855 matching lines...) Expand 10 before | Expand all | Expand 10 after
866 def replace_builtin_open(new_open_func): 866 def replace_builtin_open(new_open_func):
867 original_open = builtins.open 867 original_open = builtins.open
868 builtins.open = new_open_func 868 builtins.open = new_open_func
869 return original_open 869 return original_open
870 870
871 class Test_hook_encoded(unittest.TestCase): 871 class Test_hook_encoded(unittest.TestCase):
872 """Unit tests for fileinput.hook_encoded()""" 872 """Unit tests for fileinput.hook_encoded()"""
873 873
874 def test(self): 874 def test(self):
875 encoding = object() 875 encoding = object()
876 result = fileinput.hook_encoded(encoding) 876 errors = object()
877 result = fileinput.hook_encoded(encoding, errors=errors)
877 878
878 fake_open = InvocationRecorder() 879 fake_open = InvocationRecorder()
879 original_open = builtins.open 880 original_open = builtins.open
880 builtins.open = fake_open 881 builtins.open = fake_open
881 try: 882 try:
882 filename = object() 883 filename = object()
883 mode = object() 884 mode = object()
884 open_result = result(filename, mode) 885 open_result = result(filename, mode)
885 finally: 886 finally:
886 builtins.open = original_open 887 builtins.open = original_open
887 888
888 self.assertEqual(fake_open.invocation_count, 1) 889 self.assertEqual(fake_open.invocation_count, 1)
889 890
890 args, kwargs = fake_open.last_invocation 891 args, kwargs = fake_open.last_invocation
891 self.assertIs(args[0], filename) 892 self.assertIs(args[0], filename)
892 self.assertIs(args[1], mode) 893 self.assertIs(args[1], mode)
893 self.assertIs(kwargs.pop('encoding'), encoding) 894 self.assertIs(kwargs.pop('encoding'), encoding)
895 self.assertIs(kwargs.pop('errors'), errors)
894 self.assertFalse(kwargs) 896 self.assertFalse(kwargs)
895 897
898 def test_errors(self):
899 with open(TESTFN, 'wb') as f:
900 # UTF-7 is a convenient, seldom used encoding
storchaka 2016/04/25 21:03:01 Unrelated comment.
901 f.write(b'\x80abc')
902 self.addCleanup(safe_unlink, TESTFN)
903
904 def check(errors, expected_lines):
905 with FileInput(files=TESTFN, mode='r',
906 openhook=hook_encoded('utf-8', errors=errors)) as fi:
907 lines = list(fi)
908 self.assertEqual(lines, expected_lines)
909
910 check('ignore', ['abc'])
911 with self.assertRaises(UnicodeDecodeError):
912 check('strict', ['abc'])
913 check('replace', ['\ufffdabc'])
914 check('backslashreplace', ['\\x80abc'])
915
storchaka 2016/04/25 21:03:01 Trailing whitespaces.
896 def test_modes(self): 916 def test_modes(self):
897 with open(TESTFN, 'wb') as f: 917 with open(TESTFN, 'wb') as f:
898 # UTF-7 is a convenient, seldom used encoding 918 # UTF-7 is a convenient, seldom used encoding
899 f.write(b'A\nB\r\nC\rD+IKw-') 919 f.write(b'A\nB\r\nC\rD+IKw-')
900 self.addCleanup(safe_unlink, TESTFN) 920 self.addCleanup(safe_unlink, TESTFN)
901 921
902 def check(mode, expected_lines): 922 def check(mode, expected_lines):
903 with FileInput(files=TESTFN, mode=mode, 923 with FileInput(files=TESTFN, mode=mode,
904 openhook=hook_encoded('utf-7')) as fi: 924 openhook=hook_encoded('utf-7')) as fi:
905 lines = list(fi) 925 lines = list(fi)
(...skipping 10 matching lines...) Expand all
916 936
917 class MiscTest(unittest.TestCase): 937 class MiscTest(unittest.TestCase):
918 938
919 def test_all(self): 939 def test_all(self):
920 blacklist = {'DEFAULT_BUFSIZE'} 940 blacklist = {'DEFAULT_BUFSIZE'}
921 support.check__all__(self, fileinput, blacklist=blacklist) 941 support.check__all__(self, fileinput, blacklist=blacklist)
922 942
923 943
924 if __name__ == "__main__": 944 if __name__ == "__main__":
925 unittest.main() 945 unittest.main()
OLDNEW
« Lib/fileinput.py ('K') | « Lib/fileinput.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+