Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Backslash in f-string format specifier gets escape #105194

Closed
Assignees
pablogsallysnikolaou
Labels
type-bugAn unexpected behavior, bug, or error
@domdfcoding

Description

@domdfcoding

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 ann character (i.e. the string"\n")

The following script demonstrates this:

# issue.pyclassF:def__format__(self,format_spec):print("spec:",repr(format_spec))returnformat_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.pyspec: '\n'out:  'a\nb\nc'spec: '☃'out:  'a☃b☃c'spec: '\n'out:  'a\nb\nc'spec: '☃'out:  'a☃b☃c'
$python3.12 issue.pyspec: '\\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 theformat() 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

Metadata

Metadata

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp