GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Backslash in f-string format specifier gets escape · Issue #105194 · python/cpython

Backslash in f-string format specifier gets escape #105194

New issue
Copy link
New issue
Copy link
Closed
Closed
Backslash in f-string format specifier gets escape#105194
Copy link
Assignees
pablogsallysnikolaou
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@domdfcoding
domdfcoding
opened on Jun 1, 2023
Issue body actions

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

  • gh-105194: Fix format specifier escaped characters in f-strings #105231
  • [3.12] gh-105194: Fix format specifier escaped characters in f-strings (GH-105231) #105234

Activity

  1. domdfcoding
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jun 1, 2023
  2. AlexWaygood
    assigned
    pablogsal
    and
    lysnikolaou
    on Jun 1, 2023
  3. pablogsal commented on Jun 1, 2023

    @pablogsal
    pablogsal
    on Jun 1, 2023
    Member
    More actions

    @isidentical @lysnikolaou can you take this one?

  4. lysnikolaou commented on Jun 1, 2023

    @lysnikolaou
    lysnikolaou
    on Jun 1, 2023
    Member
    More actions

    Sure! Will probably have a look tomorrow morning.

    Reacted by Pablo Galindo Salgado
  5. pablogsal
    added a commit that references this issue on Jun 2, 2023

    pythongh-105194: Fix format specifier escaped characters in f-strings

    a63b87a
  6. pablogsal commented on Jun 2, 2023

    @pablogsal
    pablogsal
    on Jun 2, 2023
    Member
    More actions

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

  7. pablogsal
    added 3 commits that reference this issue on Jun 2, 2023

    fixup! pythongh-105194: Fix format specifier escaped characters in f-…

    9907c0f

    fixup! fixup! pythongh-105194: Fix format specifier escaped character…

    fd297bd

    gh-105194: Fix format specifier escaped characters in f-strings (#105231

    41de543
  8. pablogsal
    closed this as completedon Jun 2, 2023
  9. miss-islington
    added a commit that references this issue on Jun 2, 2023

    pythongh-105194: Fix format specifier escaped characters in f-strings (…

    e32ae2a
  10. pablogsal
    added a commit that references this issue on Jun 2, 2023

    [3.12] gh-105194: Fix format specifier escaped characters in f-strings (

    103ae4e
  11. domdfcoding commented on Jun 3, 2023

    @domdfcoding
    domdfcoding
    on Jun 3, 2023
    ContributorAuthor
    More actions

    Thanks for fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @lysnikolaou
    lysnikolaou
  • @pablogsal
    pablogsal

Labels

type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.