From 49ff291f617a928d68aef80a69cbbbe2d52b3fb3 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Wed, 23 Dec 2015 15:22:19 +0100 Subject: [PATCH] Use utf-8 to encode files by default. Should fix this bug in Seen: Traceback (most recent call last): File "C:\Program Files\Python34\lib\site-packages\supybot\world.py", line 121, in flush f() File "C:\Program Files\Python34\lib\site-packages\supybot\plugins\__init__.py", line 271, in flush writer.writerow(L) File "C:\Program Files\Python34\lib\site-packages\supybot\utils\file.py", line 172, in write return self._fd.write(data) File "C:\Program Files\Python34\lib\encodings\cp1250.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u25c4' in position 33: character maps to --- plugins/__init__.py | 3 ++- src/utils/file.py | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/__init__.py b/plugins/__init__.py index bafd1bbaa..5796de465 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -32,6 +32,7 @@ import gc import os import csv import time +import codecs import fnmatch import os.path import threading @@ -221,7 +222,7 @@ class ChannelUserDB(ChannelUserDictionary): ChannelUserDictionary.__init__(self) self.filename = filename try: - fd = open(self.filename) + fd = codecs.open(self.filename, encoding='utf8') except EnvironmentError as e: log.warning('Couldn\'t open %s: %s.', self.filename, e) return diff --git a/src/utils/file.py b/src/utils/file.py index 768878f48..34d74536b 100644 --- a/src/utils/file.py +++ b/src/utils/file.py @@ -30,6 +30,7 @@ import os import time +import codecs import random import shutil import os.path @@ -126,7 +127,8 @@ class AtomicFile(object): makeBackupIfSmaller = True allowEmptyOverwrite = True def __init__(self, filename, mode='w', allowEmptyOverwrite=None, - makeBackupIfSmaller=None, tmpDir=None, backupDir=None): + makeBackupIfSmaller=None, tmpDir=None, backupDir=None, + encoding=None): if tmpDir is None: tmpDir = force(self.default.tmpDir) if backupDir is None: @@ -135,6 +137,8 @@ class AtomicFile(object): makeBackupIfSmaller = force(self.default.makeBackupIfSmaller) if allowEmptyOverwrite is None: allowEmptyOverwrite = force(self.default.allowEmptyOverwrite) + if encoding is None and 'b' not in mode: + encoding = 'utf8' if mode not in ('w', 'wb'): raise ValueError(format('Invalid mode: %q', mode)) self.rolledback = False @@ -153,7 +157,7 @@ class AtomicFile(object): self.tempFilename = os.path.join(tmpDir, tempFilename) # This doesn't work because of the uncollectable garbage effect. # self.__parent = super(AtomicFile, self) - self._fd = open(self.tempFilename, mode) + self._fd = codecs.open(self.tempFilename, mode, encoding=encoding) def __enter__(self): return self