Skip to content

empty string constant in f-string format_spec #110309

Description

@15r10nk

Bug report

Bug description:

The f-string f'{1:{name}}' has a extra Constant(value='') in the format_spec part in 3.12, which is not part of the AST in 3.11. This has also an effect on the generated bytecode.

import ast
import dis

print(ast.dump(ast.parse("f'{1:{name}}'"),indent=2))

dis.dis(compile("f'{1:{name}}'","<string>","exec"))

output (Python 3.12.0):

Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[
          FormattedValue(
            value=Constant(value=1),
            conversion=-1,
            format_spec=JoinedStr(
              values=[
                FormattedValue(
                  value=Name(id='name', ctx=Load()),
                  conversion=-1),
                Constant(value='')]))]))],            # <-- extra Constant
  type_ignores=[])
  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1)
              4 LOAD_NAME                0 (name)
              6 FORMAT_VALUE             0
              8 LOAD_CONST               1 ('')        # <-- extra LOAD_CONST
             10 BUILD_STRING             2             # <-- extra BUILD_STRING
             12 FORMAT_VALUE             4 (with format)
             14 POP_TOP
             16 RETURN_CONST             2 (None)

output (Python 3.11.3):

Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[
          FormattedValue(
            value=Constant(value=1),
            conversion=-1,
            format_spec=JoinedStr(
              values=[
                FormattedValue(
                  value=Name(id='name', ctx=Load()),
                  conversion=-1)]))]))],
  type_ignores=[])
  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1)
              4 LOAD_NAME                0 (name)
              6 FORMAT_VALUE             0
              8 FORMAT_VALUE             4 (with format)
             10 POP_TOP
             12 LOAD_CONST               1 (None)
             14 RETURN_VALUE

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 3, 2023
  2. sunmy2019 commented on Oct 3, 2023

    @sunmy2019
    Member

    Confirmed also on main: 8d92b6e

  3. pablogsal commented on Oct 3, 2023

    @pablogsal
    Member

    @lysnikolaou @isidentical can you take a look? If you are not available I can take a look at this next week

  4. sunmy2019 commented on Oct 3, 2023

    @sunmy2019
    Member

    I proposed a fix: apply the same empty constant prune algorithm here.

  5. added a commit that references this issue on Oct 5, 2023
  6. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.12only security fixes3.13only security fixestopic-parsertype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions