Skip to content

NULL in lexical analysis of f-string #116881

Closed
@chepner

Description

@chepner

Documentation

This is somewhat related to #116580. There are two references to NULL in the description of f-strings that don't have a clear meaning.

format_spec       ::=  (literal_char | NULL | replacement_field)*
literal_char      ::=  <any code point except "{", "}" or NULL>

In both cases (but especially literal_char), it could refer to U+0000, but I'm unaware that a null character is allowed anywhere in Python source. At least, my attempt to inject one failed:

>>> print(ast.dump(ast.parse("f'{x:"'\x00'"}'"), indent=2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ast.py", line 52, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: source code string cannot contain null bytes

For format_spec, it could refer to an empty specification (f'{x:}'), but (literal_char | replacement_field)* would cover that just as well.

Linked PRs

Metadata

Metadata

Assignees

Labels

docsDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-parser

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions