You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PEP 701 – Syntactic formalization of f-strings #102856
I found that no one has claimed test_eof yet, so I made some work. :)
Failed test case: test_eof.test_eof_with_line_continuation
I looked at its commit history. This test case is a regression test for crash, so it seems like a good choice to keep the case and update the error message directly.
expect="unexpected EOF while parsing (<string>, line 1)"
Update unexpected EOF while parsing (<string>, line 1) to (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape (<string>, line 1)
@pablogsal in test_type_comments we have not used any f-strings.
The one failing there is this problem:
======================================================================
FAIL: test_fstring (test.test_type_comments.TypeCommentTests.test_fstring)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_type_comments.py", line 275, in test_fstring
for tree in self.parse_all(fstring, minver=6):
File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_type_comments.py", line 239, in parse_all
with self.assertRaisesRegex(SyntaxError, expected_regex,
AssertionError: SyntaxError not raised : feature_version=(3, 4)
----------------------------------------------------------------------
@pablogsal in test_type_comments we have not used any f-strings.
The one failing there is this problem:
======================================================================
FAIL: test_fstring (test.test_type_comments.TypeCommentTests.test_fstring)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_type_comments.py", line 275, in test_fstring
for tree in self.parse_all(fstring, minver=6):
File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_type_comments.py", line 239, in parse_all
with self.assertRaisesRegex(SyntaxError, expected_regex,
AssertionError: SyntaxError not raised : feature_version=(3, 4)
----------------------------------------------------------------------
We can just change this to 6, and this test will be pass.
I don't research this problem, but this solution looks like the simplest.
However... supporting syntax of python3.4 && 3.5 looks cinda strange.
We can just change this to 6, and this test will be pass.
I don't research this problem, but this solution looks like the simplest.
However... supporting syntax of python3.4 && 3.5 looks cinda strange.
I don't think that will work because we are not doing version checking anymore. See previous comments. The fix is probably to no pass feature_version.
@isidentical@lysnikolaou i have pushed some rules for error messages, please take a look and complete them with more if you have some spare cycles. With these the failures in test_fstring have decreased notably
I can confirm that the total number of failures has been decrased from 88 to 63. I'll try to see what are the most high impact ones and submit a PR to clear them.
If anyone intends to work on any of the remaining tasks in test_fstring, please double check with this PR (pablogsal#52) since it brings down the total failures to 30 with some explanations/required decisions for the rest.
Old parser and the new parser raises the same exception (UnicodeDecodeError), but with different col_offset. This is because it was raised by the wrong token.
I would consider it a bug in the old parser. Just as this comment mentions,
the error token was not correctly set in the old parser.
Maybe we should open an issue for the old parse? But the possible fixing might be error-prone, since we might need to keep track of every possible code path.
As for the new parser, I think a change in the test case would be fine.
The root cause is that someone forgot to use _PyArena_AddPyObject in 3 places.
This is very tricky because _PyArena_AddPyObject was scattered in many subroutines. Sometimes you should add _PyArena_AddPyObject, but sometimes you should not (add will cause a negative ref count).
Just like an old saying, managing memory by hand is so much pain, and also error-prone. This check can be done, by analyzing the PyObject*s registered to the arena by the time the AST was created, but that is a totally different story.
I think we can make it before beta 1. Formalizing fstring error messages (see pablogsal#52 (comment)) can be left to the end, since it is non-functional.
I think we can make it before beta 1. Formalizing fstring error messages (see pablogsal#52 (comment)) can be left to the end, since it is non-functional.
Yes and no: there may be some design consequences and I am not a fan of deactivating tests to reactivate them later.
There's a small discrepancy around the conversion character between our version and the string parser. The string parser does not allow spaces before or after the conversion character. For example:
What do you think about this? I feel okay about it, although it isn't really consistent with our decision on spaces after the ! character.
I feel that this is fine, and would be very difficult to disallow to be honest because these are different tokens anyway. It won't affect existing code and I don't see any reason to forbid it going foward.
======================================================================FAIL: test_format_specifier_expressions (test.test_fstring.TestCase.test_format_specifier_expressions) (str='f\'{"s"!r{":10"}}\'')
----------------------------------------------------------------------File"<string>", line1f'{"s"!r{":10"}}'^SyntaxError: f-string: expecting'}'Duringhandlingoftheaboveexception, anotherexceptionoccurred:
Traceback (mostrecentcalllast):
File"/Users/lysnikolaou/repos/python/cpython/Lib/test/test_fstring.py", line32, inassertAllRaisewithself.assertRaisesRegex(exception_type, regex):
AssertionError: "f-string: invalid conversion character 'r{"': expected 's', 'r', or 'a'" does not match "f-string: expecting '}' (<string>, line1)"
======================================================================FAIL: test_format_specifier_expressions (test.test_fstring.TestCase.test_format_specifier_expressions) (str="f'{4:{/5}}'")
----------------------------------------------------------------------File"<string>", line1f'{4:{/5}}'^SyntaxError: f-string: expecting'}'Duringhandlingoftheaboveexception, anotherexceptionoccurred:
Traceback (mostrecentcalllast):
File"/Users/lysnikolaou/repos/python/cpython/Lib/test/test_fstring.py", line32, inassertAllRaisewithself.assertRaisesRegex(exception_type, regex):
AssertionError: "f-string: invalid syntax"doesnotmatch"f-string: expecting '}' (<string>, line 1)"
These are hard to handle correctly, if we have the invalid_replacement_field rule stay the same to catch those expected '}' cases. There are the following 3 options, I guess.
Take more time to figure out whether we can find a smart way to differentiate between these things (can't see something since yesterday)
Be okay with sometimes emitting a expecting '}' error, while there are other syntax errors within the f-string (keeping invalid_replacement_field as-is).
Be okay with sometimes emitting a invalid syntax error, when we could have done a better error (probably making invalid_replacement_field require both a conversion character and a valid format spec).
Catching all errors seems unrealistic. But I think we do have an opportunity to improve.
By changing the invalid_replacement_field, we will have a better complain message about what to expect, and we can capture the syntax error from the field (yield_expr | star_expressions).
This fails when the f-string closes in a different line due to the expression taking multiple lines:
>>> f"blech{
... 1
... +
... 1
... }
... "
Or for example, if you do
>>> f"123{1}
...
In both cases the values of tok->cur and tok->line_start are garbage. I suspect the problem is that we are not updating current_tok->f_string_start and current_tok->f_string_multi_line_start if the f-string expression takes multiple lines and is not triple quotes.
To check this, please compile with --with-address-sanitizer
@mgmacias95@pablogsal How's the work on the tokenize module going? Are we going to be able to add that until the beta? Is it going to be considered okay to add it later as well?
Closing this as we already have a What's New entry, C tokenizer, and Python tokenizer. Let's tackle any small remaining items in separate issues from now on.
Activity
CharlieZhao95 commentedon Mar 22, 2023
I found that no one has claimed
test_eofyet, so I made some work. :)Failed test case:
test_eof.test_eof_with_line_continuationI looked at its commit history. This test case is a regression test for crash, so it seems like a good choice to keep the case and update the error message directly.
cpython/Lib/test/test_eof.py
Lines 39 to 40 in 72186aa
Update
unexpected EOF while parsing (<string>, line 1)to(unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape (<string>, line 1)ramvikrams commentedon Mar 23, 2023
@pablogsal in
test_type_commentswe have not used any f-strings.pablogsal commentedon Mar 23, 2023
The one failing there is this problem:
which I think is a feature version problem.
Eclips4 commentedon Mar 23, 2023
cpython/Lib/test/test_type_comments.py
Line 223 in 4695709
We can just change this to 6, and this test will be pass.
I don't research this problem, but this solution looks like the simplest.
However... supporting syntax of python3.4 && 3.5 looks cinda strange.
pablogsal commentedon Mar 23, 2023
I don't think that will work because we are not doing version checking anymore. See previous comments. The fix is probably to no pass
feature_version.sunmy2019 commentedon Mar 24, 2023
Looks like no one is analyzing
test_exceptions. I will look into it these two days.4 platforms seem to have met the same problem here.
pablogsal commentedon Mar 24, 2023
@isidentical @lysnikolaou i have pushed some rules for error messages, please take a look and complete them with more if you have some spare cycles. With these the failures in
test_fstringhave decreased notablyisidentical commentedon Mar 25, 2023
I can confirm that the total number of failures has been decrased from 88 to 63. I'll try to see what are the most high impact ones and submit a PR to clear them.
isidentical commentedon Mar 25, 2023
If anyone intends to work on any of the remaining tasks in
test_fstring, please double check with this PR (pablogsal#52) since it brings down the total failures to 30 with some explanations/required decisions for the rest.sunmy2019 commentedon Mar 25, 2023
After looking into the failure in
test_exceptions.Old parser and the new parser raises the same exception (UnicodeDecodeError), but with different col_offset. This is because it was raised by the wrong token.
I would consider it a bug in the old parser. Just as this comment mentions,
cpython/Parser/string_parser.c
Lines 38 to 48 in 64cb1a4
the error token was not correctly set in the old parser.
Maybe we should open an issue for the old parse? But the possible fixing might be error-prone, since we might need to keep track of every possible code path.
As for the new parser, I think a change in the test case would be fine.
sunmy2019 commentedon Mar 31, 2023
I am working on an PR to fix
test_unparsethis weekend._PyPegen_concatenate_stringsdid not implement concatenating emptyConstantwithFormattedValue, resultingunparsefailure.sunmy2019 commentedon Mar 31, 2023
Hi, I got some bad news.
I have been testing against memory leaks with
./python -m test -j $(nproc) -R :~30% of the tests failed on current head 270b661
For example,
These references are most likely to leak during the compilation (such as using
import, usingcompile/exec/eval, or usingast.parse)We might need to look into that.
Update: Memory Leakage fixed by commit pablogsal@d8b12e2
The root cause is that someone forgot to use
_PyArena_AddPyObjectin 3 places.This is very tricky because
_PyArena_AddPyObjectwas scattered in many subroutines. Sometimes you should add_PyArena_AddPyObject, but sometimes you should not (add will cause a negative ref count).Just like an old saying, managing memory by hand is so much pain, and also error-prone. This check can be done, by analyzing the PyObject*s registered to the arena by the time the AST was created, but that is a totally different story.
pablogsal commentedon Apr 6, 2023
Only 12 test left in
test_fstringand we are ready to go!lysnikolaou commentedon Apr 6, 2023
I'll have a look at them today.
sunmy2019 commentedon Apr 6, 2023
I think we can make it before beta 1. Formalizing fstring error messages (see pablogsal#52 (comment)) can be left to the end, since it is non-functional.
pablogsal commentedon Apr 6, 2023
I pushed a commit and we are now down to 8
pablogsal commentedon Apr 6, 2023
Yes and no: there may be some design consequences and I am not a fan of deactivating tests to reactivate them later.
lysnikolaou commentedon Apr 6, 2023
There's a small discrepancy around the conversion character between our version and the string parser. The string parser does not allow spaces before or after the conversion character. For example:
Our version, while not allowing for spaces between
!and the character, allows spaces after.What do you think about this? I feel okay about it, although it isn't really consistent with our decision on spaces after the
!character.pablogsal commentedon Apr 6, 2023
I feel that this is fine, and would be very difficult to disallow to be honest because these are different tokens anyway. It won't affect existing code and I don't see any reason to forbid it going foward.
pablogsal commentedon Apr 6, 2023
7 tests to go thanks to @lysnikolaou ❤️
lysnikolaou commentedon Apr 7, 2023
With pablogsal#65, we only have two errors to go.
These are hard to handle correctly, if we have the
invalid_replacement_fieldrule stay the same to catch thoseexpected '}'cases. There are the following 3 options, I guess.expecting '}'error, while there are other syntax errors within the f-string (keepinginvalid_replacement_fieldas-is).invalid syntaxerror, when we could have done a better error (probably makinginvalid_replacement_fieldrequire both a conversion character and a valid format spec).Thoughts?
sunmy2019 commentedon Apr 7, 2023
Catching all errors seems unrealistic. But I think we do have an opportunity to improve.
By changing the invalid_replacement_field, we will have a better complain message about what to expect, and we can capture the syntax error from the field
(yield_expr | star_expressions).sunmy2019 commentedon Apr 7, 2023
With this new grammer,
much better I think.
lysnikolaou commentedon Apr 7, 2023
Totally agree. Good idea! Will you open a PR?
sunmy2019 commentedon Apr 7, 2023
Sure
sunmy2019 commentedon Apr 11, 2023
Hi, there. Will anyone take a look at pablogsal#67?
pablogsal commentedon Apr 13, 2023
We are almost there, seems that we still have a small failure:
@isidentical this may be that we lack some changes in the untokenize function
sunmy2019 commentedon Apr 13, 2023
@pablogsal We lack changes in both the tokenize and the untokenize function. It cannot recognize the new f-string grammar. See pablogsal#67 (comment)
pablogsal commentedon Apr 13, 2023
We should be ok excluding the files that use the new funky syntax until we have the new tokenize implementation
pablogsal commentedon Apr 13, 2023
Seems that we have a failing tests in the buildbots:
https://buildbot.python.org/all/#/builders/802/builds/760
lysnikolaou commentedon Apr 13, 2023
Looking into the failure.
gh-102856: Initial implementation of PEP 701 (#102855)
pythongh-102856: Skip test_mismatched_parens in WASI builds
gh-102856: Skip test_mismatched_parens in WASI builds (#103633)
pythongh-102856: Skip failing test in test_fstring to fix WASI buildbot
pythongh-102856: Clean some of the PEP 701 tokenizer implementation
fixup! pythongh-102856: Clean some of the PEP 701 tokenizer implement…
gh-102856: Clean some of the PEP 701 tokenizer implementation (#103634)
pablogsal commentedon Apr 19, 2023
This code is kind of wrong:
cpython/Parser/tokenizer.c
Lines 2513 to 2517 in 6be7aee
This fails when the f-string closes in a different line due to the expression taking multiple lines:
Or for example, if you do
In both cases the values of
tok->curandtok->line_startare garbage. I suspect the problem is that we are not updatingcurrent_tok->f_string_startandcurrent_tok->f_string_multi_line_startif the f-string expression takes multiple lines and is not triple quotes.To check this, please compile with
--with-address-sanitizerMerge branch 'main' into superopt
pablogsal commentedon Apr 22, 2023
@cmaureir is going to work on allowing comments as per the specification.
pythongh-102856: Allow comments inside multi-line f-string expresions
pythongh-102856: Allow comments inside multi-line f-string expresions
pythongh-102856: Allow comments inside multi-line f-string expresions
lysnikolaou commentedon May 9, 2023
@mgmacias95 @pablogsal How's the work on the tokenize module going? Are we going to be able to add that until the beta? Is it going to be considered okay to add it later as well?
mgmacias95 commentedon May 9, 2023
hi @lysnikolaou, I just created a draft PR (#104323) please take a look.
pythongh-102856: Allow comments inside multi-line f-string expresions
gh-102856: Python tokenizer implementation for PEP 701 (#104323)
gh-102856: Tokenize performance improvement (#104731)
gh-102856: Allow comments inside multi-line f-string expresions (#104006
gh-102856: Add changes related to PEP 701 in 3.12 What's New docs (#1…
pythongh-102856: Add changes related to PEP 701 in 3.12 What's New do…
[3.12] gh-102856: Add changes related to PEP 701 in 3.12 What's New d…
pablogsal commentedon May 24, 2023
Closing this as we already have a What's New entry, C tokenizer, and Python tokenizer. Let's tackle any small remaining items in separate issues from now on.
Probably we need to alter https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals. @lysnikolaou can you take a go at that?
pythongh-102856: Add missing quote to fix doctest (pythonGH-104852)
gh-102856: Add missing quote to fix doctest (#104852)
lysnikolaou commentedon May 24, 2023
Sure!
[3.12] gh-102856: Add missing quote to fix doctest (GH-104852) (#104854)
pythongh-102856: Update Formatted string literals docs section after …
pythongh-102856: Update "Formatted string literals" docs section afte…
gh-102856: Update "Formatted string literals" docs section after PEP7…
pythongh-102856: Update "Formatted string literals" docs section afte…
[3.12] gh-102856: Update "Formatted string literals" docs section aft…
flying-sheep commentedon Oct 2, 2023
Ah great! I didn't know someone would continue PEP 536. Happy that it's happening!
Shouldn't PEP 536 be mentioned in this one?
pablogsal commentedon Oct 3, 2023
It's mentioned in the PEP:
https://peps.python.org/pep-0701/
Btw as heads up: We don't monitor normally closed issues so is very likely that people won't answer to comments when the issue is closed :)
flying-sheep commentedon Oct 3, 2023
Thanks! Seems like I missed the mention then. Perfect!