From 28db54436b2c16e2e4af4ccbdd282807bd9949d0 Mon Sep 17 00:00:00 2001 From: un-pogaz <46523284+un-pogaz@users.noreply.github.com> Date: Thu, 13 Oct 2022 16:21:10 +0200 Subject: [PATCH] ensure that the Python template returns a string --- src/calibre/utils/formatter.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index 51eda846bc..e10b0e7f96 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -1602,11 +1602,16 @@ class TemplateFormatter(string.Formatter): db=get_database(self.book, get_database(self.book, None)), globals=self.global_vars, arguments=arguments) - return compiled_template(self.book, self.python_context_object) + rslt = compiled_template(self.book, self.python_context_object) except Exception as e: ss = traceback.extract_tb(exc_info()[2])[-1] raise ValueError(_('Error in function {0} on line {1} : {2} - {3}').format( ss.name, ss.lineno, type(e).__name__, str(e))) + + if not isinstance(rslt, str): + raise ValueError(_('The Python template does not return a string')) + + return rslt def compile_python_template(self, template): def replace_func(mo):