Socket: Fix compatibility with Python 3.1.

This commit is contained in:
Valentin Lorentz 2013-02-22 20:43:27 +00:00
parent f9171f5c5e
commit 7f2d38b885

View File

@ -1,4 +1,4 @@
### ##
# Copyright (c) 2002-2004, Jeremiah Fincher # Copyright (c) 2002-2004, Jeremiah Fincher
# Copyright (c) 2010, James McCoy # Copyright (c) 2010, James McCoy
# All rights reserved. # All rights reserved.
@ -194,7 +194,7 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
#first, try to decode using utf-8 #first, try to decode using utf-8
try: try:
line = line.decode(encoding='utf-8', errors='strict') line = line.decode('utf8', 'strict')
except UnicodeError: except UnicodeError:
# if this fails and chardet is loaded, try to guess the correct encoding # if this fails and chardet is loaded, try to guess the correct encoding
if chardetLoaded: if chardetLoaded:
@ -204,18 +204,19 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
if u.result['encoding']: if u.result['encoding']:
# try to use the guessed encoding # try to use the guessed encoding
try: try:
line = line.decode(u.result['encoding'], errors='strict') line = line.decode(u.result['encoding'],
'strict')
# on error, give up and replace the offending characters # on error, give up and replace the offending characters
except UnicodeError: except UnicodeError:
line = line.decode(errors='replace') line = line.decode(errors='replace')
else: else:
# if no encoding could be guessed, fall back to utf-8 and # if no encoding could be guessed, fall back to utf-8 and
# replace offending characters # replace offending characters
line = line.decode(encoding='utf-8', errors='replace') line = line.decode('utf8', 'replace')
# if chardet is not loaded, try to decode using utf-8 and replace any # if chardet is not loaded, try to decode using utf-8 and replace any
# offending characters # offending characters
else: else:
line = line.decode(encoding='utf-8', errors='replace') line = line.decode('utf8', 'replace')
msg = drivers.parseMsg(line) msg = drivers.parseMsg(line)
if msg is not None: if msg is not None: