From 69bc90f5b44f02dc34795150e5fc69ef5bab6ae0 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 29 Mar 2021 11:14:46 +0100 Subject: [PATCH] Make export_ship() paranoid about possible encoding issues. We had a report of a UnicodeDecodeError trying to read an old file. So try utf-8 first, if it fails try the default, and if nothing else try to write a new file so the next call should actually work as expected. --- monitor.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/monitor.py b/monitor.py index 18a751b7..e0c75ff3 100644 --- a/monitor.py +++ b/monitor.py @@ -1085,7 +1085,7 @@ class EDLogs(FileSystemEventHandler): # type: ignore # See below return d - def export_ship(self, filename=None) -> None: + def export_ship(self, filename=None) -> None: # noqa: C901, CCR001 """ Export ship loadout as a Loadout event. @@ -1124,9 +1124,23 @@ class EDLogs(FileSystemEventHandler): # type: ignore # See below regexp = re.compile(re.escape(ship) + r'\.\d{4}\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt') oldfiles = sorted((x for x in listdir(config.get_str('outdir')) if regexp.match(x))) # type: ignore if oldfiles: - with open(join(config.get_str('outdir'), oldfiles[-1]), 'rU') as h: # type: ignore - if h.read() == string: - return # same as last time - don't write + try: + with open(join(config.get('outdir'), oldfiles[-1]), 'r', encoding='utf-8') as h: # type: ignore + if h.read() == string: + return # same as last time - don't write + + except UnicodeError: + logger.exception("UnicodeError reading old ship loadout with utf-8 encoding, trying without...") + try: + with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h: # type: ignore + if h.read() == string: + return # same as last time - don't write + + except OSError: + logger.exception("OSError reading old ship loadout default encoding.") + + except OSError: + logger.exception("OSError reading old ship loadout with default encoding") # Write ts = strftime('%Y-%m-%dT%H.%M.%S', localtime(time()))