From 4c9bd683a623a2ae1623a02c3432ac8b55fb2eba Mon Sep 17 00:00:00 2001 From: Yajo Date: Tue, 5 Apr 2016 16:58:19 +0200 Subject: [PATCH] Do not die miserably when you get `False` as the HTML input. Test it. --- html_text/models/ir_fields_converter.py | 2 +- html_text/tests/test_extractor.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/html_text/models/ir_fields_converter.py b/html_text/models/ir_fields_converter.py index 503b2c7c7..1ad3572ea 100644 --- a/html_text/models/ir_fields_converter.py +++ b/html_text/models/ir_fields_converter.py @@ -41,7 +41,7 @@ class IrFieldsConverter(models.Model): # Parse HTML try: doc = html.fromstring(html_content) - except (etree.XMLSyntaxError, etree.ParserError): + except (TypeError, etree.XMLSyntaxError, etree.ParserError): if fail: raise else: diff --git a/html_text/tests/test_extractor.py b/html_text/tests/test_extractor.py index f7d0843d3..22443e32a 100644 --- a/html_text/tests/test_extractor.py +++ b/html_text/tests/test_extractor.py @@ -46,6 +46,12 @@ class ExtractorCase(TransactionCase): with self.assertRaises(etree.XMLSyntaxError): self.text_from_html("", fail=True) + def test_false_html(self): + """``False`` HTML handled correctly.""" + self.assertEqual(self.text_from_html(False), "") + with self.assertRaises(TypeError): + self.text_from_html(False, fail=True) + def test_bad_html(self): """Bad HTML handled correctly.""" self.assertEqual(self.text_from_html("<"), "")