From fdbffe60fe170af1cd9e18fbc65954aae54418bd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 31 Dec 2021 20:48:14 +0530 Subject: [PATCH] MOBI output: Dont fail if input document contains invalid % based lengths. Fixes #1956097 [news download crashes with TypeError: '<' not supported between instances of 'float' and 'str'](https://bugs.launchpad.net/calibre/+bug/1956097) --- src/calibre/ebooks/mobi/mobiml.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/mobi/mobiml.py b/src/calibre/ebooks/mobi/mobiml.py index 284b2c220e..7d44cc4d8e 100644 --- a/src/calibre/ebooks/mobi/mobiml.py +++ b/src/calibre/ebooks/mobi/mobiml.py @@ -6,14 +6,17 @@ __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' import copy -import re import numbers +import re +from contextlib import suppress from lxml import etree -from calibre.ebooks.oeb.base import namespace, barename -from calibre.ebooks.oeb.base import XHTML, XHTML_NS, urlnormalize + +from calibre.ebooks.mobi.utils import convert_color_for_font_tag +from calibre.ebooks.oeb.base import ( + XHTML, XHTML_NS, barename, namespace, urlnormalize +) from calibre.ebooks.oeb.stylizer import Stylizer from calibre.ebooks.oeb.transforms.flatcss import KeyMapper -from calibre.ebooks.mobi.utils import convert_color_for_font_tag from calibre.utils.imghdr import identify from polyglot.builtins import string_or_bytes @@ -55,7 +58,8 @@ def convert_margin(style, which): ans = asfloat(style[which]) raw = style._get(which) if isinstance(raw, str) and '%' in raw: - ans = min(style._unit_convert(raw, base=600), ans) + with suppress(TypeError): + ans = min(style._unit_convert(raw, base=600), ans) return ans