Skip to content

Commit 270e9be

Browse files
committed
bunch of minor fixes to get test_cvs passing
1 parent e84b253 commit 270e9be

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

graalpython/lib-python/3/_csv.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,15 @@ def __new__(cls, dialect = None, **kwargs):
120120
def set_char(x):
121121
if x is None:
122122
return None
123-
if isinstance(x, str) and len(x) <= 1:
123+
if not isinstance(x, str):
124+
raise TypeError('"%s" must be string, not %s' % (name, type(x).__name__))
125+
if len(x) == 1:
124126
return x
125-
raise TypeError("%r must be a 1-character string" % (name,))
127+
raise TypeError('"%s" must be a 1-character string' % (name,))
126128
def set_str(x):
127129
if isinstance(x, str):
128130
return x
129-
raise TypeError("%r must be a string" % (name,))
131+
raise TypeError('"%s" must be a string' % (name))
130132
def set_quoting(x):
131133
if x in range(4):
132134
return x
@@ -257,8 +259,7 @@ def __next__(self):
257259
raise
258260

259261
self.line_num += 1
260-
261-
if '\0' in line:
262+
if isinstance(line, str) and '\0' in line or isinstance(line, bytes) and line.index(0) >=0:
262263
raise Error("line contains NULL byte")
263264
pos = 0
264265
while pos < len(line):
@@ -504,16 +505,20 @@ def _join_append(self, field, quoted, quote_empty):
504505

505506

506507
def writerow(self, row):
508+
if row is None:
509+
raise Error
510+
507511
dialect = self.dialect
508-
try:
509-
rowlen = len(row)
510-
except TypeError:
511-
raise Error("sequence expected")
512512

513513
# join all fields in internal buffer
514514
self._join_reset()
515515

516+
fields = []
516517
for field in row:
518+
fields.append(field)
519+
520+
rowlen = len(fields)
521+
for field in fields:
517522
quoted = False
518523
if dialect.quoting == QUOTE_NONNUMERIC:
519524
try:
@@ -526,6 +531,8 @@ def writerow(self, row):
526531
quoted = True
527532

528533
if field is None:
534+
if dialect.quoting == QUOTE_NONE:
535+
raise Error
529536
value = ""
530537
elif isinstance(field, float):
531538
value = repr(field)
@@ -589,7 +596,7 @@ def field_size_limit(limit=undefined):
589596
old_limit = _field_limit
590597

591598
if limit is not undefined:
592-
if not isinstance(limit, (int, long)):
599+
if not isinstance(limit, int):
593600
raise TypeError("int expected, got %s" %
594601
(limit.__class__.__name__,))
595602
_field_limit = limit

0 commit comments

Comments
 (0)