Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-112364: correct unparsing of backslashes and quotes #115696

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

Merged
merged 1 commit into from
Feb 21, 2024

Conversation

15r10nk
Copy link
Contributor

@15r10nk 15r10nk commented Feb 19, 2024

This pull-request fixes also two other things which are related to the original issue:

  • " can cause the same problem than '
  • \ backslashes where not escaped which converted \\ into \ and not \\.

@ghost
Copy link

ghost commented Feb 19, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Feb 19, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@15r10nk 15r10nk force-pushed the fix-issue-112364 branch 3 times, most recently from 5290f7f to ced3764 Compare February 20, 2024 18:40
@15r10nk 15r10nk marked this pull request as ready for review February 20, 2024 18:40
@15r10nk 15r10nk requested a review from isidentical as a code owner February 20, 2024 18:40
@15r10nk
Copy link
Contributor Author

15r10nk commented Feb 20, 2024

@pablogsal I think this solves the issue. Did I miss anything?

@@ -0,0 +1 @@
Fixed :func:`ast.unparse` to handle format_spec with ``"``, ``'`` or ``\\``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "Patch by Frank Hoffmann" at the end 😉

@pablogsal pablogsal added the needs backport to 3.12 only security fixes label Feb 20, 2024
@@ -649,6 +649,21 @@ def test_multiquote_joined_string(self):
self.check_ast_roundtrip("""f'''""\"''\\'{"\\n\\"'"}''' """)
self.check_ast_roundtrip("""f'''""\"''\\'{""\"\\n\\"'''""\" '''\\n'''}''' """)

def test_backslash_in_format_spec(self):
self.check_ast_roundtrip("""f"{x:\\}" """)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests with an uneven number of \?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this should be valid:

>>> class A:
...   def __format__(self, *args):
...      print(args)
...      return "foo"
...
>>> f"{ A():\\\ }"
('\\\\ ',)
'foo'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you confused me and I checked it.

>>> len("""\\""")
1

This example already has 1,2,3 and 4 backslashes. But I added a space at the end like in your example.

@pablogsal
Copy link
Member

LGTM! Great job and congratulations on your first CPython bugfix! 🎉

Hopefully the first of many 😉

@pablogsal pablogsal merged commit 69ab930 into python:main Feb 21, 2024
@miss-islington-app
Copy link

Thanks @15r10nk for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @15r10nk and @pablogsal, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 69ab93082d14425aaac48b8393711c716575b132 3.12

@pablogsal
Copy link
Member

Sorry, @15r10nk and @pablogsal, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 69ab93082d14425aaac48b8393711c716575b132 3.12

@15r10nk Can you follow these instructions to do the 3.12 backport?

@15r10nk
Copy link
Contributor Author

15r10nk commented Feb 21, 2024

Yes, I will port it back in the evening.

@bedevere-app
Copy link

bedevere-app bot commented Feb 21, 2024

GH-115782 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Feb 21, 2024
15r10nk added a commit to 15r10nk/cpython that referenced this pull request Feb 21, 2024
…n ast.unparse (pythonGH-115696)

(cherry picked from commit 69ab930)

Co-authored-by: Frank Hoffmann <[email protected]>
pablogsal pushed a commit that referenced this pull request Feb 21, 2024
#115782)

[3.12] gh-112364: Correct unparsing of backslashes and quotes in ast.unparse (GH-115696)
(cherry picked from commit 69ab930)
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this pull request Mar 4, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
LukasWoodtli pushed a commit to LukasWoodtli/cpython that referenced this pull request Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants