Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thatbirdguythatuknownot opened this issue Nov 19, 2022 · 3 comments · Fixed by #112407
Closed

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

thatbirdguythatuknownot opened this issue Nov 19, 2022 · 3 comments · Fixed by #112407
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@thatbirdguythatuknownot
Copy link
Contributor

thatbirdguythatuknownot commented Nov 19, 2022

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

@thatbirdguythatuknownot thatbirdguythatuknownot added the type-bug An unexpected behavior, bug, or error label Nov 19, 2022
@pochmann
Copy link
Contributor

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

@thatbirdguythatuknownot
Copy link
Contributor Author

thatbirdguythatuknownot commented Nov 20, 2022

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.

@ericvsmith
Copy link
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

@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 25, 2023
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Nov 25, 2023
iritkatriel added a commit to iritkatriel/cpython that referenced this issue Nov 26, 2023
iritkatriel added a commit that referenced this issue Nov 26, 2023
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
4 participants