From 8cbb3ef014d6dd0d91c2eafba298bdc41ead750b Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sat, 8 Mar 2014 17:49:11 -0800 Subject: [PATCH] 7519: GRAMPs unable to handle ... Path with accented characters Much worse, actually. Gramps wasn't able to handle any non-ascii characters in any preference setting from Python2. repr() in Py2 effectively runs "encode(val, ascii, backslashreplace)" on its argument, and there's no way to reconstruct the string. --- gramps/gen/utils/configmanager.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/gramps/gen/utils/configmanager.py b/gramps/gen/utils/configmanager.py index b81874e16..4296a242f 100644 --- a/gramps/gen/utils/configmanager.py +++ b/gramps/gen/utils/configmanager.py @@ -50,7 +50,11 @@ _ = glocale.translation.gettext def safe_eval(exp): # restrict eval to empty environment - return eval(exp, {}) + try: + return eval(exp, {}) + except SyntaxError: + logging.warning ("Invalid command string: %s", exp) + return exp ##try: ## from ast import literal_eval as safe_eval @@ -368,9 +372,17 @@ class ConfigManager(object): default = "" if isinstance(value, int): value = int(value) - key_file.write(("%s%s=%s\n")% (default, - key, - repr(value))) + # repr() in Py2 effectively runs "encode(val, + # ascii, backslashreplace)" on its argument, + # and there's no way to reconstruct the + # string, so we special-case handling writing + # to ensure the unicode is preserved. + if isinstance(value, str) or isinstance(value, unicode): + key_file.write(("%s%s=u'%s'\n") % (default, key, + value)) + else: + key_file.write(("%s%s=%s\n")% (default, key, + repr(value))) key_file.write("\n") key_file.close() # else, no filename given; nothing to save so do nothing quietly