Fix Book XML handler to deal with unusual characters in Book name

Fixes #10387
This commit is contained in:
prculley 2018-01-25 10:50:23 -06:00 committed by Nick Hall
parent 4b13c95467
commit b3ca17a90f

View File

@ -495,14 +495,14 @@ class BookList:
"""
Saves the current BookList to the associated file.
"""
with open(self.file, "w") as b_f:
with open(self.file, "w", encoding="utf-8") as b_f:
b_f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
b_f.write('<booklist>\n')
for name in sorted(self.bookmap): # enable a diff of archived copies
book = self.get_book(name)
dbname = book.get_dbname()
dbname = escape(book.get_dbname())
b_f.write(' <book name="%s" database="%s">'
'\n' % (name, dbname))
'\n' % (escape(name), dbname))
for item in book.get_item_list():
b_f.write(' <item name="%s" '
'trans_name="%s">\n' % (
@ -578,8 +578,15 @@ class BookList:
try:
parser = make_parser()
parser.setContentHandler(BookParser(self, self.dbase))
with open(self.file) as the_file:
parser.parse(the_file)
# bug 10387; XML should be utf8, but was not previously saved
# that way. So try to read utf8, if fails, try with system
# encoding. Only an issue on non-utf8 systems.
try:
with open(self.file, encoding="utf-8") as the_file:
parser.parse(the_file)
except UnicodeDecodeError:
with open(self.file) as the_file:
parser.parse(the_file)
except (IOError, OSError, ValueError, SAXParseException, KeyError,
AttributeError):
LOG.debug("Failed to parse book list", exc_info=True)