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

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
(...skipping 725 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 736
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 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 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 with self.assertRaises(csv.Error) as cm: 761 with self.assertRaises(csv.Error) as cm:
758 mydialect() 762 mydialect()
759 self.assertEqual(str(cm.exception), 763 self.assertEqual(str(cm.exception),
760 '"quotechar" must be an 1-character string') 764 '"quotechar" must be an 1-character string')
761 765
762 mydialect.quotechar = 4 766 mydialect.quotechar = 4
763 with self.assertRaises(csv.Error) as cm: 767 with self.assertRaises(csv.Error) as cm:
764 mydialect() 768 mydialect()
765 self.assertEqual(str(cm.exception), 769 self.assertEqual(str(cm.exception),
766 '"quotechar" must be string, not int') 770 '"quotechar" must be string, not int')
767 771
768 def test_delimiter(self): 772 def test_delimiter(self):
769 class mydialect(csv.Dialect): 773 class mydialect(csv.Dialect):
770 delimiter = ";" 774 delimiter = ";"
771 escapechar = '\\' 775 escapechar = '\\'
772 doublequote = False 776 doublequote = False
773 skipinitialspace = True 777 skipinitialspace = True
774 lineterminator = '\r\n' 778 lineterminator = '\r\n'
775 quoting = csv.QUOTE_NONE 779 quoting = csv.QUOTE_NONE
776 mydialect() 780 d = mydialect()
781 self.assertEqual(d.delimiter, ";")
777 782
778 mydialect.delimiter = ":::" 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 = ""
779 with self.assertRaises(csv.Error) as cm: 790 with self.assertRaises(csv.Error) as cm:
780 mydialect() 791 mydialect()
781 self.assertEqual(str(cm.exception), 792 self.assertEqual(str(cm.exception),
782 '"delimiter" must be an 1-character string') 793 '"delimiter" must be an 1-character string')
783 794
784 mydialect.delimiter = b"," 795 mydialect.delimiter = b","
785 with self.assertRaises(csv.Error) as cm: 796 with self.assertRaises(csv.Error) as cm:
786 mydialect() 797 mydialect()
787 self.assertEqual(str(cm.exception), 798 self.assertEqual(str(cm.exception),
788 '"delimiter" must be string, not bytes') 799 '"delimiter" must be string, not bytes')
789 800
790 mydialect.delimiter = 4 801 mydialect.delimiter = 4
791 with self.assertRaises(csv.Error) as cm: 802 with self.assertRaises(csv.Error) as cm:
792 mydialect() 803 mydialect()
793 self.assertEqual(str(cm.exception), 804 self.assertEqual(str(cm.exception),
794 '"delimiter" must be string, not int') 805 '"delimiter" must be string, not int')
795 806
796 def test_lineterminator(self): 807 def test_lineterminator(self):
797 class mydialect(csv.Dialect): 808 class mydialect(csv.Dialect):
798 delimiter = ";" 809 delimiter = ";"
799 escapechar = '\\' 810 escapechar = '\\'
800 doublequote = False 811 doublequote = False
801 skipinitialspace = True 812 skipinitialspace = True
802 lineterminator = '\r\n' 813 lineterminator = '\r\n'
803 quoting = csv.QUOTE_NONE 814 quoting = csv.QUOTE_NONE
804 mydialect() 815 d = mydialect()
816 self.assertEqual(d.lineterminator, '\r\n')
805 817
806 mydialect.lineterminator = ":::" 818 mydialect.lineterminator = ":::"
807 mydialect() 819 d = mydialect()
820 self.assertEqual(d.lineterminator, ":::")
808 821
809 mydialect.lineterminator = 4 822 mydialect.lineterminator = 4
810 with self.assertRaises(csv.Error) as cm: 823 with self.assertRaises(csv.Error) as cm:
811 mydialect() 824 mydialect()
812 self.assertEqual(str(cm.exception), 825 self.assertEqual(str(cm.exception),
813 '"lineterminator" must be a string') 826 '"lineterminator" must be a string')
814 827
815 828
816 class TestSniffer(unittest.TestCase): 829 class TestSniffer(unittest.TestCase):
817 sample1 = """\ 830 sample1 = """\
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
1027 1040
1028 1041
1029 def test_main(): 1042 def test_main():
1030 mod = sys.modules[__name__] 1043 mod = sys.modules[__name__]
1031 support.run_unittest( 1044 support.run_unittest(
1032 *[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')]
1033 ) 1046 )
1034 1047
1035 if __name__ == '__main__': 1048 if __name__ == '__main__':
1036 test_main() 1049 test_main()
LEFTRIGHT

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