Permalink
Browse files

Fix tuple conversion bug with non-ascii strings.

  • Loading branch information...
1 parent 182634d commit e2be0e6d369cbe8ec934e7c3e2c8a53abe8e49af @emgee emgee committed Jul 21, 2009
Showing with 26 additions and 8 deletions.
  1. +18 −8 convertish/convert.py
  2. +8 −0 convertish/tests/test_basic.py
View
@@ -238,21 +238,31 @@ def __init__(self, *a, **k):
def convert_csvrow_to_list(row, delimiter=','):
- dialect = getDialect(delimiter=delimiter)
sf = StringIO()
- csvReader = csv.reader(sf, dialect=dialect)
- sf.write(row)
+ sf.write(row.encode('utf-8'))
sf.seek(0,0)
- return csvReader.next()
+ reader = csv.reader(sf, dialect=getDialect(delimiter=delimiter))
+ return list(_decode_row(reader.next()))
def convert_list_to_csvrow(l, delimiter=','):
- dialect = getDialect(delimiter=delimiter)
sf = StringIO()
- writer = csv.writer(sf, dialect=dialect)
- writer.writerow(l)
+ writer = csv.writer(sf, dialect=getDialect(delimiter=delimiter))
+ writer.writerow(list(_encode_row(l)))
sf.seek(0,0)
- return sf.read().strip()
+ return sf.read().strip().decode('utf-8')
+
+
+def _encode_row(row, encoding='utf-8'):
+ for cell in row:
+ if cell is not None:
+ cell = cell.encode(encoding)
+ yield cell
+
+
+def _decode_row(row, encoding='utf-8'):
+ for cell in row:
+ yield cell.decode(encoding)
class SequenceToStringConverter(Converter):
@@ -1,3 +1,5 @@
+# -*- coding: utf-8
+
from cStringIO import StringIO
import unittest
import schemaish
@@ -147,6 +149,12 @@ def test_sequencetupleintegerstring_string_conversion(self):
actual = string_converter(type).to_type(value)
self.assertEquals(actual,expected)
+ def test_tuple_unicode(self):
+ schema = schemaish.Tuple([schemaish.String(), schemaish.String()])
+ converter = string_converter(schema)
+ self.assertEquals(converter.from_type(('£1'.decode('utf-8'), u'foo')), '£1,foo'.decode('utf-8'))
+ self.assertEquals(converter.to_type('£1,foo'.decode('utf-8')), ('£1'.decode('utf-8'), u'foo'))
+
def test_tuple_noneifying(self):
schema = schemaish.Tuple([schemaish.Integer(), schemaish.String()])
converter = string_converter(schema)

0 comments on commit e2be0e6

Please sign in to comment.