mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
Replace python-coded validation of csv dialect with a call to the C
dialect type (which has a better idea of what is and isn't valid).
This commit is contained in:
parent
8c94b42f31
commit
7130ff5eb9
2 changed files with 7 additions and 44 deletions
47
Lib/csv.py
47
Lib/csv.py
|
@ -8,6 +8,7 @@ from _csv import Error, __version__, writer, reader, register_dialect, \
|
||||||
unregister_dialect, get_dialect, list_dialects, \
|
unregister_dialect, get_dialect, list_dialects, \
|
||||||
QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE, \
|
QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE, \
|
||||||
__doc__
|
__doc__
|
||||||
|
from _csv import Dialect as _Dialect
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
@ -41,48 +42,14 @@ class Dialect:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
if self.__class__ != Dialect:
|
if self.__class__ != Dialect:
|
||||||
self._valid = True
|
self._valid = True
|
||||||
errors = self._validate()
|
self._validate()
|
||||||
if errors != []:
|
|
||||||
raise Error, "Dialect did not validate: %s" % ", ".join(errors)
|
|
||||||
|
|
||||||
def _validate(self):
|
def _validate(self):
|
||||||
errors = []
|
try:
|
||||||
if not self._valid:
|
_Dialect(self)
|
||||||
errors.append("can't directly instantiate Dialect class")
|
except TypeError, e:
|
||||||
|
# We do this for compatibility with py2.3
|
||||||
if self.delimiter is None:
|
raise Error(str(e))
|
||||||
errors.append("delimiter character not set")
|
|
||||||
elif (not isinstance(self.delimiter, str) or
|
|
||||||
len(self.delimiter) > 1):
|
|
||||||
errors.append("delimiter must be one-character string")
|
|
||||||
|
|
||||||
if self.quotechar is None:
|
|
||||||
if self.quoting != QUOTE_NONE:
|
|
||||||
errors.append("quotechar not set")
|
|
||||||
elif (not isinstance(self.quotechar, str) or
|
|
||||||
len(self.quotechar) > 1):
|
|
||||||
errors.append("quotechar must be one-character string")
|
|
||||||
|
|
||||||
if self.lineterminator is None:
|
|
||||||
errors.append("lineterminator not set")
|
|
||||||
elif not isinstance(self.lineterminator, str):
|
|
||||||
errors.append("lineterminator must be a string")
|
|
||||||
|
|
||||||
if self.doublequote not in (True, False) and self.quoting != QUOTE_NONE:
|
|
||||||
errors.append("doublequote parameter must be True or False")
|
|
||||||
|
|
||||||
if self.skipinitialspace not in (True, False):
|
|
||||||
errors.append("skipinitialspace parameter must be True or False")
|
|
||||||
|
|
||||||
if self.quoting is None:
|
|
||||||
errors.append("quoting parameter not set")
|
|
||||||
|
|
||||||
if self.quoting is QUOTE_NONE:
|
|
||||||
if (not isinstance(self.escapechar, (unicode, str)) or
|
|
||||||
len(self.escapechar) > 1):
|
|
||||||
errors.append("escapechar must be a one-character string or unicode object")
|
|
||||||
|
|
||||||
return errors
|
|
||||||
|
|
||||||
class excel(Dialect):
|
class excel(Dialect):
|
||||||
"""Describe the usual properties of Excel-generated CSV files."""
|
"""Describe the usual properties of Excel-generated CSV files."""
|
||||||
|
|
|
@ -727,10 +727,6 @@ class TestDialectValidity(unittest.TestCase):
|
||||||
mydialect.quoting = None
|
mydialect.quoting = None
|
||||||
self.assertRaises(csv.Error, mydialect)
|
self.assertRaises(csv.Error, mydialect)
|
||||||
|
|
||||||
mydialect.quoting = csv.QUOTE_NONE
|
|
||||||
mydialect.escapechar = None
|
|
||||||
self.assertRaises(csv.Error, mydialect)
|
|
||||||
|
|
||||||
mydialect.doublequote = True
|
mydialect.doublequote = True
|
||||||
mydialect.quoting = csv.QUOTE_ALL
|
mydialect.quoting = csv.QUOTE_ALL
|
||||||
mydialect.quotechar = '"'
|
mydialect.quotechar = '"'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue