Decode messages from IOError before logging them

IOError messages are bytestrings, often in the language of the system, so they
may include non-ASCII characters. Thus, we must decode them using the locale's
preferred encoding to get Unicode objects we safely can pass on for logging
the IOError.
This commit is contained in:
Stein Magnus Jodal 2012-08-10 23:17:41 +02:00
parent 0e66ffe6a5
commit d6f17b4cf0
3 changed files with 12 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import os
logger = logging.getLogger('mopidy.backends.local.translator')
from mopidy.models import Track, Artist, Album
from mopidy.utils import locale_decode
from mopidy.utils.path import path_to_uri
def parse_m3u(file_path):
@ -33,8 +34,8 @@ def parse_m3u(file_path):
try:
with open(file_path) as m3u:
contents = m3u.readlines()
except IOError, e:
logger.error('Couldn\'t open m3u: %s', e)
except IOError as error:
logger.error('Couldn\'t open m3u: %s', locale_decode(error))
return uris
for line in contents:
@ -61,8 +62,8 @@ def parse_mpd_tag_cache(tag_cache, music_dir=''):
try:
with open(tag_cache) as library:
contents = library.read()
except IOError, e:
logger.error('Could not open tag cache: %s', e)
except IOError as error:
logger.error('Could not open tag cache: %s', locale_decode(error))
return tracks
current = {}

View File

@ -5,7 +5,7 @@ from pykka import registry, actor
from mopidy import listeners, settings
from mopidy.frontends.mpd import dispatcher, protocol
from mopidy.utils import network, process, log
from mopidy.utils import locale_decode, log, network, process
logger = logging.getLogger('mopidy.frontends.mpd')
@ -32,8 +32,8 @@ class MpdFrontend(actor.ThreadingActor, listeners.BackendListener):
try:
network.Server(hostname, port, protocol=MpdSession,
max_connections=settings.MPD_SERVER_MAX_CONNECTIONS)
except IOError, e:
logger.error(u'MPD server startup failed: %s', e)
except IOError as error:
logger.error(u'MPD server startup failed: %s', locale_decode(error))
sys.exit(1)
logger.info(u'MPD server running at [%s]:%s', hostname, port)

View File

@ -9,6 +9,8 @@ from pykka import ActorDeadError
from pykka.actor import ThreadingActor
from pykka.registry import ActorRegistry
from mopidy.utils import locale_decode
logger = logging.getLogger('mopidy.utils.server')
class ShouldRetrySocketCall(Exception):
@ -21,9 +23,9 @@ def try_ipv6_socket():
try:
socket.socket(socket.AF_INET6).close()
return True
except IOError, e:
except IOError as error:
logger.debug(u'Platform supports IPv6, but socket '
'creation failed, disabling: %s', e)
'creation failed, disabling: %s', locale_decode(error))
return False
#: Boolean value that indicates if creating an IPv6 socket will succeed.