From 08d8d4c7759312aa19424e851b2b02f965d84a21 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Thu, 17 Jan 2013 19:47:44 +0000 Subject: [PATCH] [Bug 6364] Fix file-opening failures due to unicode content. svn: r21147 --- gramps/gen/plug/_pluginreg.py | 14 +++++++++++++- gramps/gen/utils/configmanager.py | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/gramps/gen/plug/_pluginreg.py b/gramps/gen/plug/_pluginreg.py index c149e97c5..05b1f6591 100644 --- a/gramps/gen/plug/_pluginreg.py +++ b/gramps/gen/plug/_pluginreg.py @@ -1093,9 +1093,21 @@ class PluginRegister(object): if sys.version_info[0] < 3: full_filename = full_filename.encode(glocale.getfilesystemencoding()) local_gettext = glocale.get_addon_translator(full_filename).gettext + try: + stream = open(full_filename).read() + except UnicodeError as err: + if sys.version_info[0] < 3: + print(_("ERROR: Failed to read file %s, %s") % (full_filename, str(err))) + continue + else: + try: + stream = open(full_filename, encoding = 'utf-8').read() + except ValueError as err: + print(_("ERROR: Failed to read file %s, %s") % (full_filename, str(err))) + continue try: #execfile(full_filename, - exec(compile(open(full_filename).read(), full_filename, 'exec'), + exec(compile(stream, full_filename, 'exec'), make_environment(_=local_gettext), {}) except ValueError as msg: diff --git a/gramps/gen/utils/configmanager.py b/gramps/gen/utils/configmanager.py index 6fa579b51..c766852bf 100644 --- a/gramps/gen/utils/configmanager.py +++ b/gramps/gen/utils/configmanager.py @@ -335,7 +335,10 @@ class ConfigManager(object): except OSError as exp: if exp.errno != errno.EEXIST: raise - key_file = open(filename, "w") + if sys.version_info[0] < 3: + key_file = open(filename, "w") + else: + key_file = open(filename, "w", encoding="utf-8") key_file.write(";; Gramps key file\n") key_file.write((";; Automatically created at %s" % time.strftime("%Y/%m/%d %H:%M:%S")) + "\n\n")