@@ -120,13 +120,15 @@ def __new__(cls, dialect = None, **kwargs):
120
120
def set_char (x ):
121
121
if x is None :
122
122
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 :
124
126
return x
125
- raise TypeError ("%r must be a 1-character string" % (name ,))
127
+ raise TypeError ('"%s" must be a 1-character string' % (name ,))
126
128
def set_str (x ):
127
129
if isinstance (x , str ):
128
130
return x
129
- raise TypeError ("%r must be a string" % (name , ))
131
+ raise TypeError ('"%s" must be a string' % (name ))
130
132
def set_quoting (x ):
131
133
if x in range (4 ):
132
134
return x
@@ -257,8 +259,7 @@ def __next__(self):
257
259
raise
258
260
259
261
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 :
262
263
raise Error ("line contains NULL byte" )
263
264
pos = 0
264
265
while pos < len (line ):
@@ -504,16 +505,20 @@ def _join_append(self, field, quoted, quote_empty):
504
505
505
506
506
507
def writerow (self , row ):
508
+ if row is None :
509
+ raise Error
510
+
507
511
dialect = self .dialect
508
- try :
509
- rowlen = len (row )
510
- except TypeError :
511
- raise Error ("sequence expected" )
512
512
513
513
# join all fields in internal buffer
514
514
self ._join_reset ()
515
515
516
+ fields = []
516
517
for field in row :
518
+ fields .append (field )
519
+
520
+ rowlen = len (fields )
521
+ for field in fields :
517
522
quoted = False
518
523
if dialect .quoting == QUOTE_NONNUMERIC :
519
524
try :
@@ -526,6 +531,8 @@ def writerow(self, row):
526
531
quoted = True
527
532
528
533
if field is None :
534
+ if dialect .quoting == QUOTE_NONE :
535
+ raise Error
529
536
value = ""
530
537
elif isinstance (field , float ):
531
538
value = repr (field )
@@ -589,7 +596,7 @@ def field_size_limit(limit=undefined):
589
596
old_limit = _field_limit
590
597
591
598
if limit is not undefined :
592
- if not isinstance (limit , ( int , long ) ):
599
+ if not isinstance (limit , int ):
593
600
raise TypeError ("int expected, got %s" %
594
601
(limit .__class__ .__name__ ,))
595
602
_field_limit = limit
0 commit comments