Skip to content

Bytecode for empty f-string != bytecode for empty normal string #99606

Closed

Description

Bug report

The bytecode for an empty f-string does not match the bytecode for an empty normal string.

import dis
dis.dis('""')
dis.dis('f""')

Output:

0           0 RESUME                   0

1           2 LOAD_CONST               0 ('')
            4 RETURN_VALUE
0           0 RESUME                   0

1           2 BUILD_STRING             0
            4 RETURN_VALUE

Environment

  • CPython versions tested on:
    3.11, 3.12.0a2

Linked PRs

Activity

  1. added
    type-bug
    on Nov 19, 2022
  2. pochmann commented on Nov 20, 2022

    @pochmann
    Contributor

    Why do you call that a bug? And why would you write f""?

  3. thatbirdguythatuknownot commented on Nov 20, 2022

    @thatbirdguythatuknownot
    ContributorAuthor

    Why do you call that a bug? And why would you write f""?

    Aren't they the same thing so shouldn't they have the same bytecode? It's pretty inconsistent with f"string contents" without the replacement fields and the other prefix combos with empty strings (they all use LOAD_CONST). Also sometimes I can forget to remove or subconsciously add f.

  4. ericvsmith commented on Nov 20, 2022

    @ericvsmith
    Member

    The intention was that an f-string without any substitutions would be equivalent to a normal string. These examples are 3.12+. It's true for non-empty strings, it should probably be the same for empty strings. Not that I think it's a big deal.

    >>> dis.dis("f'x'")
      0           0 RESUME                   0
    
      1           2 LOAD_CONST               0 ('x')
                  4 RETURN_VALUE
    >>> dis.dis("'x'")
      0           0 RESUME                   0
    
      1           2 LOAD_CONST               0 ('x')
                  4 RETURN_VALUE
    >>> dis.dis("f''")
      0           0 RESUME                   0
    
      1           2 BUILD_STRING             0
                  4 RETURN_VALUE
    >>> dis.dis("''")
      0           0 RESUME                   0
    
      1           2 LOAD_CONST               0 ('')
                  4 RETURN_VALUE
  5. added
    interpreter-core
    on Nov 25, 2023
  6. added 2 commits that reference this issue on Nov 25, 2023
  7. added a commit that references this issue on Nov 26, 2023
  8. added a commit that references this issue on Feb 11, 2024
  9. 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

Assignees

No one assigned

    Labels

    interpreter-coretype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions