python3: Default open encoding for files to UTF-8

Also tidy up unicode check in processLine, which is handled by `2to3`
pull/128/merge^2
Steven Hiscocks 2013-02-23 22:19:26 +00:00
parent 5d0d362e3f
commit df255063ae
1 changed files with 7 additions and 4 deletions

View File

@ -289,8 +289,7 @@ class Filter(JailThread):
def processLine(self, line): def processLine(self, line):
"""Split the time portion from log msg and return findFailures on them """Split the time portion from log msg and return findFailures on them
""" """
if (sys.version_info >= (3,) and isinstance(line, bytes)) or \ if not isinstance(line, unicode):
(sys.version_info < (3,) and isinstance(line, str)):
try: try:
# Decode line to UTF-8 # Decode line to UTF-8
line = line.decode('utf-8') line = line.decode('utf-8')
@ -523,7 +522,7 @@ class FileContainer:
self.__handler = None self.__handler = None
# Try to open the file. Raises an exception if an error occured. # Try to open the file. Raises an exception if an error occured.
if sys.version_info >= (3,): if sys.version_info >= (3,):
handler = open(filename, errors='ignore') handler = open(filename, encoding='utf-8', errors='ignore')
else: else:
handler = open(filename) handler = open(filename)
stats = os.fstat(handler.fileno()) stats = os.fstat(handler.fileno())
@ -548,7 +547,11 @@ class FileContainer:
return self.__filename return self.__filename
def open(self): def open(self):
self.__handler = open(self.__filename) if sys.version_info >= (3,):
self.__handler = open(
self.__filename, encoding='utf-8', errors='ignore')
else:
self.__handler = open(self.__filename)
# Set the file descriptor to be FD_CLOEXEC # Set the file descriptor to be FD_CLOEXEC
fd = self.__handler.fileno() fd = self.__handler.fileno()
fcntl.fcntl(fd, fcntl.F_SETFD, fd | fcntl.FD_CLOEXEC) fcntl.fcntl(fd, fcntl.F_SETFD, fd | fcntl.FD_CLOEXEC)