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

f-strings: allow backslashes only in the string parts, not in the expression parts #72135

Closed
ericvsmith opened this issue Sep 3, 2016 · 5 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ericvsmith
Copy link
Member

BPO 27948
Nosy @ericvsmith, @flying-sheep, @serhiy-storchaka
Files
  • 27948-1.diff
  • 27948-2.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ericvsmith'
    closed_at = <Date 2016-09-10.01:59:14.229>
    created_at = <Date 2016-09-03.13:24:05.065>
    labels = ['interpreter-core', 'type-bug']
    title = 'f-strings: allow backslashes only in the string parts, not in the expression parts'
    updated_at = <Date 2016-09-19.08:56:02.116>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2016-09-19.08:56:02.116>
    actor = 'flying sheep'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2016-09-10.01:59:14.229>
    closer = 'eric.smith'
    components = ['Interpreter Core']
    creation = <Date 2016-09-03.13:24:05.065>
    creator = 'eric.smith'
    dependencies = []
    files = ['44438', '44499']
    hgrepos = []
    issue_num = 27948
    keywords = ['patch']
    message_count = 5.0
    messages = ['274294', '274811', '275309', '275540', '275541']
    nosy_count = 4.0
    nosy_names = ['eric.smith', 'flying sheep', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue27948'
    versions = ['Python 3.6']

    @ericvsmith
    Copy link
    Member Author

    See bpo-27921.

    Currently (and for 3.6 beta 1), backslashes are not allowed anywhere in f-strings. This needs to be changed to allow them in the string parts, but not in the expression parts.

    Also, require that the start and end of an expression be literal '{' and '}, not escapes like '\0x7b' and '\u007d'.

    @ericvsmith ericvsmith self-assigned this Sep 3, 2016
    @ericvsmith ericvsmith added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 3, 2016
    @ericvsmith
    Copy link
    Member Author

    This patch implements the new logic. It passes all of the fstring tests (except of course the ones that had backslashes in the expressions). I'd still like to add some more tests, because I'm not convinced the named unicode escapes are correct (f"\N{LEFT CURLY BRACE}". Because I'm now scanning the decoded utf-8 version of the string, I need to be aware of this syntax and not decide it's part of an expression (because of the braces).

    @ericvsmith
    Copy link
    Member Author

    Updated patch, works against HEAD. I added a few tests and tweaked some comments. My goal is to commit this today, before the 3.6b1 cutoff. I don't think there's much risk to it.

    I still need to update the PEP.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 10, 2016

    New changeset b3ac1e154cdd by Eric V. Smith in branch 'default':
    bpo-27948: Allow backslashes in the literal string portion of f-strings, but not in the expressions. Also, require expressions to begin and end with literal curly braces.
    https://hg.python.org/cpython/rev/b3ac1e154cdd

    @ericvsmith
    Copy link
    Member Author

    I've fixed the code and fixed/added tests. I still need to update the docs and PEP-498. I'll create a separate issue for the docs, once I'm done with the PEP.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    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
    Development

    No branches or pull requests

    1 participant