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

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

Issue 18829: csv produces confusing error message when passed a non-string delimiter
Patch Set: Created 6 years, 6 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
« no previous file with comments | « no previous file | Modules/_csv.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 711 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 747
748 mydialect.quoting = None 748 mydialect.quoting = None
749 self.assertRaises(csv.Error, mydialect) 749 self.assertRaises(csv.Error, mydialect)
750 750
751 mydialect.doublequote = True 751 mydialect.doublequote = True
752 mydialect.quoting = csv.QUOTE_ALL 752 mydialect.quoting = csv.QUOTE_ALL
753 mydialect.quotechar = '"' 753 mydialect.quotechar = '"'
754 d = mydialect() 754 d = mydialect()
755 755
756 mydialect.quotechar = "''" 756 mydialect.quotechar = "''"
757 self.assertRaises(csv.Error, mydialect) 757 self.assertRaises(ValueError, mydialect)
758 758
759 mydialect.quotechar = 4 759 mydialect.quotechar = 4
760 self.assertRaises(csv.Error, mydialect)
761
762 def test_delimiter(self):
763 class mydialect(csv.Dialect):
764 delimiter = ";"
765 escapechar = '\\'
766 doublequote = False
767 skipinitialspace = True
768 lineterminator = '\r\n'
769 quoting = csv.QUOTE_NONE
770 d = mydialect()
771
772 mydialect.delimiter = ":::"
773 self.assertRaises(csv.Error, mydialect)
774
775 mydialect.delimiter = 4
776 self.assertRaises(csv.Error, mydialect) 760 self.assertRaises(csv.Error, mydialect)
777 761
778 def test_lineterminator(self): 762 def test_lineterminator(self):
779 class mydialect(csv.Dialect): 763 class mydialect(csv.Dialect):
780 delimiter = ";" 764 delimiter = ";"
781 escapechar = '\\' 765 escapechar = '\\'
782 doublequote = False 766 doublequote = False
783 skipinitialspace = True 767 skipinitialspace = True
784 lineterminator = '\r\n' 768 lineterminator = '\r\n'
785 quoting = csv.QUOTE_NONE 769 quoting = csv.QUOTE_NONE
786 d = mydialect() 770 d = mydialect()
787 771
788 mydialect.lineterminator = ":::" 772 mydialect.lineterminator = ":::"
789 d = mydialect() 773 d = mydialect()
790 774
791 mydialect.lineterminator = 4 775 mydialect.lineterminator = 4
792 self.assertRaises(csv.Error, mydialect) 776 self.assertRaises(csv.Error, mydialect)
777
778 def test_invaliddelimiter(self):
779 class mydialect(csv.Dialect):
780 delimiter = b","
781
782 with self.assertRaises(csv.Error) as cm:
783 mydialect()
784 self.assertEqual(str(cm.exception),
785 '"delimiter" must be string, not bytes')
786
787 mydialect.delimiter = 4
788
789 with self.assertRaises(csv.Error) as cm:
790 mydialect()
791 self.assertEqual(str(cm.exception),
792 '"delimiter" must be string, not int')
793 793
794 794
795 class TestSniffer(unittest.TestCase): 795 class TestSniffer(unittest.TestCase):
796 sample1 = """\ 796 sample1 = """\
797 Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes 797 Harry's, Arlington Heights, IL, 2/1/03, Kimi Hayes
798 Shark City, Glendale Heights, IL, 12/28/02, Prezence 798 Shark City, Glendale Heights, IL, 12/28/02, Prezence
799 Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow 799 Tommy's Place, Blue Island, IL, 12/28/02, Blue Sunday/White Crow
800 Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back 800 Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back
801 """ 801 """
802 sample2 = """\ 802 sample2 = """\
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 1006
1007 1007
1008 def test_main(): 1008 def test_main():
1009 mod = sys.modules[__name__] 1009 mod = sys.modules[__name__]
1010 support.run_unittest( 1010 support.run_unittest(
1011 *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')] 1011 *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
1012 ) 1012 )
1013 1013
1014 if __name__ == '__main__': 1014 if __name__ == '__main__':
1015 test_main() 1015 test_main()
OLDNEW
« no previous file with comments | « no previous file | Modules/_csv.c » ('j') | no next file with comments »

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