RustPython/RustPython · error · AssertionError

expected name token at %r

Error message

expected name token at %r

What it means

_scan_name applies the name-token regex _declname_match (a letter followed by name characters and trailing whitespace); when it cannot match at a position where the grammar requires a name, AssertionError('expected name token at %r') is raised with a 20-character excerpt starting at the declaration plus the updated line/offset. It is called from parse_declaration, _parse_decl, parse_marked_section, and the internal-subset parsers.

Source

Thrown at Lib/_markupbase.py:390

                    return j

    # Internal -- scan a name token and the new position and the token, or
    # return -1 if we've reached the end of the buffer.
    def _scan_name(self, i, declstartpos):
        rawdata = self.rawdata
        n = len(rawdata)
        if i == n:
            return None, -1
        m = _declname_match(rawdata, i)
        if m:
            s = m.group()
            name = s.strip()
            if (i + len(s)) == n:
                return None, -1  # end of buffer
            return name.lower(), m.end()
        else:
            self.updatepos(declstartpos, i)
            raise AssertionError(
                "expected name token at %r" % rawdata[declstartpos:declstartpos+20]
            )

    # To be overridden -- handlers for unknown objects
    def unknown_decl(self, data):
        pass

View on GitHub (pinned to aaeab4f754)

Solutions

  1. Catch AssertionError around feed()/close()
  2. Fix the generator so declarations always carry a real name token where the grammar requires one
  3. Call parser.close() after the last chunk so buffer-end states resolve as incomplete rather than misparsed

Example fix

# before
parser.feed('<![ [ x ]]>')
# AssertionError: expected name token at '<![ [ x ]]>...'

# after
import re
text = re.sub(r'<!\[\s*\[', '<![ignore[', text)
parser.feed(text)
Defensive patterns

Strategy: try-catch

Validate before calling

import re

def declarations_have_names(text: str) -> bool:
    # '<!' or '<![' followed by a non-name character where a name is required
    return not re.search(r'<!\s*[^\w>\-\[]|<!\[\s*\[', text)

Try / catch

try:
    parser.feed(text)
    parser.close()
except AssertionError as e:
    log.warning('declaration missing name token: %s', e)

Prevention

When it happens

Trigger: `HTMLParser().feed('<![ [ x ]]>')` - a marked section missing its status keyword; `<!DOCTYPE doc [<! x>` - an empty declaration name inside the subset; any declaration whose name position holds whitespace or punctuation.

Common situations: Templated placeholders used as names (`<!${decl}`); truncation mid-declaration; whitespace where a keyword must be.

Related errors


AI-assisted analysis of RustPython/RustPython@aaeab4f754 (2026-08-17). Data as JSON: /api/errors/c6e6e11129631342. Report an issue: GitHub.