From c89d542479af2205dc90f59ab71cf52e89b09b1a Mon Sep 17 00:00:00 2001 From: josdion Date: Sun, 31 May 2020 13:46:16 +0300 Subject: [PATCH] fix encoding problems in subtitle class - don't use directly self._guessed_encoding, but instead call the function guessed_encoding() as the variable can be None. - use self.guessed_encoding() only if self.encoding is not set. --- libs/subliminal_patch/subtitle.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libs/subliminal_patch/subtitle.py b/libs/subliminal_patch/subtitle.py index 4b2c5510d..4ef52edbe 100644 --- a/libs/subliminal_patch/subtitle.py +++ b/libs/subliminal_patch/subtitle.py @@ -82,9 +82,7 @@ class Subtitle(Subtitle_): return if not isinstance(self.content, text_type): - if self.encoding: - return self.content.decode(self.encoding, errors='replace') - return self.content.decode(self.guess_encoding(), errors='replace') + return self.content.decode(self.get_encoding(), errors='replace') return self.content @@ -106,8 +104,11 @@ class Subtitle(Subtitle_): """ return self + def get_encoding(self): + return self.encoding if self.encoding else self.guess_encoding() + def set_encoding(self, encoding): - ge = self.guess_encoding() + ge = self.get_encoding() if encoding == ge: return @@ -115,6 +116,7 @@ class Subtitle(Subtitle_): logger.debug("Changing encoding: to %s, from %s", encoding, ge) self.content = unicontent.encode(encoding) self._guessed_encoding = encoding + self.encoding = encoding def normalize(self): """ @@ -284,7 +286,7 @@ class Subtitle(Subtitle_): subs = pysubs2.SSAFile.from_string(text, fps=sub_fps) unicontent = self.pysubs2_to_unicode(subs) - self.content = unicontent.encode(self._guessed_encoding) + self.content = unicontent.encode(self.get_encoding()) except: logger.exception("Couldn't convert subtitle %s to .srt format: %s", self, traceback.format_exc()) return False @@ -364,8 +366,8 @@ class Subtitle(Subtitle_): :return: string """ if not self.mods: - return fix_text(self.content.decode(encoding=self._guessed_encoding), **ftfy_defaults).encode( - encoding=self._guessed_encoding) + return fix_text(self.content.decode(encoding=self.get_encoding()), **ftfy_defaults).encode( + encoding=self.get_encoding()) submods = SubtitleModifications(debug=debug) if submods.load(content=self.text, language=self.language): @@ -374,7 +376,7 @@ class Subtitle(Subtitle_): self.mods = submods.mods_used content = fix_text(self.pysubs2_to_unicode(submods.f, format=format), **ftfy_defaults)\ - .encode(encoding=self._guessed_encoding) + .encode(encoding=self.get_encoding()) submods.f = None del submods return content