summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_csv.py
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-06-29 22:42:24 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-06-29 22:42:24 (GMT)
commit634e076bbe08de3b5b4dfc2a1e10a20b64114ac1 (patch)
tree4f1ff4125ddc40af6ed8a17baa033d57937693f3 /Lib/test/test_csv.py
parent06beaba785b78cc89986b447ef2bb7be4f7a7696 (diff)
parent925a3225708c40c8cd58a7475c5a6d3bd89db01d (diff)
downloadcpython-634e076bbe08de3b5b4dfc2a1e10a20b64114ac1.zip
cpython-634e076bbe08de3b5b4dfc2a1e10a20b64114ac1.tar.gz
cpython-634e076bbe08de3b5b4dfc2a1e10a20b64114ac1.tar.bz2
Merge #18155: Regex-escape delimiter, in case it is a regex special char.
Diffstat (limited to 'Lib/test/test_csv.py')
-rw-r--r--Lib/test/test_csv.py42
1 files changed, 39 insertions, 3 deletions
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 974d73d..559e51f 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -805,7 +805,7 @@ Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back
'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow'
'Stonecutters ''Seafood'' and Chop House':'Lemont':'IL':'12/19/02':'Week Back'
"""
- header = '''\
+ header1 = '''\
"venue","city","state","date","performers"
'''
sample3 = '''\
@@ -824,10 +824,35 @@ Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back
sample6 = "a|b|c\r\nd|e|f\r\n"
sample7 = "'a'|'b'|'c'\r\n'd'|e|f\r\n"
+# Issue 18155: Use a delimiter that is a special char to regex:
+
+ header2 = '''\
+"venue"+"city"+"state"+"date"+"performers"
+'''
+ sample8 = """\
+Harry's+ Arlington Heights+ IL+ 2/1/03+ Kimi Hayes
+Shark City+ Glendale Heights+ IL+ 12/28/02+ Prezence
+Tommy's Place+ Blue Island+ IL+ 12/28/02+ Blue Sunday/White Crow
+Stonecutters Seafood and Chop House+ Lemont+ IL+ 12/19/02+ Week Back
+"""
+ sample9 = """\
+'Harry''s'+ Arlington Heights'+ 'IL'+ '2/1/03'+ 'Kimi Hayes'
+'Shark City'+ Glendale Heights'+' IL'+ '12/28/02'+ 'Prezence'
+'Tommy''s Place'+ Blue Island'+ 'IL'+ '12/28/02'+ 'Blue Sunday/White Crow'
+'Stonecutters ''Seafood'' and Chop House'+ 'Lemont'+ 'IL'+ '12/19/02'+ 'Week Back'
+"""
+
def test_has_header(self):
sniffer = csv.Sniffer()
self.assertEqual(sniffer.has_header(self.sample1), False)
- self.assertEqual(sniffer.has_header(self.header+self.sample1), True)
+ self.assertEqual(sniffer.has_header(self.header1 + self.sample1),
+ True)
+
+ def test_has_header_regex_special_delimiter(self):
+ sniffer = csv.Sniffer()
+ self.assertEqual(sniffer.has_header(self.sample8), False)
+ self.assertEqual(sniffer.has_header(self.header2 + self.sample8),
+ True)
def test_sniff(self):
sniffer = csv.Sniffer()
@@ -861,13 +886,24 @@ Stonecutters Seafood and Chop House, Lemont, IL, 12/19/02, Week Back
dialect = sniffer.sniff(self.sample7)
self.assertEqual(dialect.delimiter, "|")
self.assertEqual(dialect.quotechar, "'")
+ dialect = sniffer.sniff(self.sample8)
+ self.assertEqual(dialect.delimiter, '+')
+ dialect = sniffer.sniff(self.sample9)
+ self.assertEqual(dialect.delimiter, '+')
+ self.assertEqual(dialect.quotechar, "'")
def test_doublequote(self):
sniffer = csv.Sniffer()
- dialect = sniffer.sniff(self.header)
+ dialect = sniffer.sniff(self.header1)
+ self.assertFalse(dialect.doublequote)
+ dialect = sniffer.sniff(self.header2)
self.assertFalse(dialect.doublequote)
dialect = sniffer.sniff(self.sample2)
self.assertTrue(dialect.doublequote)
+ dialect = sniffer.sniff(self.sample8)
+ self.assertFalse(dialect.doublequote)
+ dialect = sniffer.sniff(self.sample9)
+ self.assertTrue(dialect.doublequote)
if not hasattr(sys, "gettotalrefcount"):
if support.verbose: print("*** skipping leakage tests ***")