Repository navigation
HTMLParser raises exception on some inputs #77057
Copy link
Copy link
Closed
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
| BPO | 32876 |
|---|---|
| Nosy | @ezio-melotti, @stevendaprano, @berkerpeksag, @hannob, @iritkatriel |
| PRs | |
| Superseder |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/ezio-melotti'
closed_at = None
created_at = <Date 2018-02-19.19:52:16.326>
labels = ['type-bug', 'library', '3.11']
title = 'HTMLParser raises exception on some inputs'
updated_at = <Date 2022-01-14.14:32:47.114>
user = 'https://github.com/hannob'bugs.python.org fields:
activity = <Date 2022-01-14.14:32:47.114>
actor = 'iritkatriel'
assignee = 'ezio.melotti'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2018-02-19.19:52:16.326>
creator = 'hanno'
dependencies = []
files = []
hgrepos = []
issue_num = 32876
keywords = ['patch']
message_count = 10.0
messages = ['312363', '312379', '312380', '312381', '323971', '325330', '401507', '410559', '410561', '410563']
nosy_count = 5.0
nosy_names = ['ezio.melotti', 'steven.daprano', 'berker.peksag', 'hanno', 'iritkatriel']
pr_nums = ['9295']
priority = 'normal'
resolution = None
stage = 'resolved'
status = 'open'
superseder = '31844'
type = 'behavior'
url = 'https://bugs.python.org/issue32876'
versions = ['Python 3.11']Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
hannob commentedon Feb 19, 2018
I noticed that the HTMLParser will raise an exception on some inputs.
I'm not sure what the expectations here are, but given that real-world HTML often contains all kinds of broken content I would assume an HTMLParser to always try to parse a document and not be interrupted by an exception if an error occurs.
Here's a minified example:
However I actually stepped upon HTML failing on a real webpage:
https://kafanews.com/
Exception of minified example:
stevendaprano commentedon Feb 19, 2018
The stdlib HTML parser requires correct HTML.
To parse broken HTML, as you find in the real world, you need a third-party library like BeautifulSoup. BeautifulSoup is much more complex (about 7-8 times as many LOC) but can handle nearly anything a browser can.
I doubt the stdlib will ever compete with BeautifulSoup.
hannob commentedon Feb 19, 2018
Actually BeautifulSoup also uses the python html parser in the backend, so it has the same problem. (It can use alternative backends, but the python parser is the default and they also describe it as "lenient", which I would interpret as "it can handle that".)
ezio-melotti commentedon Feb 19, 2018
The HTMLParser has been updated to handle HTML5 and should never fail parsing a document, so if it raises an error it's probably a bug.
berkerpeksag commentedon Aug 23, 2018
bpo-34480 is another relevant issue. The HTMLParse method doesn't have an error() method and it doesn't raise any exceptions, but its base class still does. I think there is a compatibility problem between html.parser.HTMLParser() and _markupbase.ParserBase() classes. See https://bugs.python.org/msg323966 for more details about this.
ezio-melotti commentedon Sep 14, 2018
There are at least a couple of issues here.
The first one is the way the parser handles '<![...'. The linked page contains markup like '<![STAT]-[USER-ACTIVE]!>' and since the parser currently checks for '<![' only, _markupbase.py:parse_marked_section gets called and an error gets incorrectly raised.
However "8.2.4.42. Markup declaration open state"0, states that after consuming '<!', there are only 4 valid paths forward:
The above example should therefore fall into 4), and be treated like a bogus comment.
PR-9295 changes parse_html_declaration() to align to the specs and implement path 3), resulting in the webpage being parsed without errors (the invalid markup is considered as a bogus comment).
The second issue is about an EOF in the middle of a bogus markup declaration, like in the minified example provided by OP ("<![\n"). In this case the comment should still be emitted ('[\n'), but currently nothing gets emitted. I'll look more into it either tomorrow or later this month and update the PR accordingly (or perhaps I'll open a separate issue).
iritkatriel commentedon Sep 9, 2021
I get a different error now:
iritkatriel commentedon Jan 14, 2022
The error() method was removed in bpo-31844.
hannob commentedon Jan 14, 2022
Now the example code raises an AssertionError(). Is that intended? I don't think that's any better.
I usually wouldn't expect an HTML parser to raise any error if you pass it a string, but instead to do fault tolerant parsing. And if it's expected that some inputs can generate exceptions, at least I think this should be properly documented.
9 remaining items
pythongh-77057: Fix handling of invalid markup declarations in HTMLPa…
pythongh-77057: Fix handling of invalid markup declarations in HTMLPa…
serhiy-storchaka commentedon May 10, 2025
@ezio-melotti's PR has been merged.
Truncated comment, doctype declaration or CDATA section now produce a
data.[3.13] gh-77057: Fix handling of invalid markup declarations in HTMLP…
[3.14] gh-77057: Fix handling of invalid markup declarations in HTMLP…
ParserRejectedMarkup is no longer raised
pythongh-77057: Fix handling of invalid markup declarations in HTMLPa…
pythongh-77057: Fix handling of invalid markup declarations in HTMLPa…
strip_markup: never propagate parser exceptions