RTF Input: Fix handling of RTF files with invalid encoded text. Fixes #1864719 [RTF conversion error: argument 1 must be unicode, not str](https://bugs.launchpad.net/calibre/+bug/1864719)

This commit is contained in:
Kovid Goyal 2020-02-26 07:04:59 +05:30
parent 17b09210ee
commit afcb54df1e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,6 +1,5 @@
from __future__ import unicode_literals, absolute_import, print_function, division from __future__ import unicode_literals, absolute_import, print_function, division
import os, sys import os, sys
from codecs import EncodedFile
from calibre.ebooks.rtf2xml import copy, check_encoding from calibre.ebooks.rtf2xml import copy, check_encoding
from calibre.ptempfile import better_mktemp from calibre.ptempfile import better_mktemp
@ -274,15 +273,10 @@ class ConvertToTags:
if self.__convert_utf or self.__bad_encoding: if self.__convert_utf or self.__bad_encoding:
copy_obj = copy.Copy(bug_handler=self.__bug_handler) copy_obj = copy.Copy(bug_handler=self.__bug_handler)
copy_obj.rename(self.__write_to, self.__file) copy_obj.rename(self.__write_to, self.__file)
file_encoding = "utf-8"
if self.__bad_encoding:
file_encoding = "us-ascii"
with open_for_read(self.__file) as read_obj: with open_for_read(self.__file) as read_obj:
with open_for_write(self.__write_to) as write_obj: with open_for_write(self.__write_to) as write_obj:
write_objenc = EncodedFile(write_obj, self.__encoding,
file_encoding, 'replace')
for line in read_obj: for line in read_obj:
write_objenc.write(line) write_obj.write(line)
copy_obj = copy.Copy(bug_handler=self.__bug_handler) copy_obj = copy.Copy(bug_handler=self.__bug_handler)
if self.__copy: if self.__copy:
copy_obj.copy_file(self.__write_to, "convert_to_tags.data") copy_obj.copy_file(self.__write_to, "convert_to_tags.data")