python/cpython · error · AssertionError

unknown status keyword %r in marked section

Error message

unknown status keyword %r in marked section

What it means

AssertionError raised by _markupbase.ParserBase.parse_marked_section when a <![keyword[ ... ]]> construct uses a status keyword the parser does not recognize. Only temp, cdata, ignore, include, rcdata (standard) and if, else, endif (MS Office downlevel-revealed conditionals) are accepted; anything else aborts.

Source

Thrown at Lib/_markupbase.py:154

                return j
        return -1 # incomplete

    # Internal -- parse a marked section
    # Override this to handle MS-word extension syntax <![if word]>content<![endif]>
    def parse_marked_section(self, i, report=1):
        rawdata= self.rawdata
        assert rawdata[i:i+3] == '<![', "unexpected call to parse_marked_section()"
        sectName, j = self._scan_name( i+3, i )
        if j < 0:
            return j
        if sectName in {"temp", "cdata", "ignore", "include", "rcdata"}:
            # look for standard ]]> ending
            match= _markedsectionclose.search(rawdata, i+3)
        elif sectName in {"if", "else", "endif"}:
            # look for MS Office ]> ending
            match= _msmarkedsectionclose.search(rawdata, i+3)
        else:
            raise AssertionError(
                'unknown status keyword %r in marked section' % rawdata[i+3:j]
            )
        if not match:
            return -1
        if report:
            j = match.start(0)
            self.unknown_decl(rawdata[i+3: j])
        return match.end(0)

    # Internal -- parse comment, return length or -1 if not terminated
    def parse_comment(self, i, report=1):
        rawdata = self.rawdata
        if rawdata[i:i+4] != '<!--':
            raise AssertionError('unexpected call to parse_comment()')
        match = _commentclose.search(rawdata, i+4)
        if not match:
            return -1
        if report:

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Preprocess: strip <![...[...]]> blocks with a regex (r'<!\[.*?\]\]>|<!\[.*?\]>', DOTALL) before feeding.
  2. Correct the keyword if you control the source: use CDATA/IGNORE/INCLUDE spellings.
  3. Catch AssertionError and resume feeding after the section terminator (]]> or ]> for MS conditionals).
  4. For documents that legitimately use marked sections, use an SGML-capable parser (lxml/opensp) rather than html.parser.

Example fix

# before
HTMLParser().feed('<![expect[ secret ]]>')  # AssertionError: unknown status keyword

# after
import re
feed = re.sub(r'<!\[.*?\]\]?>', '', raw, flags=re.S)
HTMLParser().feed(feed)
Defensive patterns

Strategy: try-catch

Validate before calling

import re
_KNOWN_SECT = {'temp', 'cdata', 'ignore', 'include', 'rcdata', 'if', 'else', 'endif'}

def marked_sections_ok(raw: str) -> bool:
    for m in re.finditer(r'<!\[\s*(\w+)\s*\[', raw):
        if m.group(1).lower() not in _KNOWN_SECT:
            return False
    return True

Try / catch

try:
    parser.feed(data)
except AssertionError as e:
    if 'marked section' in str(e):
        data = re.sub(r'<!\[.*?\]\]?>', '', data, flags=re.S)
        parser.reset(); parser.feed(data)
    else:
        raise

Prevention

When it happens

Trigger: HTMLParser.feed() on markup like <![expect[ ... ]]> or <![switch[ ...]]> — sectName comes back unrecognized. Common with XML 'INCLUDE/IGNORE'-misspellings, CDATA variants, or proprietary marked sections in publishing/DTD files.

Common situations: Scraping legacy SGML documents and old help files; parsing XML with marked sections using a non-standard keyword; test corpora containing unusual <![...[ constructs.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/c340d016a5d47c8a. Report an issue: GitHub.