You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
html.parser produces different output than documented #131535
3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesdocsDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or error
Hi! This is the expected behavior. You need to set convert_charrefs to False in order for handle_entityref / handle_charref to be called (the default is True):
HTMLParser.handle_entityref(name)
This method is called to process a named character reference of the form &name; (e.g. >), where name is a general entity reference (e.g. 'gt'). This method is never called if convert_charrefs is True.
HTMLParser.handle_charref(name)
This method is called to process decimal and hexadecimal numeric character references of the form &#NNN; and &#xNNN;. For example, the decimal equivalent for > is >, whereas the hexadecimal is >; in this case the method will receive '62' or 'x3E'. This method is never called if convert_charrefs is True.
Thank you, I understand now. However, the example code at the bottom of this documentation page does not explicitly set convert_charrefs=False, yet it still produces results as if convert_charrefs were True. This has confused me. Would it be better to refine this example for clarity?
Ah, in that case I agree the example should be clarified. It looks like it wasn't updated when the default for convert_charref was changed from False to True in Python 3.5.
It would be good to migrate the examples to .. doctest:: blocks at the same time to help catch this sort of discrepancy.
3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesdocsDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or error
Activity
brianschubert commentedon Mar 21, 2025
Hi! This is the expected behavior. You need to set
convert_charrefstoFalsein order forhandle_entityref/handle_charrefto be called (the default isTrue):From the docs:
mouchen626 commentedon Mar 21, 2025
Thank you, I understand now. However, the example code at the bottom of this documentation page does not explicitly set convert_charrefs=False, yet it still produces results as if convert_charrefs were True. This has confused me. Would it be better to refine this example for clarity?
brianschubert commentedon Mar 21, 2025
Ah, in that case I agree the example should be clarified. It looks like it wasn't updated when the default for
convert_charrefwas changed fromFalsetoTruein Python 3.5.It would be good to migrate the examples to
.. doctest::blocks at the same time to help catch this sort of discrepancy.Merge branch 'main' into pythongh-131535-docs-html-parser
Merge branch 'main' into pythongh-131535-docs-html-parser
gh-131535: Fix stale example in html.parser docs, make examples docte…
pythongh-131535: Fix stale example in html.parser docs, make examples…
[3.14] pythongh-131535: Fix stale example in html.parser docs, make e…
[3.13] gh-131535: Fix stale example in html.parser docs, make example…
[3.14] gh-131535: Fix stale example in html.parser docs, make example…
pythongh-131535: Fix stale example in html.parser docs, make examples…