fix UnicodeDecodeError when building user agent string

This commit is contained in:
Alan Hamlett 2014-12-04 11:39:03 -08:00
parent b402c2c6f2
commit 25c84259fb
3 changed files with 18 additions and 11 deletions

View file

@ -122,7 +122,7 @@ def parseConfigFile(configFile):
print(traceback.format_exc()) print(traceback.format_exc())
return None return None
except IOError: except IOError:
print(u('Error: Could not read from config file {0}').format(configFile)) print(u('Error: Could not read from config file {0}').format(u(configFile)))
return configs return configs
@ -227,8 +227,8 @@ def should_ignore(fileName, patterns):
return pattern return pattern
except re.error as ex: except re.error as ex:
log.warning(u('Regex error ({msg}) for ignore pattern: {pattern}').format( log.warning(u('Regex error ({msg}) for ignore pattern: {pattern}').format(
msg=str(ex), msg=u(ex),
pattern=pattern, pattern=u(pattern),
)) ))
except TypeError: except TypeError:
pass pass
@ -239,14 +239,14 @@ def get_user_agent(plugin):
ver = sys.version_info ver = sys.version_info
python_version = '%d.%d.%d.%s.%d' % (ver[0], ver[1], ver[2], ver[3], ver[4]) python_version = '%d.%d.%d.%s.%d' % (ver[0], ver[1], ver[2], ver[3], ver[4])
user_agent = u('wakatime/{ver} ({platform}) Python{py_ver}').format( user_agent = u('wakatime/{ver} ({platform}) Python{py_ver}').format(
ver=__version__, ver=u(__version__),
platform=platform.platform(), platform=u(platform.platform()),
py_ver=python_version, py_ver=python_version,
) )
if plugin: if plugin:
user_agent = u('{user_agent} {plugin}').format( user_agent = u('{user_agent} {plugin}').format(
user_agent=user_agent, user_agent=user_agent,
plugin=plugin, plugin=u(plugin),
) )
return user_agent return user_agent
@ -263,7 +263,7 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
if hidefilenames and targetFile is not None: if hidefilenames and targetFile is not None:
data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1] data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1]
if len(data['file'].strip('.').split('.', 1)) > 1: if len(data['file'].strip('.').split('.', 1)) > 1:
data['file'] = u('HIDDEN.{ext}').format(ext=data['file'].strip('.').rsplit('.', 1)[-1]) data['file'] = u('HIDDEN.{ext}').format(ext=u(data['file'].strip('.').rsplit('.', 1)[-1]))
else: else:
data['file'] = u('HIDDEN') data['file'] = u('HIDDEN')
if stats.get('lines'): if stats.get('lines'):
@ -381,7 +381,7 @@ def main(argv=None):
ignore = should_ignore(args.targetFile, args.ignore) ignore = should_ignore(args.targetFile, args.ignore)
if ignore is not False: if ignore is not False:
log.debug(u('File ignored because matches pattern: {pattern}').format( log.debug(u('File ignored because matches pattern: {pattern}').format(
pattern=ignore, pattern=u(ignore),
)) ))
return 0 return 0

View file

@ -21,9 +21,13 @@ is_py3 = (sys.version_info[0] == 3)
if is_py2: if is_py2:
def u(text): def u(text):
if isinstance(text, str): try:
return text.decode('utf-8') return text.decode('utf-8')
except:
try:
return unicode(text) return unicode(text)
except:
return text
open = codecs.open open = codecs.open
basestring = basestring basestring = basestring

View file

@ -15,6 +15,9 @@ import logging
import os import os
import traceback import traceback
from time import sleep from time import sleep
from .compat import u
try: try:
import sqlite3 import sqlite3
HAS_SQL = True HAS_SQL = True
@ -90,7 +93,7 @@ class Queue(object):
for row_name in ['file', 'time', 'project', 'language', 'lines', 'branch', 'is_write']: for row_name in ['file', 'time', 'project', 'language', 'lines', 'branch', 'is_write']:
if row[index] is not None: if row[index] is not None:
clauses.append('{0}=?'.format(row_name)) clauses.append('{0}=?'.format(row_name))
values.append(row[index]) values.append(u(row[index]))
else: else:
clauses.append('{0} IS NULL'.format(row_name)) clauses.append('{0} IS NULL'.format(row_name))
index += 1 index += 1