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`.
This commit is contained in:
Nicola Soranzo
2019-01-15 13:10:32 +00:00
parent 1f3eec2879
commit b23a2a44d9
2 changed files with 9 additions and 8 deletions
+2 -2
View File
@@ -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
+7 -6
View File
@@ -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