From b3ca17a90fda5de0976e6e3d36f07ffbcfd4d512 Mon Sep 17 00:00:00 2001 From: prculley Date: Thu, 25 Jan 2018 10:50:23 -0600 Subject: [PATCH] Fix Book XML handler to deal with unusual characters in Book name Fixes #10387 --- gramps/gen/plug/report/_book.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/gramps/gen/plug/report/_book.py b/gramps/gen/plug/report/_book.py index aa867e5ec..ad58a4a18 100644 --- a/gramps/gen/plug/report/_book.py +++ b/gramps/gen/plug/report/_book.py @@ -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("\n") b_f.write('\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(' ' - '\n' % (name, dbname)) + '\n' % (escape(name), dbname)) for item in book.get_item_list(): b_f.write(' \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)