Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTMLParser error handling. #87

Merged
merged 3 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions genshi/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,13 @@ def _generate():
for tag in open_tags:
yield END, QName(tag), pos
break
except html.HTMLParseError as e:
msg = '%s: line %d, column %d' % (e.msg, e.lineno, e.offset)
raise ParseError(msg, self.filename, e.lineno, e.offset)
except Exception as e:
# Python's simple HTMLParser does not raise detailed
# errors except in strict mode which was deprecated
# in Python 3.3 and removed in Python 3.5 and which in
# any case is not used is this code.
msg = str(e)
raise ParseError(msg, self.filename)
return Stream(_generate()).filter(_coalesce)

def __iter__(self):
Expand Down
16 changes: 15 additions & 1 deletion genshi/tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from genshi.core import Attrs, QName, Stream
from genshi.input import XMLParser, HTMLParser, ParseError, ET
from genshi.compat import StringIO, BytesIO
from genshi.compat import IS_PYTHON2, StringIO, BytesIO
from genshi.tests.utils import doctest_suite
from xml.etree import ElementTree

Expand Down Expand Up @@ -294,6 +294,20 @@ def test_convert_ElementTree_to_markup_stream(self):
self.assertEqual((Stream.END, QName("span")), events[4][:2])
self.assertEqual((Stream.END, QName("div")), events[5][:2])

def test_parsing_error(self):
text = u'<div></div>'.encode('utf-8')
events = HTMLParser(BytesIO(text))
if IS_PYTHON2:
self.assertRaises(ParseError, list, events)
else:
self.assertRaisesRegex(
ParseError,
r"source returned bytes, but no encoding specified",
list,
events,
)


def suite():
suite = unittest.TestSuite()
suite.addTest(doctest_suite(XMLParser.__module__))
Expand Down
Loading