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

Delta Between Two Patch Sets: Lib/test/test_csv.py

Issue 18829: csv produces confusing error message when passed a non-string delimiter
Left Patch Set: Created 6 years ago
Right Patch Set: Created 6 years 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/csv.py ('k') | Modules/_csv.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Copyright (C) 2001,2002 Python Software Foundation 1 # Copyright (C) 2001,2002 Python Software Foundation
2 # csv package unit tests 2 # csv package unit tests
3 3
4 import io 4 import io
5 import sys 5 import sys
6 import os 6 import os
7 import unittest 7 import unittest
8 from io import StringIO 8 from io import StringIO
9 from tempfile import TemporaryFile 9 from tempfile import TemporaryFile
10 import csv 10 import csv
11 import gc 11 import gc
12 from test import support 12 from test import support
13 13
14 class Test_Csv(unittest.TestCase): 14 class Test_Csv(unittest.TestCase):
15 """ 15 """
16 Test the underlying C csv parser in ways that are not appropriate 16 Test the underlying C csv parser in ways that are not appropriate
17 from the high level interface. Further tests of this nature are done 17 from the high level interface. Further tests of this nature are done
18 in TestDialectRegistry. 18 in TestDialectRegistry.
19 """ 19 """
20 def _test_arg_valid(self, ctor, arg): 20 def _test_arg_valid(self, ctor, arg):
21 self.assertRaises(TypeError, ctor) 21 self.assertRaises(TypeError, ctor)
22 self.assertRaises(TypeError, ctor, None) 22 self.assertRaises(TypeError, ctor, None)
23 self.assertRaises(TypeError, ctor, arg, bad_attr = 0) 23 self.assertRaises(TypeError, ctor, arg, bad_attr = 0)
24 self.assertRaises(TypeError, ctor, arg, delimiter = 0) 24 self.assertRaises(TypeError, ctor, arg, delimiter = 0)
25 self.assertRaises(TypeError, ctor, arg, delimiter = 'XX') 25 self.assertRaises(ValueError, ctor, arg, delimiter = 'XX')
26 self.assertRaises(csv.Error, ctor, arg, 'foo') 26 self.assertRaises(csv.Error, ctor, arg, 'foo')
27 self.assertRaises(TypeError, ctor, arg, delimiter=None) 27 self.assertRaises(TypeError, ctor, arg, delimiter=None)
28 self.assertRaises(TypeError, ctor, arg, delimiter=1) 28 self.assertRaises(TypeError, ctor, arg, delimiter=1)
29 self.assertRaises(TypeError, ctor, arg, quotechar=1) 29 self.assertRaises(TypeError, ctor, arg, quotechar=1)
30 self.assertRaises(TypeError, ctor, arg, lineterminator=None) 30 self.assertRaises(TypeError, ctor, arg, lineterminator=None)
31 self.assertRaises(TypeError, ctor, arg, lineterminator=1) 31 self.assertRaises(TypeError, ctor, arg, lineterminator=1)
32 self.assertRaises(TypeError, ctor, arg, quoting=None) 32 self.assertRaises(TypeError, ctor, arg, quoting=None)
33 self.assertRaises(TypeError, ctor, arg, 33 self.assertRaises(TypeError, ctor, arg,
34 quoting=csv.QUOTE_ALL, quotechar='') 34 quoting=csv.QUOTE_ALL, quotechar='')
35 self.assertRaises(TypeError, ctor, arg, 35 self.assertRaises(TypeError, ctor, arg,
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 class TestDialectValidity(unittest.TestCase): 737 class TestDialectValidity(unittest.TestCase):
738 def test_quoting(self): 738 def test_quoting(self):
739 class mydialect(csv.Dialect): 739 class mydialect(csv.Dialect):
740 delimiter = ";" 740 delimiter = ";"
741 escapechar = '\\' 741 escapechar = '\\'
742 doublequote = False 742 doublequote = False
743 skipinitialspace = True 743 skipinitialspace = True
744 lineterminator = '\r\n' 744 lineterminator = '\r\n'
745 quoting = csv.QUOTE_NONE 745 quoting = csv.QUOTE_NONE
746 d = mydialect() 746 d = mydialect()
747 self.assertEqual(d.quoting, csv.QUOTE_NONE)
747 748
748 mydialect.quoting = None 749 mydialect.quoting = None
749 self.assertRaises(csv.Error, mydialect) 750 self.assertRaises(csv.Error, mydialect)
750 751
751 mydialect.doublequote = True 752 mydialect.doublequote = True
752 mydialect.quoting = csv.QUOTE_ALL 753 mydialect.quoting = csv.QUOTE_ALL
753 mydialect.quotechar = '"' 754 mydialect.quotechar = '"'
754 d = mydialect() 755 d = mydialect()
756 self.assertEqual(d.quoting, csv.QUOTE_ALL)
757 self.assertEqual(d.quotechar, '"')
758 self.assertTrue(d.doublequote)
755 759
756 mydialect.quotechar = "''" 760 mydialect.quotechar = "''"
757 self.assertRaises(csv.Error, mydialect) 761 with self.assertRaises(csv.Error) as cm:
762 mydialect()
763 self.assertEqual(str(cm.exception),
764 '"quotechar" must be an 1-character string')
758 765
759 mydialect.quotechar = 4 766 mydialect.quotechar = 4
760 self.assertRaises(csv.Error, mydialect) 767 with self.assertRaises(csv.Error) as cm:
768 mydialect()
769 self.assertEqual(str(cm.exception),
770 '"quotechar" must be string, not int')
771
772 def test_delimiter(self):
773 class mydialect(csv.Dialect):
774 delimiter = ";"
775 escapechar = '\\'
776 doublequote = False
777 skipinitialspace = True
778 lineterminator = '\r\n'
779 quoting = csv.QUOTE_NONE
780 d = mydialect()
781 self.assertEqual(d.delimiter, ";")
782
783 mydialect.delimiter = ":::"
784 with self.assertRaises(csv.Error) as cm:
785 mydialect()
786 self.assertEqual(str(cm.exception),
787 '"delimiter" must be an 1-character string')
788
789 mydialect.delimiter = ""
790 with self.assertRaises(csv.Error) as cm:
791 mydialect()
792 self.assertEqual(str(cm.exception),
793 '"delimiter" must be an 1-character string')
794
795 mydialect.delimiter = b","
796 with self.assertRaises(csv.Error) as cm:
797 mydialect()
798 self.assertEqual(str(cm.exception),
799 '"delimiter" must be string, not bytes')
800
801 mydialect.delimiter = 4
802 with self.assertRaises(csv.Error) as cm:
803 mydialect()
804 self.assertEqual(str(cm.exception),
805 '"delimiter" must be string, not int')
761 806
762 def test_lineterminator(self): 807 def test_lineterminator(self):
763 class mydialect(csv.Dialect): 808 class mydialect(csv.Dialect):
764 delimiter = ";" 809 delimiter = ";"
765 escapechar = '\\' 810 escapechar = '\\'
766 doublequote = False 811 doublequote = False
767 skipinitialspace = True 812 skipinitialspace = True
768 lineterminator = '\r\n' 813 lineterminator = '\r\n'
769 quoting = csv.QUOTE_NONE 814 quoting = csv.QUOTE_NONE
770 d = mydialect() 815 d = mydialect()
816 self.assertEqual(d.lineterminator, '\r\n')
771 817
772 mydialect.lineterminator = ":::" 818 mydialect.lineterminator = ":::"
773 d = mydialect() 819 d = mydialect()
820 self.assertEqual(d.lineterminator, ":::")
774 821
775 mydialect.lineterminator = 4 822 mydialect.lineterminator = 4
776 self.assertRaises(csv.Error, mydialect) 823 with self.assertRaises(csv.Error) as cm:
777 824 mydialect()
778 def test_invaliddelimiter(self): 825 self.assertEqual(str(cm.exception),
779 for dlmtr in (b",", ":::", 4): 826 '"lineterminator" must be a string')
780 class mydialect(csv.Dialect):
781 delimiter = dlmtr
782
783 with self.assertRaises(csv.Error) as cm:
784 mydialect()
785 self.assertEqual(str(cm.exception),
786 '"delimiter" must be an 1-character string')
787 827
788 828
789 class TestSniffer(unittest.TestCase): 829 class TestSniffer(unittest.TestCase):
790 sample1 = """\ 830 sample1 = """\
791 Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes 831 Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes
792 Shark City, Glendale Heights, IL, 12/28/02, Prezence 832 Shark City, Glendale Heights, IL, 12/28/02, Prezence
793 Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow 833 Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow
794 Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back 834 Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back
795 """ 835 """
796 sample2 = """\ 836 sample2 = """\
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
1000 1040
1001 1041
1002 def test_main(): 1042 def test_main():
1003 mod = sys.modules[__name__] 1043 mod = sys.modules[__name__]
1004 support.run_unittest( 1044 support.run_unittest(
1005 *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')] 1045 *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
1006 ) 1046 )
1007 1047
1008 if __name__ == '__main__': 1048 if __name__ == '__main__':
1009 test_main() 1049 test_main()
LEFTRIGHT

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