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

Backslash in f-string format specifier gets escape #105194

Closed
domdfcoding opened this issue Jun 1, 2023 · 4 comments
Closed

Backslash in f-string format specifier gets escape #105194

domdfcoding opened this issue Jun 1, 2023 · 4 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@domdfcoding
Copy link
Contributor

domdfcoding commented Jun 1, 2023

Bug report

Between Python 3.12 alpha 7 and beta 1 the behaviour of format specifiers containing a backslash (e.g. \n, \u2603) in f-strings changed.

Previously, the __format__ method would get e.g. the newline character, but it now receives a backslash and an n character (i.e. the string "\n")

The following script demonstrates this:

# issue.py

class F:
	def __format__(self, format_spec):
		print("spec:", repr(format_spec))
		return format_spec.join(["a", "b", "c"])

print("out: ", repr((f"{F():\n}")))
print("out: ", repr((f"{F():\u2603}")))

print("out: ", repr((format(F(), "\n"))))
print("out: ", repr((format(F(), "\u2603"))))
$ python3.11 issue.py
spec: '\n'
out:  'a\nb\nc'
spec: '☃'
out:  'a☃b☃c'
spec: '\n'
out:  'a\nb\nc'
spec: '☃'
out:  'a☃b☃c'
$ python3.12 issue.py
spec: '\\n'
out:  'a\\nb\\nc'
spec: '\\u2603'
out:  'a\\u2603b\\u2603c'
spec: '\n'
out:  'a\nb\nc'
spec: '☃'
out:  'a☃b☃c'

In both cases the format() function works correctly - I only encounter the issue with f-strings.

I suspect this is related to the PEP 701 changes to f-strings, but I couldn't find anything in the discussion of that PEP to suggest this was a deliberate change.

Your environment

  • CPython versions tested on: Python 3.11.2, 3.12.0alpha7, 3.12.0beta1
  • Operating system and architecture: Ubuntu 20.04 (AMD64)

Linked PRs

@domdfcoding domdfcoding added the type-bug An unexpected behavior, bug, or error label Jun 1, 2023
@pablogsal
Copy link
Member

@isidentical @lysnikolaou can you take this one?

@lysnikolaou
Copy link
Member

Sure! Will probably have a look tomorrow morning.

@pablogsal
Copy link
Member

@lysnikolaou I gave this a go and I think I have it. Can you check #105231 ?

pablogsal added a commit to pablogsal/cpython that referenced this issue Jun 2, 2023
pablogsal added a commit to pablogsal/cpython that referenced this issue Jun 2, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 2, 2023
…pythonGH-105231)

(cherry picked from commit 41de543)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
@domdfcoding
Copy link
Contributor Author

Thanks for fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants