Skip to content

Commit 735d36a

Browse files
committed
Treat all AssertionErrors as HTMLParseErrors
1 parent a048cc1 commit 735d36a

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

inbox/util/html.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from html import escape as html_escape
1010

1111

12-
class HTMLParseError(Exception):
12+
class HTMLParseError(ValueError):
1313
pass
1414

1515

@@ -42,11 +42,6 @@ def handle_data(self, d):
4242
if not self.strip_tag_contents_mode:
4343
self.fed.append(d)
4444

45-
if (3,) <= sys.version_info < (3, 10):
46-
47-
def error(self, message):
48-
raise HTMLParseError(message)
49-
5045
def handle_entityref(self, d):
5146
try:
5247
val = chr(name2codepoint[d])
@@ -69,12 +64,17 @@ def strip_tags(html: str) -> str:
6964
"""
7065
s = HTMLTagStripper()
7166

67+
# `HTMLParser.feed()` raises `AssertionError` if fed invalid HTML.
68+
# See the source at https://github.com/python/cpython/blob/main/Lib/html/parser.py
69+
# and https://github.com/python/cpython/blob/main/Lib/_markupbase.py#L30.
70+
# We want to catch this exception and raise a more informative exception to
71+
# tell it apart from general `AssertionError`s that normally mean a programmer error.
72+
# Those are later caught in `Message.calculate_html_snippet` so it can recover from
73+
# invalid HTML.
7274
try:
7375
s.feed(html)
7476
except AssertionError as e:
75-
if e.args[0].startswith("unknown status keyword"):
76-
raise HTMLParseError(*e.args) from e
77-
raise
77+
raise HTMLParseError(*e.args) from e
7878

7979
return s.get_data()
8080

0 commit comments

Comments
 (0)