From 049bc57019559752b69ec748f2ed52a54d60e1cb Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 21 Nov 2013 01:09:37 -0800 Subject: [PATCH] fix #17 by removing non-utf8 characters --- packages/wakatime/wakatime/__init__.py | 3 +++ packages/wakatime/wakatime/log.py | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/wakatime/wakatime/__init__.py b/packages/wakatime/wakatime/__init__.py index 70e36f5..38b26cf 100644 --- a/packages/wakatime/wakatime/__init__.py +++ b/packages/wakatime/wakatime/__init__.py @@ -63,6 +63,9 @@ def parseConfigFile(configFile): 'verbose': False, } + if not os.path.isfile(configFile): + return configs + try: with open(configFile) as fh: for line in fh: diff --git a/packages/wakatime/wakatime/log.py b/packages/wakatime/wakatime/log.py index 561a365..860efe8 100644 --- a/packages/wakatime/wakatime/log.py +++ b/packages/wakatime/wakatime/log.py @@ -25,7 +25,12 @@ class CustomEncoder(json.JSONEncoder): if isinstance(obj, bytes): obj = bytes.decode(obj) return json.dumps(obj) - return super(CustomEncoder, self).default(obj) + try: + encoded = super(CustomEncoder, self).default(obj) + except UnicodeDecodeError: + obj = obj.decode('utf-8', 'ignore') + encoded = super(CustomEncoder, self).default(obj) + return encoded class JsonFormatter(logging.Formatter):