Skip to content

gh-108469: Update ast.unparse for unescaped quote support from PEP701 [3.12]#108553

Merged
pablogsal merged 11 commits into
python:mainfrom
tonybaloney:unparse_pep701
Sep 5, 2023
Merged

gh-108469: Update ast.unparse for unescaped quote support from PEP701 [3.12]#108553
pablogsal merged 11 commits into
python:mainfrom
tonybaloney:unparse_pep701

Conversation

@tonybaloney

@tonybaloney tonybaloney commented Aug 27, 2023

Copy link
Copy Markdown
Contributor

@sunmy2019

Copy link
Copy Markdown
Member

Sorry that I have taken over.

@sunmy2019
sunmy2019 marked this pull request as ready for review September 2, 2023 20:40
Comment thread Lib/test/test_unparse.py Outdated
@tonybaloney

Copy link
Copy Markdown
Contributor Author

Sorry that I have taken over.

You're welcome. I spent hours debugging these private methods and can't figure out which ones need removing or changing

@sunmy2019

Copy link
Copy Markdown
Member

Oh, I've found some corner case that previous logic did not handle. Give me some time for a more thorough rewrite.

@sunmy2019

Copy link
Copy Markdown
Member

I discovered it was a bug in the original implementation. Opened #108843.

@sunmy2019 sunmy2019 added 3.12 only security fixes 3.13 bugs and security fixes and removed tests Tests in the Lib/test dir labels Sep 3, 2023
@sunmy2019

Copy link
Copy Markdown
Member

Fixed on this branch.

Comment thread Lib/ast.py Outdated
def visit_FormattedValue(self, node):
def unparse_inner(inner):
unparser = type(self)(_avoid_backslashes=True)
unparser = type(self)(_avoid_backslashes=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we still need this mode or can we drop avoid_backslashes all together?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess we can drop it.

Comment thread Lib/ast.py Outdated
Comment on lines +1253 to +1266
for c in value:
if c == "\\" or not c.isprintable():
res.append(c.encode("unicode_escape").decode("ascii"))
continue
if c == quote:
if consecutive_quotes == len(quote_type) - 1:
# escape when we see a full `quote_type`
res.append("\\")
consecutive_quotes = 0
else:
consecutive_quotes += 1
else:
consecutive_quotes = 0
res.append(c)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This logic probably belongs somewhere else, something similiar to what we had with the existing f-string unparsing implementation:

https://github.com/python/cpython/blob/21229ce1f0c8fb2e580df62972296d629960f30e/Lib/ast.py#L1182C1-L1218C1

@isidentical

Copy link
Copy Markdown
Member

@sunmy2019 if we are interested in quickly shipping a fix for it, I'd avoid changing anything cosmetic and simply dropping the existing avoid_backslashes logic all together. It should probably probably work for all the supported cases. We can then simplify the handling of string literals with backslashes in a subsequent PR that only improves the generated code cosmeticly

@sunmy2019

Copy link
Copy Markdown
Member

I'd avoid changing anything cosmetic and simply dropping the existing avoid_backslashes logic all together

That what I tried to do before 1da1d4c.

Then I discovered a bug (#108843) in the original implementation. So I thought I can fix it along the way.

What do you think?

@isidentical

Copy link
Copy Markdown
Member

I think separting the fix for that from the actual PEP 701 support makes sense, and also reduces the possible complexity of changes this PR brings in. Let's first do the cleanup and the support for PEP 701 and we can do the fix later since this seems like the more serious problem.

@pablogsal
pablogsal requested a review from Yhg1s September 4, 2023 18:25
@pablogsal

Copy link
Copy Markdown
Member

Let's try to land this today or tomorrow. I added @Yhg1s to the PR so he is aware of it.

@sunmy2019

Copy link
Copy Markdown
Member

Let's first do the cleanup and the support for PEP 701

I got it reverted.

@pablogsal

pablogsal commented Sep 5, 2023

Copy link
Copy Markdown
Member

@sunmy2019 can you make a PR for the fix changes and ping me, @isidentical and @Yhg1s ?

@pablogsal
pablogsal merged commit 2c4c26c into python:main Sep 5, 2023
@pablogsal pablogsal added the needs backport to 3.12 only security fixes label Sep 5, 2023
@miss-islington

Copy link
Copy Markdown
Contributor

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 5, 2023
…PEP701 [3.12] (pythonGH-108553)

(cherry picked from commit 2c4c26c)

Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
@bedevere-bot

Copy link
Copy Markdown

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

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Sep 5, 2023
pablogsal pushed a commit that referenced this pull request Sep 5, 2023
… PEP701 [3.12] (GH-108553) (#108960)

Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
@sunmy2019

Copy link
Copy Markdown
Member

@sunmy2019 can you make a PR for the fix changes and ping me, @isidentical and @Yhg1s ?

Working on it.

hauntsaninja added a commit to hauntsaninja/cpython that referenced this pull request Dec 31, 2023
As of python#108553, the `_avoid_backslashes` code path is dead

`scape_newlines` was introduced in python#110271. Happy to drop the typo fix
if we don't want it
hauntsaninja added a commit that referenced this pull request Jan 16, 2024
As of #108553, the `_avoid_backslashes` code path is dead

`scape_newlines` was introduced in #110271. Happy to drop the typo fix
if we don't want it
kulikjak pushed a commit to kulikjak/cpython that referenced this pull request Jan 22, 2024
As of python#108553, the `_avoid_backslashes` code path is dead

`scape_newlines` was introduced in python#110271. Happy to drop the typo fix
if we don't want it
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
As of python#108553, the `_avoid_backslashes` code path is dead

`scape_newlines` was introduced in python#110271. Happy to drop the typo fix
if we don't want it
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
As of python#108553, the `_avoid_backslashes` code path is dead

`scape_newlines` was introduced in python#110271. Happy to drop the typo fix
if we don't want it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.12 only security fixes 3.13 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants