From b23a2a44d99c9d68def546bda3bbf0d7059c8f95 Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Tue, 15 Jan 2019 11:54:50 +0000 Subject: [PATCH] Fix ``UnicodeDecodeError`` exception under Python3 Fix the following exception reported in the startup logs: ``` galaxy.webhooks ERROR 2019-01-15 11:31:18,095 'ascii' codec can't decode byte 0xef in position 8291: ordinal not in range(128) Traceback (most recent call last): File "/galaxy/lib/galaxy/webhooks/__init__.py", line 62, in load_webhooks self.load_webhook_from_config(directory, config_file_path) File "/galaxy/lib/galaxy/webhooks/__init__.py", line 96, in load_webhook_from_config webhook.script = file.read() File "/galaxy_venv3/lib/python3.5/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 8291: ordinal not in range(128) ``` Also rename variables from `file` (Python2 standard library function and class) to `fh`. --- lib/galaxy/web/framework/base.py | 4 ++-- lib/galaxy/webhooks/__init__.py | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/galaxy/web/framework/base.py b/lib/galaxy/web/framework/base.py index b2ef292deb1..7f08994f20c 100644 --- a/lib/galaxy/web/framework/base.py +++ b/lib/galaxy/web/framework/base.py @@ -499,12 +499,12 @@ def send_file(start_response, trans, body): return body -def iterate_file(file): +def iterate_file(fh): """ Progressively return chunks from `file`. """ while 1: - chunk = file.read(CHUNK_SIZE) + chunk = fh.read(CHUNK_SIZE) if not chunk: break yield chunk diff --git a/lib/galaxy/webhooks/__init__.py b/lib/galaxy/webhooks/__init__.py index e32ee2d9ba6..e5d32fdc7d4 100644 --- a/lib/galaxy/webhooks/__init__.py +++ b/lib/galaxy/webhooks/__init__.py @@ -1,6 +1,7 @@ """ This module manages loading of Galaxy webhooks. """ +import io import logging import os @@ -64,8 +65,8 @@ class WebhooksRegistry(object): log.exception(e) def load_webhook_from_config(self, webhook_dir, config_file_path): - with open(config_file_path) as file: - config = yaml.safe_load(file) + with open(config_file_path) as fh: + config = yaml.safe_load(fh) weight = config.get('weight', 1) if weight < 1: @@ -83,8 +84,8 @@ class WebhooksRegistry(object): # single file try: styles_file = os.path.join(webhook_dir, 'styles.css') - with open(styles_file, 'r') as file: - webhook.styles = file.read().replace('\n', '') + with open(styles_file, 'r') as fh: + webhook.styles = fh.read().replace('\n', '') except IOError: pass @@ -92,8 +93,8 @@ class WebhooksRegistry(object): # single file try: script_file = os.path.join(webhook_dir, 'script.js') - with open(script_file, 'r') as file: - webhook.script = file.read() + with io.open(script_file, 'r', encoding='utf-8') as fh: + webhook.script = fh.read() except IOError: pass