6618: Export CSV crashes gramps 4.0 beta

svn: r21999
This commit is contained in:
Benny Malengier 2013-04-15 11:01:24 +00:00
parent 77adb230c5
commit 16cfc2af23
2 changed files with 13 additions and 6 deletions

View File

@ -50,7 +50,7 @@ class CSVTab(TabbedDoc):
else: else:
self.filename = filename self.filename = filename
self.f = open(self.filename, "wb") self.f = open(self.filename, "w")
self.writer = csv.writer(self.f) self.writer = csv.writer(self.f)
def close(self): def close(self):

View File

@ -154,15 +154,22 @@ class UnicodeWriter(object):
self.encoder = codecs.getencoder(encoding) self.encoder = codecs.getencoder(encoding)
def writerow(self, row): def writerow(self, row):
if sys.version_info[0] < 3:
self.writer.writerow([s.encode('utf-8') for s in row]) self.writer.writerow([s.encode('utf-8') for s in row])
# Fetch UTF-8 output from the queue ... # Fetch UTF-8 output from the queue ...
data = self.queue.getvalue() data = self.queue.getvalue()
data = data.decode('utf-8') data = data.decode('utf-8')
else:
self.writer.writerow(row)
data = self.queue.getvalue()
#in python3, StringIO self.queue returns unicode!
#data now contains the csv data in unicode
# ... and reencode it into the target encoding # ... and reencode it into the target encoding
data, length = self.encoder(data) data, length = self.encoder(data)
# write to the target stream # write to the target stream
self.stream.write(data) self.stream.write(data)
# empty queue # empty queue, go to start position, then truncate
self.queue.seek(0)
self.queue.truncate(0) self.queue.truncate(0)
def writerows(self, rows): def writerows(self, rows):