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.

(cherry picked from commit 8cbb3ef014)
This commit is contained in:
John Ralls 2014-03-08 17:49:11 -08:00
parent 3a56ed8433
commit 3535f7a79f

View File

@ -50,7 +50,11 @@ _ = glocale.translation.gettext
def safe_eval(exp): def safe_eval(exp):
# restrict eval to empty environment # restrict eval to empty environment
return eval(exp, {}) try:
return eval(exp, {})
except SyntaxError:
logging.warning ("Invalid command string: %s", exp)
return exp
##try: ##try:
## from ast import literal_eval as safe_eval ## from ast import literal_eval as safe_eval
@ -364,9 +368,17 @@ class ConfigManager(object):
default = "" default = ""
if isinstance(value, int): if isinstance(value, int):
value = int(value) value = int(value)
key_file.write(("%s%s=%s\n")% (default, # repr() in Py2 effectively runs "encode(val,
key, # ascii, backslashreplace)" on its argument,
repr(value))) # 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.write("\n")
key_file.close() key_file.close()
# else, no filename given; nothing to save so do nothing quietly # else, no filename given; nothing to save so do nothing quietly