1# _workarounds.py
2
3import io
4import csv
5import warnings
6
7from ._common import PY2
8
9ESCAPECHAR = '\\'
10
11
12def issue12178(escapechar=ESCAPECHAR):
13    with (io.BytesIO() if PY2 else io.StringIO(newline='')) as stream:
14        csv.writer(stream, escapechar=escapechar).writerow([escapechar])
15        line = stream.getvalue()
16    return (escapechar * 2) not in line
17
18
19def issue31590(line='spam%s\neggs,spam\r\n' % ESCAPECHAR, escapechar=ESCAPECHAR):
20    with (io.BytesIO(line) if PY2 else io.StringIO(line, newline='')) as stream:
21        reader = csv.reader(stream, quoting=csv.QUOTE_NONE, escapechar=escapechar)
22        row = next(reader)
23    return len(row) != 2
24
25
26def has_issue12178(dialect, affected=issue12178()):
27    return affected and dialect.escapechar and dialect.quoting != csv.QUOTE_NONE
28
29
30def has_issue31590(dialect, affected=issue31590()):
31    return affected and dialect.escapechar and dialect.quoting == csv.QUOTE_NONE
32
33
34def warn_if_issue31590(reader):
35    if has_issue31590(reader.dialect):
36        warnings.warn('%r cannot parse embedded newlines correctly, '
37                      'see https://bugs.python.org/issue31590'
38                      'for details' % reader)
39